[REBOL] Re: [BUG] in make struct! ?
From: rebol-list2:seznam:cz at: 4-Aug-2003 19:59
Hello Gabriele,
Tuesday, July 29, 2003, 12:44:57 PM, you wrote:
GS> Hi rebOldes,
GS> On Tuesday, July 29, 2003, 12:28:35 PM, you wrote:
r>> but:
>>>> s: make struct! [a [short] b [long]] [1 2] third s
r>> == #{0100000002000000}
GS> That's called padding. Most CPUs/architectures require that 32 bit
GS> data be word aligned, i.e. start on an address that is divisible
GS> by 4. This is very platform dependent anyway, so what you see here
GS> is not what you might get on other platforms. Remember that
GS> structs are intended for interfacing with C libraries.
if it would be padding, why this struct is not padded as well:
>> s: make struct! [a [short] b [short] c [short]] [1 2 3] third s
== #{010002000300}
I would like to see Carl's comment (probably will have to try some luck
and write on the feedback)
If this is not a bug it's bad, because one will not be able to do thinks
like that (which is working on windows):
REBOL [
Title: "Bmp-tools"
Date: 25-Jul-2003/19:14:56+2:00
Name: none
Version: 0.0.1
File: %bmp-tools.r
Author: "oldes"
Usage: [
bmp-info %test.bmp
bmp-create %/j/test/test3.bmp 20x20
]
Purpose: {'bmp-create is a function useful to create extra large BMP files}
Email: [oliva--david--seznam--cz]
Links: [
http://www.wotsit.org/download.asp?f=bmpfrmat
http://www.wotsit.org/download.asp?f=bmp
]
Require: [rss-utils %print-struct.r]
]
print-struct: func[
{Prints readable pairs of variables and values of the struct! datatype}
st [struct!] "Struct! to explore"
/local val i
][
i: 0
parse first st [
opt [set val string!] (print val loop length? val [prin "="] print "")
any [
set val word! (
insert/dup tail val: to-string val #"." 20 - length? val
print [val pick second st i: i + 1]
)
| any-type!
]]
]
s_BitmapFileHeader: make struct! [
"Size, and layout of a device-independent bitmap file"
;Type [short] "The characters identifying the bitmap."
Size [long] "Complete file size in bytes"
Reserved [long] "Reserved for later use."
OffsetBits [long] "Offset from beginning of file to the beginning of the bitmap
data."
] none
s_BitmapInfoHeader: make struct! [
"The dimensions, compression type, and color format for the bitmap."
Size [long] "Length of the Bitmap Info Header"
Width [long] "Horizontal width of bitmap in pixels"
Height [long] "Vertical height of bitmap in pixels"
Planes [short] "Number of planes in this bitmap"
BitCount [short] "Bits per pixel used to store palette entry information"
Compression [long] "Compression specifications"
SizeImage [long] "Size of the bitmap data in bytes"
XPelsPerMeter [long] "Horizontal resolution expressed in pixel per meter"
YPelsPerMeter [long] "Vertical resolution expressed in pixels per meter"
ColorsUsed [long] "Number of colors used by this bitmap"
ColorsImportant [long] "Number of important colors"
] none
bmp-info: func [bmp [binary! port! file!] "BMP file to parse"][
if file? bmp [bmp: open/direct bmp]
if (to string! copy/part bmp 2) <> "BM" [make error! "Not a BMP file!"]
if binary? bmp [bmp: skip bmp 2]
change third s_BitmapFileHeader copy/part bmp 12
print-struct s_BitmapFileHeader
if binary? bmp [bmp: skip bmp 12]
change third s_BitmapInfoHeader copy/part bmp 40
print-struct s_BitmapInfoHeader
if port? bmp [close bmp]
]
bmp-create: func [
target [file! none!] "Where to save the new BMP file"
size [pair!] "Size of the created BMP file"
][
bmp: open/new/direct/binary/write/no-wait target
insert bmp #{424D}
insert bmp third make struct! s_BitmapFileHeader reduce [(54 + (3 * size/x * size/y))
0 54]
insert bmp third make struct! s_BitmapInfoHeader reduce [40 size/x size/y 1 24 0 0 11808
11808 0 0]
loop size/y [
insert/dup bmp #{FFFFFF} size/x
]
close bmp
bmp/state/outBuffer: none
unset 'bmp
]
--
Best regards,
rebOldes mailto:[oliva--david--seznam--cz]