Basic Data Types

Basic Data Types Contents

Basic Data Types Contents............................................................................................................................................................................. 1

Basic Data Types............................................................................................................................................................................................................... 1

Coordinates and Twips......................................................................................................................................................................................................... 1

Integer Types and Byte Order.......................................................................................................................................................................................... 2

Fixed Point Numbers............................................................................................................................................................................................................ 2

Bit Values..................................................................................................................................................................................................................................... 3

Using Bit Values...................................................................................................................................................................................................................... 4

String Values.............................................................................................................................................................................................................................. 4

RGB Color Record................................................................................................................................................................................................................ 5

RGBA Color with Alpha Record................................................................................................................................................................................... 5

Rectangle Record.................................................................................................................................................................................................................... 5

Matrix Record........................................................................................................................................................................................................................... 5

Color Transform Record.................................................................................................................................................................................................... 6

Color Transform with Alpha Record........................................................................................................................................................................... 7

 

Basic Data Types

This section describes the basic data types that make up the more complex data structures in the SWF file format.  All other structures in SWF are built on these fundamental types, so it is important to understand these types before moving on.

Coordinates and Twips

SWF stores all X-Y coordinates as integers, in a unit of measurement called a twip.  Strictly speaking, a twip is defined as 1/1440th of an inch, or 1/20th of a point, a traditional measure in printing.   However, in the context of the SWF format, it is easiest to think of a twip as 1/20th of a logical pixel.

A logical pixel is the same as a screen pixel when the movie is played at 100% – i.e. without scaling.

 

For example, a rectangle 800 twips wide by 400 twips high is rendered as 40 by 20 logical pixels. Fractional pixel sizes are approximated with antialiasing.  A rectangle 790 by 390 twips (39.5 by 19.5 pixels) appears to have slightly blurred edges.

 

Twips are a good compromise between size and precision.  They provide sub-pixel accuracy for zooming and precise placement of objects, while consuming very few bits per coordinate.

 

Integer Types and Byte Order

SWF supports 8-bit, 16-bit and 32-bit, signed and unsigned integer types.   All integer values are stored in the SWF file using the little-endian byte order.  That is, the least significant byte is stored first, and the most significant byte is stored last, in the same way as the Intel x86 architecture.  The 32-bit value B1B2B3B4, is stored in the SWF file as: B4B3B2B1.

 

For example:

 

The 32-bit value 0x456e7120      is stored as: 20 71 6e 45

The 16-bit value 0xe712            is stored as: 12 e7

 

The Flash Format SDK isolates you from endian issues.  The methods OutputByte, OutputWord, OutputDWord of the FSWFStream class, always write integer values in the little-endian format

 

All integer types must be byte-aligned.  That is, the first bit of an integer value must be stored in the first bit of a byte in the SWF file.

 

Integer Types

Type

Comment

SI8

Signed 8-bit integer value

SI16

Signed 16-bit integer value

SI32

Signed 32-bit integer value

SI8[n]

Signed 8-bit array - n is number of array elements

SI16[n]

Signed 16-bit array - n is number of array elements

UI8

Unsigned 8-bit integer value

UI16

Unsigned 16-bit integer value

UI32

Unsigned 32-bit integer value

UI8[n]

Unsigned 8-bit array - n is number of array elements

UI16[n]

Unsigned 16-bit array - n is number of array elements

UI32[n]

Unsigned 32-bit array - n is number of array elements

 

Fixed Point Numbers

Fixed values are 32-bit 16.16 signed fixed-point numbers.  That is, the high 16 bits represent the number before the decimal point, and the low 16 bits represent the number after the decimal point.

FIXED values are stored like 32 bit integers in the SWF file (using the little-endian format) and must be byte-aligned.

 

For example:

 

The real value 7.5 is equivalent to: 0007.8000

Which is equal to the 32-bit value:            0x00078000

Which is stored in the SWF file as:            00 80 07 00

 

FIXED

Type

Comment

FIXED

  32-bit 16.16 fixed-point number

 

 

Bit Values

Bit values are variable-length bit fields that can represent three types of numbers:

 

·             Unsigned integers

·             Signed integers

·             Signed 16.16 fixed-point values.

 

