LazPaint LZP Format: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | 1 | ## LZP File format specification | |
| 2 | 2 | ||
| 3 | 3 | In this document, [[Hexadecimal|hexadecimal]] numbers follow Pascal convention, i.e. they are prefixed with a [[Dollar sign|dollar sign]]. For example \$10 means 16. As an exception, sequences of bytes are not prefixed. | |
| 4 | 4 | ||
| 5 | 5 | The default encoding for numbers is little endian. So the hexadecimal number \$12345678 would be stored as the sequence of bytes 78 56 34 12. | |
| 6 | 6 | ||
| 7 | 7 | Numbers are 32-bit signed integers, unless stated otherwise. So -1 is stored like \$FFFFFFFF. | |
| 8 | 8 | ||
| 9 | 9 | ### Global structure | |
| 10 | 10 | ||
| 11 | 11 | The file is composed of: | |
| 12 | 12 | ||
| 13 | 13 | `Offset Content`\ | |
| 14 | 14 | `------ --------------------`\ | |
| 15 | 15 | `0 File header`\ | |
| 16 | 16 | `? Thumbnail (optional)`\ | |
| 17 | 17 | `? `[[LazPaint LZP Format Single Image|Flat image]]\ | |
| 18 | 18 | `? Layer structure (optional)` | |
| 19 | 19 | ||
| 20 | 20 | Note: some old formats do not have a header and start directly with the flat image. Image readers may discard those files. However for your information, in this case, images are always stored with ZStream compression and the size of the image is deduced from the size of the flat image. | |
| 21 | 21 | ||
| 22 | 22 | ### File header | |
| 23 | 23 | ||
| 24 | 24 | `Offset Size Name Content`\ | |
| 25 | 25 | `------ ---- ------------------ ------------------------`\ | |
| 26 | 26 | `0 8 `*`magic`*` Always contains the string 'LazPaint'`\ | |
| 27 | 27 | `8 4 `*`zero1`*` Contains the number 0`\ | |
| 28 | 28 | `12 4 `*`headerSize`*` Size of this header`\ | |
| 29 | - | ||
| 29 | + | ||
| 30 | 30 | `16 4 `*`width`*` Width of the image canvas`\ | |
| 31 | 31 | `20 4 `*`height`*` Height of the image canvas`\ | |
| 32 | 32 | `24 4 `*`nbLayers`*` Number of layers`\ | |
| 33 | 33 | `28 4 `*`previewOffset`*` Offset (from the beginning of the file) of the `[[LazPaint LZP Format Single Image|flattened image]]\ | |
| 34 | - | ||
| 34 | + | ||
| 35 | 35 | `32 4 `*`zero2`*` Contains the number 0`\ | |
| 36 | 36 | `36 4 `*`compressionMode`*` Contains flags indicating the compression and the presence of a thumbnail`\ | |
| 37 | 37 | `40 4 `*`reserved1`*` Reserved for future use. Set to 0.`\ | |
| 38 | 38 | `44 4 `*`layersOffset`*` Offset (from the beginning of the file) of the layer structure` | |
| 39 | 39 | ||
| 40 | 40 | *headerSize* contains the size of this header. An image reader must read this to know which fields are available. It must be greater or equal to 48. | |
| 41 | 41 | ||
| 42 | 42 | *width* and *height* are the dimension of the canvas. There is no real limitation for these numbers however it is recommended to keep it under or equal to 8192. | |
| 43 | 43 | ||
| 44 | 44 | *nbLayers* is the number of layers. The minimum is 1. There is no real limitation however it is recommended to keep it under or equal to 99. | |
| 45 | 45 | ||
| 46 | 46 | *layersOffset* can be 0, in which case the image is flat and that there is no layer description. | |
| 47 | 47 | ||
| 48 | 48 | #### Compression | |
| 49 | 49 | ||
| 50 | 50 | *compressionMode* contains flags indicating the compression and the presence of a thumbnail. | |
| 51 | 51 | ||
| 52 | 52 | The low order byte indicates the compression of the flattened image. It is the value (*compressionMode* **and** \$ff): | |
| 53 | 53 | ||
| 54 | 54 | \- \$01 indicates ZStream compression | |
| 55 | 55 | ||
| 56 | 56 | \- \$02 indicates RLE compression | |
| 57 | 57 | ||
| 58 | 58 | \- any other value should be considered as an indication that the image reader cannot interpret the content of the file | |
| 59 | 59 | ||
| 60 | 60 | The following flags can be defined: | |
| 61 | 61 | ||
| 62 | 62 | \- \$100 indicates that there is thumbnail in PNG format; check with (*compressionMode* **and** \$100) \<\> 0 | |
| 63 | 63 | ||
| 64 | 64 | If there is a thumbnail, it is directly after the header. The reader should read *headerSize* to have the correct location. | |
| 65 | 65 | ||
| 66 | 66 | The compression of the layers is defined in the layer structure. | |
| 67 | 67 | ||
| 68 | 68 | #### Thumbnail | |
| 69 | 69 | ||
| 70 | 70 | Its presence is recommended however optional. Its presence is specified by the flag \$100 of *compressionMode*. | |
| 71 | 71 | ||
| 72 | 72 | Currently, it is a PNG file embedded directly after the header. It is provided as a way to facilitate quick preview. | |
| 73 | 73 | ||
| 74 | 74 | PNG format is widespread so it is easy to implement a browser that would display the thumbnail. To do so, check that the header contains *magic* and that the flag for the thumbnail is set in *compressionMode*. Then jump to the end of the header using *headerSize*. Then read the PNG file. | |
| 75 | 75 | ||
| 76 | 76 | There is no real limitation but the recommended size for the thumbnail is 128 along the largest dimension. The other dimension may be smaller if the image is not a square. Of course if the image is smaller than 128 then the thumbnail will be smaller than 128. | |
| 77 | 77 | ||
| 78 | 78 | ### Flat image | |
| 79 | 79 | ||
| 80 | 80 | The file always include a flattened image to make it easier for image viewers. It is in the [[LazPaint LZP Format Single Image|LZP single image format]]. | |
| 81 | 81 | ||
| 82 | 82 | If there is no layer structure, the caption of this image is the name of the unique layer. If there is a layer structure, then the caption of this image is discarded. | |
| 83 | 83 | ||
| 84 | 84 | ### Layer structure | |
| 85 | 85 | ||
| 86 | 86 | `Offset Size Content`\ | |
| 87 | 87 | `------ ---- -------------------------------------------------------`\ | |
| 88 | 88 | `0 20 Contains the string 'TBGRALayeredBitmap' followed by the bytes 1A 00`\ | |
| 89 | 89 | `20 4 Size S of the additional header after this field (must be at least 12)`\ | |
| 90 | 90 | `24 4 Number of layers N (should be identical to the number in the file header)`\ | |
| 91 | 91 | `28 4 Selected layer index. Equals to -1 if no layer is selected, otherwise positive from 0 to N-1`\ | |
| 92 | 92 | `32 4 Stack flags`\ | |
| 93 | 93 | `36 4 Canvas width (optional)`\ | |
| 94 | 94 | `40 4 Canvas height (optional)`\ | |
| 95 | 95 | `44 8 64-bit offset to the directory structure relative to the start of this header (optional)`\ | |
| 96 | 96 | `24+S First layer (bottom one)` | |
| 97 | 97 | ||
| 98 | 98 | The following flags are defined: | |
| 99 | 99 | ||
| 100 | 100 | - \$01: the layers are blended without gamma correction | |
| 101 | 101 | - \$02: the layers are compressed with RLE compression (otherwise they are compressed with ZStream compression) | |
| 102 | 102 | ||
| 103 | 103 | If the canvas size is not supplied (when the total header size is less than 44), then the canvas size is the size of the first non-empty layer bitmap. | |
| 104 | 104 | ||
| 105 | 105 | Similarly, the [[LazPaint LZP directory structure|directory structure]] offset may not to be supplied or set to zero. In this case, there is none. It is optional and only necessary for vector shapes. | |
| 106 | 106 | ||
| 107 | 107 | The total header size of the layer structure is equal to 24 + S. Then for each layer there is a layer entry. | |
| 108 | 108 | ||
| 109 | 109 | The first layer entry is the bottom most layer. Then each layer is blended over the previous ones. The last layer is on top and is not hidden by any other layer. | |
| 110 | 110 | ||
| 111 | 111 | #### Layer entry | |
| 112 | 112 | ||
| 113 | 113 | Header of the layer entry: | |
| 114 | 114 | ||
| 115 | 115 | `Offset Size Content`\ | |
| 116 | 116 | `------ ---- -------------------------------------------------------`\ | |
| 117 | 117 | `0 4 Size S1 of the optional fields that follows`\ | |
| 118 | 118 | `4 4 Layer flags (lowest bit means that it is visible)`\ | |
| 119 | 119 | `8 4 Layer blend operation`\ | |
| 120 | 120 | `12 4 Layer horizontal offset in pixels`\ | |
| 121 | 121 | `16 4 Layer vertical offset in pixels`\ | |
| 122 | 122 | `20 4 Layer ID (if used, each layer must have a different ID in a given file)`\ | |
| 123 | 123 | `24 4 Layer opacity from 0 (transparent) to 65535 (opaque)`\ | |
| 124 | 124 | `28 4 Lower bits of size S2 of image data`\ | |
| 125 | 125 | `32 4 Upper bits of size S2 (optional, forming 64-bit integer)`\ | |
| 126 | 126 | `36 16 GUID of the original used to render the layer (optional)`\ | |
| 127 | 127 | `52 24 Affine matrix of the transform applied to the original`\ | |
| 128 | 128 | `4+S1 S2 `[[LazPaint LZP Format Single Image|Image data]]\ | |
| 129 | 129 | `4+S1+S2 Next entry` | |
| 130 | 130 | ||
| 131 | 131 | All fields are optional so in theory, S1 can be set to zero and the image data can directly follow. However it is recommended to write the whole header. | |
| 132 | 132 | ||
| 133 | 133 | The default values are: visible, blend operation boTransparent, offset (0,0), undefined ID (maximum integer value), opaque, size S2 = 0, GUID = NULL. If an original is specified the affine matrix transform is mandatory. | |
| 134 | 134 | ||
| 135 | 135 | The size of the image data is a 64-bit integer, though the upper value is optional and by default zero. When the image size S2 \<\> 0, the offset to the next entry is computed using the formula 4+S1+S2. Otherwise, the data is expected to continue after the end of the image data. | |
| 136 | 136 | ||
| 137 | 137 | The GUID is stored here as binary (in little endian): one DWord, two Words and 8 bytes. | |
| 138 | 138 | ||
| 139 | 139 | The affine transform is stored as binary (in little endian): 6 single precision floats ux, uy, vx, vy, tx, ty (axes u and v and translation t). | |
| 140 | 140 | ||
| 141 | 141 | #### Blend operations | |
| 142 | 142 | ||
| 143 | 143 | - Under is the value of the channels of the layer underneath | |
| 144 | 144 | - Over is the value of the channels of the layer above | |
| 145 | 145 | - Mix is an intermediate value that is then blended over the previous layers, and that, unless specified, is computed for each RGB channel. The alpha channel is computed differently (see below table). | |
| 146 | 146 | - Final is the resulting value | |
| 147 | 147 | ||
| 148 | 148 | Note: | |
| 149 | 149 | ||
| 150 | 150 | - values (Red, Green, Blue, Alpha, Mix, Under, Over, Final) are considered to be between 0 and 255 | |
| 151 | 151 | - GE means gamma expansion. GE(x) = Power(x/255, 2.1)*65535 | |
| 152 | 152 | - GC means gamma compression. GC(x) = Power(x/65535, 1/2.1)*255 | |
| 153 | 153 | - ByteSqrt means the square root. ByteSqrt(x) = Power(X/255, 0.5)*255 | |
| 154 | 154 | - **div** stands for integer division (rounded down) | |
| 155 | 155 | - HSL is the standard HSL colorspace, without gamma correction | |
| 156 | 156 | - GSB is the corrected HSL colorspace of LazPaint. It is computed with gamma correction and the B (brightness) component correspond to the perceived lightness: | |
| 157 | 157 | ||
| 158 | 158 | `B = (306*GE(Red) + 601*GE(Green) + 117*GE(Blue))/1024`\ | |
| 159 | 159 | `B between 0 and 65535` | |
| 160 | 160 | ||
| 161 | 161 | Here is the list of operations supported in LazPaint: | |
| 162 | 162 | ||
| 163 | 163 | `Value Name Description`\ | |
| 164 | 164 | `----- ----------------- ----------------------------------------------------`\ | |
| 165 | 165 | `0 LinearBlend Blend over the previous layers without gamma correction`\ | |
| 166 | 166 | `1 Transparent Blend over the previous layers `\ | |
| 167 | 167 | ` (with gamma correction unless it is specified otherwise in the stack flags)`\ | |
| 168 | - | ||
| 168 | + | ||
| 169 | 169 | `2 Lighten Mix := Max(Over, Under)`\ | |
| 170 | 170 | `3 Screen Mix := 255 - ((255 - Under) * (255 - Over) `**`shr`**` 8)`\ | |
| 171 | 171 | `4 Additive Mix := GC(GE(Under)+GE(Over))`\ | |
| 172 | 172 | `5 LinearAdd Mix := Under + Over`\ | |
| 173 | 173 | `6 ColorDodge Mix := `**`if`**` Over = 255 `**`then`**` 255`\ | |
| 174 | 174 | ` `**`else`**` Min(255, (Under `**`shl`**` 8) `**`div`**` (255 - Over))`\ | |
| 175 | 175 | `7 Divide Mix := `**`if`**` Over = 0 `**`then`**` 255`\ | |
| 176 | 176 | ` `**`else`**` Min(255, (Under `**`shl`**` 8) `**`div`**` Over)`\ | |
| 177 | 177 | `8 NiceGlow Mix := `**`if`**` Under = 255 `**`then`**` 255`\ | |
| 178 | 178 | ` `**`else`**` Min(255, Over*Over `**`div`**` (255 - Under))`\ | |
| 179 | 179 | `9 SoftLight Mix := ((255 - Under)*Over shr 7 + Under)*Under div 255`\ | |
| 180 | 180 | `10 HardLight Mix := `**`if`**` Over <= 128 `**`then`**` Under*Over `**`shr`**` 7`\ | |
| 181 | 181 | ` `**`else`**` 255 - ((255 - Under)*(255 - Over) `**`shr`**` 7)`\ | |
| 182 | - | ||
| 182 | + | ||
| 183 | 183 | `11 Glow Mix := `**`if`**` Under = 255 `**`then`**` 255`\ | |
| 184 | 184 | ` `**`else`**` Min(255, Over*Over `**`div`**` (255 - Under))`\ | |
| 185 | 185 | `12 Reflect Mix := `**`if`**` Over = 255 `**`then`**` 255`\ | |
| 186 | 186 | ` `**`else`**` Min(255, Under*Under `**`div`**` (255 - Over))`\ | |
| 187 | 187 | `13 Overlay Mix := `**`if`**` Under <= 128 `**`then`**` Under*Over `**`shr`**` 7`\ | |
| 188 | 188 | ` `**`else`**` 255 - ((255 - Under)*(255 - Over) `**`shr`**` 7)`\ | |
| 189 | 189 | `14 DarkOverlay Mix := `**`if`**` GE(Under) <= 32768 `**`then`**` Under*Over `**`shr`**` 7`\ | |
| 190 | 190 | ` `**`else`**` GC(65535 - ((65535 - GE(Under))*(65535 - GE(Over)) `**`shr`**` 15)`\ | |
| 191 | 191 | `15 Darken Mix := Min(Under, Over)`\ | |
| 192 | 192 | `16 Multiply Mix := `**`if`**` Over >= 128 `**`then`**` Under*(Over+1) `**`shr`**` 8 `**`else`**` Under*Over `**`shr`**` 8`\ | |
| 193 | 193 | `17 ColorBurn Mix := `**`if`**` Over = 0 `**`then`**` 0`\ | |
| 194 | 194 | ` `**`else`**` Max(0, 255 - (Under `**`shl`**` 8) `**`div`**` Over)`\ | |
| 195 | - | ||
| 195 | + | ||
| 196 | 196 | `18 Difference Mix := GC(Abs(GE(Under) - GE(Over)))`\ | |
| 197 | 197 | `19 LinearDifference Mix := Abs(Under - Over)`\ | |
| 198 | 198 | `20 Exclusion Mix := GC(GE(Under) + GE(Over) - (GE(Under)*GE(Over) `**`shr`**` 15)`\ | |
| 199 | 199 | `21 LinearExclusion Mix := Under + Over - (Under*Over `**`shr`**` 7)`\ | |
| 200 | 200 | `22 Subtract Mix := GC(Max(0, GE(Under) - GE(Over)))`\ | |
| 201 | 201 | `23 LinearSubtract Mix := Max(0, Under - Over)`\ | |
| 202 | 202 | `24 SubtractInverse Mix := GC(Max(0, GE(Under) - (65535 - GE(Over))))`\ | |
| 203 | 203 | `25 LinearSubtractInv Mix := Max(0, Under - (255 - Over))`\ | |
| 204 | - | ||
| 204 | + | ||
| 205 | 205 | `26 Negation Mix := `**`if`**` GE(Under) + GE(Over) <= 65535 `**`then`**` GC(GE(Under) + GE(Over))`\ | |
| 206 | 206 | ` `**`else`**` GC(131071 - (GE(Under) + GE(Over)))`\ | |
| 207 | 207 | `27 LinearNegation Mix := `**`if`**` Under + Over <= 255 `**`then`**` Under + Over`\ | |
| 208 | 208 | ` `**`else`**` 511 - (Under + Over)`\ | |
| 209 | 209 | `28 Xor Mix := Under `**`xor`**` Over`\ | |
| 210 | - | ||
| 210 | + | ||
| 211 | 211 | `29 SvgSoftLight Mix := `**`if`**` Over <= 128 `**`then`**` Under - (((256 - 2*Over)* Under `**`shr`**` 8)*(255 - Under) `**`shr`**` 8)`\ | |
| 212 | 212 | ` `**`else`**` `**`if`**` Under <= 64 `**`then`**` Under + ((2*Over - 256) * (Under*7 - (4*Under*(4*Under + 256)*(256 - Under) `**`shr`**` 16)) `**`shr`**` 8)`\ | |
| 213 | 213 | ` `**`else`**` Under + ((2*Over - 255) * (ByteSqrt(Under)-Under) `**`shr`**` 8)`\ | |
| 214 | 214 | `30 Mask ApplyOpacity := ((2*Over.Red + 4*Over.Green + Over.Blue)*Over.Alpha + 255*7*(255-Over.Alpha)) / (255*7)`\ | |
| 215 | 215 | `31 LinearMultiplySat L := (min(Under.Red, Under.Green, Under.Blue) + max(Under.Red, Under.Green, Under.Blue)) / 2`\ | |
| 216 | 216 | ` Mix := `**`if`**` Over = 0 `**`then`**` L`\ | |
| 217 | 217 | ` `**`else`**` (Under - L)*Over/255 + L`\ | |
| 218 | 218 | `32 LinearHue Mix := HSL(Over.H, Under.S, Under.L)`\ | |
| 219 | 219 | `33 LinearColor Mix := HSL(Over.H, Over.S, Under.L)`\ | |
| 220 | 220 | `34 LinearLightness Mix := HSL(Under.H, Under.S, Over.L)`\ | |
| 221 | 221 | `35 LinearSaturation Mix := HSL(Under.H, `**`if`**` Under.S <> 0 `**`then`**` Over.S `**`else`**` 0, Under.L)` | |
| 222 | 222 | ||
| 223 | 223 | `36 CorrectedHue Mix := GSB(Over.H, Under.S, Under.L)`\ | |
| 224 | 224 | `37 CorrectedColor Mix := GSB(Over.H, Over.S, Under.L)`\ | |
| 225 | 225 | `38 CorrectedLightness Mix := GSB(Under.H, Under.S, Over.L)`\ | |
| 226 | 226 | `39 CorrectedSat Mix := GSB(Under.H, `**`if`**` Under.S <> 0 `**`then`**` Over.S `**`else`**` 0, Under.L)` | |
| 227 | 227 | ||
| 228 | 228 | For the mask, the color channels are unchanged and the ApplyOpacity value is used to multiply the destination alpha value: | |
| 229 | 229 | ||
| 230 | 230 | `Final.Alpha := Under.Alpha * ApplyOpacity / 255` | |
| 231 | 231 | ||
| 232 | 232 | For other blend modes, first the RGB channels are modified according to the underlying alpha value: | |
| 233 | 233 | ||
| 234 | 234 | `Mix := (Mix * Under.Alpha + Over * (255 - Under.Alpha)) / 255`\ | |
| 235 | 235 | `Mix.Alpha := Over.Alpha` | |
| 236 | 236 | ||
| 237 | 237 | In other words, if the layer underneath is transparent, the merged color is simply the color of the layer above. | |
| 238 | 238 | ||
| 239 | 239 | Then the Mix color is drawn over the underlying layer. If Mix.Alpha = 255 then the color is replace: | |
| 240 | 240 | ||
| 241 | 241 | ` Final = Mix` | |
| 242 | 242 | ||
| 243 | 243 | If Mix.Alpha = 0 then the color is unchanged: | |
| 244 | 244 | ||
| 245 | 245 | ` Final = Under` | |
| 246 | 246 | ||
| 247 | 247 | Otherwise, the color is blended using the regular drawing function (with or without gamma correction depending on the stack option). | |
| 248 | 248 | ||
| 249 | 249 | #### Computation of blend-over | |
| 250 | 250 | ||
| 251 | 251 | Linear blend-over: | |
| 252 | 252 | ||
| 253 | 253 | **`var`**` `\ | |
| 254 | 254 | ` a1f, a2f, a12, a12m: DWord; //unsigned`\ | |
| 255 | 255 | **`begin`**\ | |
| 256 | 256 | ` a12 := 65025 - (255 - UnderAlpha) * (255 - OverAlpha);`\ | |
| 257 | 257 | ` a1f := UnderAlpha * (255 - OverAlpha);`\ | |
| 258 | 258 | ` a2f := OverAlpha * 255;`\ | |
| 259 | 259 | ` Under := (Under * a1f + Over * a2f + (a12 `**`shr`**` 1)) `**`div`**` a12; //for each channel`\ | |
| 260 | 260 | ` UnderAlpha := (a12 + a12 `**`shr`**` 7) `**`shr`**` 8; `\ | |
| 261 | 261 | **`end`**`;` | |
| 262 | 262 | ||
| 263 | 263 | Blend-over with gamma correction: | |
| 264 | 264 | ||
| 265 | 265 | **`var`**` `\ | |
| 266 | 266 | ` a1f, a2f, a12, a12m: DWord; //unsigned`\ | |
| 267 | 267 | **`begin`**\ | |
| 268 | 268 | ` a12 := 65025 - (255 - UnderAlpha) * (255 - OverAlpha);`\ | |
| 269 | 269 | ` a1f := UnderAlpha * (255 - OverAlpha);`\ | |
| 270 | 270 | ` a2f := OverAlpha * 255;`\ | |
| 271 | 271 | ` Under := GC( (GE(Under) * a1f + GE(Over) * a2f + (a12 `**`shr`**` 1)) `**`div`**` a12 ); //for each channel`\ | |
| 272 | 272 | ` UnderAlpha := (a12 + a12 `**`shr`**` 7) `**`shr`**` 8; `\ | |
| 273 | 273 | **`end`**`;` | |
| 274 | 274 | ||
| 275 | 275 | ### Directory structure | |
| 276 | 276 | ||
| 277 | 277 | The [[LazPaint LZP directory structure|directory structure]] is optional. It can contain additional information about the layered image, in particular originals used to render the layers. A reader that does not handle vectorial information can discard it. Its offset is determined by the layer stack header. | |
| 278 | 278 | ||
| 279 | 279 | --- | |
| 280 | 280 | *From the Free Pascal wiki, page "LazPaint LZP Format", as it stood on 2021-03-01 after a change by Circular, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* | |
| 281 | 281 | Category: LazPaint |
