Macromedia Flash File Format (SWF)

recompiled with RAW HTWL

Basic Data Types


Introduction Basic Types Display List Control Tags
Shapes (Examples Shapes) Gradients Buttons
Sprites Fonts and Text Shape Morphing Bitmap
Sounds Actions ActionScripts Reference

Basic Data Types Contents

Basic Data Types
Coordinates and Twips
Integer Types and Byte Order
Fixed Point Number
Bit Values
Using Bit Values
String Values
RGB Color Record
RGBA Color with Alpha Record
Rectangle Record
Matrix Record
Color Transform Record
Color Transform with Alpha Record

Basic Data Types

This section describes the basic data types that make up the more complex data structures in the Macromedia Flash File Format (SWF). All other structures in Macromedia Flash File Format (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 Macromedia Flash (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

Macromedia Flash (SWF) supports 8-bit, 16-bit and 32-bit, signed and unsigned integer types. All integer values are stored in the Macromedia Flash (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 Macromedia Flash (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     
    SwfJava v0.0: how to create SWF-integer objects    
    new UI8 ( int value );    
    new UI16 ( int value );    
    new UI32 ( long value );    


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 Macromedia Flash (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 MACROMEDIA FLASH (SWF) file as:    00 80 07 00
     FIXED     
     Type           Comment     
     FIXED           32-bit 16.16 fixed-point number     
    SwfJava v0.0: how to create a Fixed object    
    new Fixed ( int value );    
    new Fixed ( float value );    


Bit Values

Bit values are variable-length bit fields that can represent three types of numbers: 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 UI8 or UI16 ) 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 UI16 value:
Byte1--- Byte2--- Byte3--- Byte4--- Byte5--- Byte6--- Byte7--- Byte8---
010110 10100 100100 1011 11001000 110 10111 001 1001 0000 0100110010101101
BV1---BV2--BV3---BV4-BV5-----BV6BV7--BV8BV9-pad-UI16------------
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 ( UI16 ) 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.
  1. The 19-bit fixed-point bit value 0x30000 = 111.000000000000000 is in fact equal to -1.0, since the sign bit is on. To store the decimal value 3.0 in a FB[n], we need 20 bits: 0111.0000000000000000.
  2. The 19-bit fixed-point bit value 111.8000000000000000 is equal to -0.5: 111.8000000000000000 = 111.0000000000000000 + 0.8000000000000000 = -1 + 0.5.
    SwfJava v0.0: how to create bit values objects    
    new SB ( int nBits, long value );    
    new UB ( int nBits, long value );    
    new FB ( int nBits, int value );    


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.
     STRING     
     Field           Type           Comment     
     String           UI8[zero or more]           Non-null string character data     
     StringEND           UI8           Marks end of string — always zero     
    SwfJava v0.0: how to create a String object    
        
    new SWFString ( String content );    
        


RGB Color Record

The RGB record represents a color as 24-bit red, green, and blue value.
     RGB     
     Field           Type           Comment     
     Red           UI8           Red color value     
     Green           UI8           Green color value     
     Blue           UI8           Blue color value     
    SwfJava v0.0: how to create a RGB object    
        
    new RGB ( int red, int green, int blue );    
        


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.
     RGB     
     Field           Type           Comment     
     Red           UI8           Red color value     
     Green           UI8           Green color value     
     Blue           UI8           Blue color value     
     Alpha           UI8           Transparency color value     
    SwfJava v0.0: how to create a RGBA object    
        
    new RGBA ( int red, int green, int blue, int alpha );    
        


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     


    SwfJava v0.0: how to create a Rect object    
        
    new Rect ( int xMin, int yMin, int xMax, int yMax );    
        


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.
     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     

quoting openswf.org: “The start of a MATRIX record must be byte-aligned.”
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


    SwfJava v0.0: how to create a Matrix object    
    new Matrix (    
        boolean hasScale, Fixed scaleX, Fixed scaleY,    
        boolean hasRotate, Fixed rotateSkew0, Fixed rotateSkew1,    
        int translateX, int translateY    
    );    


    XWF v0.0: how to declare a Matrix    
    <Matrix    
        [scaleX="(float)"] [scaleY="(float)"]    
        [rotateSkew0="(float)"] [rotateSkew1="(float)"]    
        translateX="(int)" translateY="(int)"    
    />    


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. Multiplication Transforms
  2. Addition Transforms
Multiplication transforms multiply the red, green, and blue components by an 8.8 fixed-point multiplication term. The fixed-point representation of 1.0 is 0x100 or 256 decimal. Therefore, the result of a multiplication operation should be divided by 256.

For any color (R,G,B) the transformed color (R', G', B') is calculated as follows:
R' = (R * RedMultTerm)    / 256
G' = (G * GreenMultTerm)  / 256
B' = (B * BlueMultTerm)   / 256
Addition transforms simply add an addition term (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))
Addition and Multiplication transforms can be combined as below. The multiplication operation is performed first.
R' = max(0, min(((R * RedMultTerm)    / 256) + RedAddTerm,    255))
G' = max(0, min(((G * GreenMultTerm)  / 256) + GreenAddTerm,  255))
B' = max(0, min(((B * BlueMultTerm)   / 256) + BlueAddTerm,   255))
     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     
quoting openswf.org: “The start of a CXForm record must be byte-aligned.”
    SwfJava v0.0: how to create a CXForm object    
    new CXForm (    
        boolean hasMult, int redMult, int greenMult, int blueMult,    
        boolean hasAdd, int redAdd, int greenAdd, int blueAdd    
    );    


    XWF v0.0: how to declare a ColorTransform    
    <ColorTransform    
        [addRed="(int)"] [addGreen="(int)"] [addBlue="(int)"]    
        [multRed="(int)"] [multGreen="(int)"] [multBlue="(int)"]    
    />    


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. Multiplication Transforms
  2. Addition Transforms
Multiplication transforms multiply the red, green, blue and alpha components by an 8.8 fixed-point value. The fixed-point representation of 1.0 is 0x100 or 256 decimal. Therefore, the result of a multiplication operation should be divided by 256.

For any color (R,G,B,A) the transformed color (R', G', B', A') is calculated as follows:
R' = (R * RedMultTerm)    / 256
G' = (G * GreenMultTerm)  / 256
B' = (B * BlueMultTerm)   / 256
A' = (A * AlphaMultTerm)  / 256
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 256.

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))
Addition and Multiplication transforms can be combined as below. The multiplication operation is performed first.
R' = max(0, min(((R * RedMultTerm)    / 256) + RedAddTerm,    255))
G' = max(0, min(((G * GreenMultTerm)  / 256) + GreenAddTerm,  255))
B' = max(0, min(((B * BlueMultTerm)   / 256) + BlueAddTerm,   255))
A' = max(0, min(((A * AlphaMultTerm)  / 256) + AlphaAddTerm,  255))
     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]           Alpha addition value     
quoting openswf.org: “The start of a CXFormWithAlpha record must be byte-aligned.”
    SwfJava v0.0: how to create a CXFormWithAlpha object    
    new CXFormWithAlpha (    
        boolean hasMult, int redMult, int greenMult, int blueMult, int alphaMult,    
        boolean hasAdd, int redAdd, int greenAdd, int blueAdd, int alphaAdd,    
    );    


    XWF v0.0: how to declare a ColorTransform    
    <ColorTransform    
        [addRed="(int)"] [addGreen="(int)"] [addBlue="(int)"] [addAlpha="(int)"]    
        [multRed="(int)"] [multGreen="(int)"] [multBlue="(int)"] [multAlpha="(int)"]    
    />    


Introduction Basic Types Display List Control Tags
Shapes (Examples Shapes) Gradients Buttons
Sprites Fonts and Text Shape Morphing Bitmap
Sounds Actions ActionScripts Reference