Bit values do not have to be byte-aligned.  A single bit value can spread across multiple bytes, and more than one bit value can be packed into a single byte.  Other types (such as U8 an U16) are always byte-aligned.  If a byte-aligned type follows a bit-value, the byte containing the bit-value is padded out with zeros.

 

Below is a stream 64 bits, made up of 9 variable-length bit values followed by a U16 value:

 

Byte1---Byte2---Byte3---Byte4---Byte5---Byte6---Byte7---Byte8---

0101101010010010010111100100011010111001100100000100110010101101

BV1---BV2--BV3---BV4-BV5-----BV6BV7--BV8BV9—pad-U16-------------

 

The bit stream begins with a 6-bit value (BV1) followed by a 5-bit value (BV2) which is spread across Byte1 and Byte2.  BV3 is spread across Byte2 and Byte3, while BV4 is wholly contained within Byte3.  Byte 5 contains two bit values; BV7 and BV8.  BV9 is followed by a byte-aligned type (U16) so the last four bits of Byte6 are padded with zeros.

 

Bit Values

Type

Comment

SB[nBits]

Signed bit value  (nBits is the number of bits used to store the value)

UB[nBits]

Unsigned bit value  (nBits is the number of bits used to store the value)

FB[nBits]

Signed fixed-point bit value  (nBits is the number of bits used to store the value)

 

When an unsigned bit value is expanded into a larger word size the leftmost bits are filled with zeros.  When a signed bit value is expanded into a larger word size the high bit is copied to the left most bits.

This is called sign extension.  For example, the 4-bit unsigned value UB[4] = 1110 would be expanded to a 16-bit value like this: 0000000000001110 = 14.  The same value interpreted as a signed value (SB[4] = 1110) would be expanded to 1111111111111110 = –2.

 

Bit values are stored using the minimum number of bits possible. The number of bits required to store an unsigned bit value is determined by the position of the leftmost ‘1’ bit.  For example, say the integer 35 was stored in a 16-bit word like this: 0000000000100011.  The leftmost ‘1’ is 6 bits from the end of the number, so this number can be represented as UB[6].  The leftmost 10 bits are ‘chopped off’ and discarded.

 

Signed bit values are similar but must take account of the sign bit.  The signed value of 35 is represented as SB[7] = 0100011.  The extra zero bit is required otherwise the high-bit would be sign-extended and the value would be interpreted as negative.

 

Fixed-point bit values are 32-bit 16.16 signed fixed-point numbers.  That is, the high 16 bits represent the number before the decimal point, and the low 16 bits represent the number after the decimal point.  A fixed-point bit value is identical to a signed bit value, but the interpretation is different.  For example, a 19-bit signed bit value of 0x30000 is interpreted as 196608 decimal.  The 19-bit fixed-point bit value 0x30000 is interpreted as 3.0.  In effect, the value was stored as 3.16.

 

Using Bit Values

Whenever possible, bit values are packed into the smallest possible number of bits possible. Coordinates are commonly stored using variable-sized bit fields.  A field indicates how many bits are used to represent subsequent values in the record.  For example, take a look at the RECT structure below:

 

RECT

Field

Type

Comment

Nbits

nBits = UB[5]

Bits in each rect value field

Xmin

SB[nBits]

X minimum position for rect

Xmax

SB[nBits]

X maximum position for rect

Ymin

SB[nBits]

Y minimum position for rect

Ymax

SB[nBits]

Y maximum position for rect

 

The nBits field determines the number of bits used to store the coordinate values Xmin, Xmax, Ymin, and Ymax.  Say the coordinates of the rectangle were as follows:

 

Xmin = 127 decimal =    1111111 binary

Xmax = 260 decimal =   10000100 binary

Ymin =  15 decimal =       1111 binary

Ymax = 514 decimal = 1000000010 binary

 

Nbits is calculated by finding the coordinate that requires the most bits to represent.   In this case that value is 514 (01000000010 binary) which requires 11 bits to represent.  So the rectangle is stored as below:

 

RECT

Field

Type

Comment

Nbits

UB[5] = 1011

Bits required (11)

Xmin

SB[11] = 00001111111

X minimum in twips (127)

Xmax

SB[11] = 00010000100

X maximum in twips (260)

Ymin

SB[11] = 00000001111

Y minimum in twips (15)

Ymax

SB[11] = 01000000010

Y maximum in twips (514)

 

String Values

A string value represents a null terminated ASCII or multiple byte character string.   The format for a string value is a sequential list of bytes terminated by the null character byte.

 

(See class FString in the Flash File Format SDK)

 

STRING

Field

Type

Comment

String

UI8[zero or more]

Non-null string character data

StringEnd

UI8

Marks end of string - always zero

 

 

RGB Color Record

The RGB record represents a color as 24-bit red, green, and blue value.

 

(See class FRGB in the Flash File Format SDK)

 

RGB

Field

Type

Comment

Red

UI8

Red color value

Green

UI8

Green color value

Blue

UI8

Blue color value

 

 

RGBA Color with Alpha Record

The RGBA record represents a color as 32-bit red, green, blue and alpha value.  An RGBA color with an alpha value of 255 is completely opaque.  An RGBA color with an alpha value of zero is completely transparent.  Alpha values between zero and 255 are partially transparent.

 

(See class FRGBA in the Flash File Format SDK)

 

RGBA

Field

Type

Comment

Red

UI8

Red color value

Green

UI8

Green color value

Blue

UI8

Blue color value

Alpha

UI8

Transparency color value

 

 

Rectangle Record

A rectangle value represents a rectangular region defined by a minimum X- and Y-coordinate position with a maximum X- and Y-coordinate position.

 

RECT

Field

Type

Comment

Nbits

nBits = UB[5]

Bits used for each subsequent field

Xmin

SB[nBits]

X minimum position for rectangle in twips

Xmax

SB[nBits]

X maximum position for rectangle in twips

Ymin

SB[nBits]

Y minimum position for rectangle in twips

Ymax

SB[nBits]

Y maximum position for rectangle in twips

 

(See class FRect in the Flash File Format SDK)

 

Matrix Record

The MATRIX record represents a standard 2x3 transformation matrix.  It is used to describe the scale, rotation and translation of a graphic object.

 

(See class FMatrix in the Flash File Format SDK)

 

MATRIX

Field

Type

Comment

HasScale

hasScale = UB[1]

Has scale values if equal to 1

NScaleBits

If hasScale nScaleBits = UB[5]

Bits in each scale value field

ScaleX

If hasScale FB[nScaleBits]

X scale value

ScaleY

If hasScale FB[nScaleBits]

Y scale value

HasRotate

hasRotate = UB[1]

Has rotate and skew values if equal to 1

NRotateBits

If hasRotate nRotateBits = UB[5]

Bits in each rotate value field

RotateSkew0

If hasRotate FB[nRotateBits]

First rotate and skew value

RotateSkew1

If hasRotate FB[nRotateBits]

Second rotate and skew value

NTranslateBits

nTranslateBits = UB[5]

Bits in each translate value field

TranslateX

SB[nTranslateBits]

X translate value in twips

TranslateY

SB[nTranslateBits]

Y translate value in twips

 

The ScaleX, ScaleY, RotateSkew0 and RotateSkew1 fields are stored as 16.16 fixed-point values.  The TranslateX and TranslateY values are stored as signed values in twips.

 

The MATRIX record is optimized for common cases such as matrix that performs a translation only.  In this case the HasScale and HasRotate flags are zero, and the matrix only contains the TranslateX and TranslateY fields.

 

The mapping from the MATRIX fields to the 2x3 matrix is as follows:

 

| ScaleX         RotateSkew0  |

| RotateSkew1  ScaleY       |

| TranslateX   TranslateY   |

 

For any coordinates (x, y), the transformed coordinates  (x', y') are calculated as follows:

 

x' = x * ScaleX + y * RotateSkew1 + TranslateX

y' = x * RotateSkew0 + y * ScaleY + TranslateY

 

The following table describes how the members of the matrix are used for each type of operation:

 

Operation

ScaleX

RotateSkew0

RotateSkew1

ScaleY

Rotation

Cosine

Sine

Negative sine

Cosine

Scaling

Horizontal scaling component

Nothing

Nothing

Vertical Scaling Component

Shear

Nothing

Horizontal Proportionality Constant

Vertical Proportionality Constant

Nothing

Reflection

Horizontal Reflection Component

Nothing

Nothing

Vertical Reflection Component

 

 

Color Transform Record

The CXFORM record defines a simple transform that can be applied to the color space of a graphic object.    There are two types of transform possible:

 

1.          Addition Transforms

2.          Multiplication Transforms

 

Addition transforms simply add a fixed value (positive or negative) to the red, green and blue components of the object being displayed.  If the result is greater than 255, the result is clamped to 255.  If the result is less than zero, the result is clamped to zero.

 

For any color (R,G,B) the transformed color (R', G', B') is calculated as follows:

 

R' = max(0, min(R + RedAddTerm,   255))

G' = max(0, min(G + GreenAddTerm, 255))

B' = max(0, min(B + BlueAddTerm,  255))

 

Multiplication transforms multiply the red, green and blue components by a fixed value and divide the result by 255.

 

For any color (R,G,B) the transformed color (R', G', B') is calculated as follows:

 

R' = (R * RedMultTerm)   / 255)

G' = (G * GreenMultTerm) / 255)

B' = (B * BlueMultTerm)  / 255)

 

Addition and Multiplication transforms can be combined into a single operation.

 

(See class FCXForm in the Flash File Format SDK)

 

CXFORM

Field

Type

Comment

HasAddTerms

hasAdd = UB[1]

Has color addition values if equal to 1

HasMultTerms

hasMult = UB[1]

Has color multiply values if equal to 1

Nbits

nBits = UB[4]

Bits in each value field

RedMultTerm

If hasMult SB[nBits]

Red multiply value

GreenMultTerm

If hasMult SB[nBits]

Green multiply value

BlueMultTerm

If hasMult SB[nBits]

Blue multiply value

RedAddTerm

If hasAdd SB[nBits]

Red addition value

GreenAddTerm

If hasAdd SB[nBits]

Green addition value

BlueAddTerm

If hasAdd SB[nBits]

Blue addition value

Color Transform with Alpha Record

The CXFORMWITHALPHA record extends the functionality of CXFORM by allowing color transforms to be applied to the alpha channel, as well as the red, green and blue channels.

 

There are two types of transform possible:

 

1.          Addition Transforms

2.          Multiplication Transforms

 

Addition transforms simply add a fixed value (positive or negative) to the red, green, blue and alpha components of the object being displayed.  If the result is greater than 255, the result is clamped to 255.  If the result is less than zero, the result is clamped to zero.

 

For any color (R,G,B,A) the transformed color (R', G', B', A') is calculated as follows:

 

R' = max(0, min(R + RedAddTerm,    255))

G' = max(0, min(G + GreenAddTerm,  255))

B' = max(0, min(B + BlueAddTerm,   255))

A' = max(0, min(A + AlphaAddTerm,  255))

 

Multiplication transforms multiply the red, green, blue and alpha components by a fixed value and divide the result by 255.

 

For any color (R,G,B,A) the transformed color (R', G', B', A') is calculated as follows:

 

R' = (R * RedMultTerm)    / 255)

G' = (G * GreenMultTerm)  / 255)

B' = (B * BlueMultTerm)   / 255)

A' = (A * AlphaMultTerm)  / 255)

 

The CXFORMWITHALPHA record is most commonly used to display objects as partially transparent.  This is achieved by multiplying the alpha channel by some value between zero and 255.

 

(See class FCXFormWAlpha in the Flash File Format SDK)

 

 

CXFORMWITHALPHA

Field

Type

Comment

HasAddTerms

HasAdd = UB[1]

Has color addition values if equal to 1

HasMultTerms

HasMult = UB[1]

Has color multiply values if equal to 1

Nbits

NBits = UB[4]

Bits in each value field

RedMultTerm

If hasMult SB[nBits]

Red multiply value

GreenMultTerm

If hasMult SB[nBits]

Green multiply value

BlueMultTerm

If hasMult SB[nBits]

Blue multiply value

AlphaMultTerm

If hasMult SB[nBits]

Alpha multiply value

RedAddTerm

If hasAdd SB[nBits]

Red addition value

GreenAddTerm

If hasAdd SB[nBits]

Green addition value

BlueAddTerm

If hasAdd SB[nBits]

Blue addition value

AlphaAddTerm

If hasAdd SB[nBits]

Transparency addition value