LazPaint LZP Format Single Image: what changed
Revision 2 to 3. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | 1 | This description applies for both the flat image and for the images in the layer structure of [[LazPaint LZP Format|LZP file format]]. | |
| 2 | 2 | ||
| 3 | 3 | There are two possible compressions. Both are lossless i.e. there is no loss of quality. | |
| 4 | 4 | ||
| 5 | 5 | If a file is saved in this format without any header, it uses ZStream compression. But in general LazPaint uses RLE compression here. | |
| 6 | 6 | ||
| 7 | 7 | #### ZStream | |
| 8 | 8 | ||
| 9 | 9 | The image has a defined width and height. There is a rectangular area for its content. Pixels that are outside of this area are transparent (all channels equal to zero). | |
| 10 | 10 | ||
| 11 | 11 | The content is compressed in any number of chunks. The uncompressed data must then be concatenated regardless of where scanlines start or end. | |
| 12 | 12 | ||
| 13 | 13 | The header of the ZStream image is as follows: | |
| 14 | 14 | ||
| 15 | - | ||
| 16 | - | ||
| 17 | - | ||
| 18 | - | ||
| 19 | - | ||
| 20 | - | ||
| 21 | - | ||
| 22 | - | ||
| 23 | - | ||
| 24 | - | ||
| 25 | - | ||
| 26 | - | ||
| 15 | + | ```text | |
| 16 | + | Offset Size Content | |
| 17 | + | ------ ---- ---------------------------- | |
| 18 | + | 0 4 Width | |
| 19 | + | 4 4 Height | |
| 20 | + | 8 4 Length of the caption (N) | |
| 21 | + | 12 N Caption (UTF8 encoded) | |
| 22 | + | 12+N 4 Left bound of the content | |
| 23 | + | 16+N 4 Top bound of the content | |
| 24 | + | 20+N 4 Right bound of the content | |
| 25 | + | 24+N 4 Bottom bound of the content | |
| 26 | + | 28+N 4 Line order (0: top to bottom, 1: bottom to top) | |
| 27 | + | 32+N 4 Number of chunks | |
| 28 | + | ``` | |
| 27 | 29 | ||
| 28 | 30 | The content contains the lines in the rectangular area, following the specified line order. Each line contains all pixels in BGRA 32-bit format, i.e. 1 byte for each channel and the order of the channels being blue, green, red, alpha. | |
| 29 | 31 | ||
| 30 | 32 | The chunks follow directly the header. Here is the structure for one chunk: | |
| 31 | 33 | ||
| 32 | - | ||
| 33 | - | ||
| 34 | - | ||
| 35 | - | ||
| 34 | + | ```text | |
| 35 | + | Offset Size Content | |
| 36 | + | ------ ---- ---------------------------- | |
| 37 | + | 0 4 Size of the compressed data in the chunk (M) | |
| 38 | + | 4 M Compressed data using ZStream (without ZStream header) | |
| 39 | + | ``` | |
| 36 | 40 | ||
| 37 | 41 | #### RLE | |
| 38 | 42 | ||
| 39 | 43 | The header of the RLE image is as follows: | |
| 40 | 44 | ||
| 41 | - | ||
| 42 | - | ||
| 43 | - | ||
| 44 | - | ||
| 45 | - | ||
| 46 | - | ||
| 47 | - | ||
| 45 | + | ```text | |
| 46 | + | Offset Size Content | |
| 47 | + | ------ ---- ---------------------------- | |
| 48 | + | 0 4 Width | |
| 49 | + | 4 4 Height | |
| 50 | + | 8 4 Length of the caption (N) | |
| 51 | + | 12 N Caption (UTF8 encoded) | |
| 52 | + | 12+N 1 Plane flags | |
| 53 | + | ``` | |
| 48 | 54 | ||
| 49 | 55 | The following plane flags are defined: | |
| 50 | 56 | ||
| 51 | 57 | \- \$01: the green channel is a copy of the red channel | |
| 52 | 58 | ||
| 53 | 59 | \- \$02: the blue channel is a copy of the red channel | |
| 54 | 60 | ||
| 55 | 61 | \- \$04: the blue channel is a copy of the green channel | |
| 56 | 62 | ||
| 57 | 63 | \- \$08: there is no alpha channel (the image is opaque) | |
| 58 | 64 | ||
| 59 | 65 | \- \$10: there is a palette and colors are expressed as indices in this palette | |
| 60 | 66 | ||
| 61 | 67 | There is then a variable number of RLE streams. The pixels are scanned from top to bottom and from left to right. | |
| 62 | 68 | ||
| 63 | 69 | ##### Full RLE structure for empty images | |
| 64 | 70 | ||
| 65 | 71 | If the image is completely transparent, there are no stream for color channels. | |
| 66 | 72 | ||
| 67 | - | ||
| 68 | - | ||
| 69 | - | ||
| 70 | - | ||
| 73 | + | ```text | |
| 74 | + | Offset Size Content | |
| 75 | + | ------ ---- ------------------------- | |
| 76 | + | 0 13+N Header | |
| 77 | + | 13+N ? Alpha channel stream | |
| 78 | + | ``` | |
| 71 | 79 | ||
| 72 | 80 | The uncompressed stream for the alpha channel contains one byte for each pixel of the canvas. 0 means the pixel is transparent and 255 means opaque. Because the image is empty, it contains only zeros. | |
| 73 | 81 | ||
| 74 | 82 | ##### Full RLE structure for non paletted images | |
| 75 | 83 | ||
| 76 | - | ||
| 77 | - | ||
| 78 | - | ||
| 79 | - | ||
| 80 | - | ||
| 81 | - | ||
| 82 | - | ||
| 84 | + | ```text | |
| 85 | + | Offset Size Content | |
| 86 | + | ------ ---- ------------------------- | |
| 87 | + | 0 13+N Header | |
| 88 | + | 13+N ? Alpha channel stream (optional) | |
| 89 | + | ? ? Red channel stream | |
| 90 | + | ? ? Green channel stream (optional) | |
| 91 | + | ? ? Blue channel stream (optional) | |
| 92 | + | ``` | |
| 83 | 93 | ||
| 84 | 94 | Alpha channel is not present if \$08 flag is used. Otherwise the uncompressed stream contains one byte for each pixel of the canvas. 0 means the pixel is transparent and 255 means opaque. | |
| 85 | 95 | ||
| 86 | 96 | The uncompressed stream for the red channel contains one byte for each **non-transparent** pixel. | |
| 87 | 97 | ||
| 88 | 98 | Green channel is not present if \$01 flag is used. Otherwise the uncompressed stream contains one byte for each **non-transparent** pixel. | |
| 89 | 99 | ||
| 90 | 100 | Blue channel is not present if \$02 or \$04 flag is used. Otherwise the uncompressed stream contains one byte for each **non-transparent** pixel. | |
| 91 | 101 | ||
| 92 | 102 | ##### Full RLE structure for paletted images | |
| 93 | 103 | ||
| 94 | - | ||
| 95 | - | ||
| 96 | - | ||
| 97 | - | ||
| 98 | - | ||
| 99 | - | ||
| 100 | - | ||
| 101 | - | ||
| 104 | + | ```text | |
| 105 | + | Offset Size Content | |
| 106 | + | ------ ---- ------------------------- | |
| 107 | + | 0 13+N Header | |
| 108 | + | 13+N ? Alpha channel stream (optional) | |
| 109 | + | ? ? Red channel palette stream | |
| 110 | + | ? ? Green channel palette stream (optional) | |
| 111 | + | ? ? Blue channel palette stream (optional) | |
| 112 | + | ? ? Indexed color stream | |
| 113 | + | ``` | |
| 102 | 114 | ||
| 103 | 115 | Alpha channel is not present if \$08 flag is used. Otherwise the uncompressed stream contains one byte for each pixel of the canvas. 0 means the pixel is transparent and 255 means opaque. | |
| 104 | 116 | ||
| 105 | 117 | Each channel palette stream contains a maximum of 256 uncompressed bytes. The streams may be shorter and in that case, the rest is padded with zeros. The position in the uncompressed stream is the color index. | |
| 106 | 118 | ||
| 107 | 119 | The uncompressed stream for the red channel contains one byte for each entry of the palette. It is padded with zeros to make 256 bytes. | |
| 108 | 120 | ||
| 109 | 121 | Green channel palette is not present if \$01 flag is used. Otherwise the stream contains the green values for the palette. | |
| 110 | 122 | ||
| 111 | 123 | Blue channel palette is not present if \$02 or \$04 flag is used. Otherwise the stream contains the blue values for the palette. | |
| 112 | 124 | ||
| 113 | 125 | The uncompressed indexed color stream contains one byte for each **non-transparent** pixel, that is the index into the palette. | |
| 114 | 126 | ||
| 115 | 127 | ##### RLE stream | |
| 116 | 128 | ||
| 117 | 129 | The structure of one RLE stream is as follows: | |
| 118 | 130 | ||
| 119 | - | ||
| 120 | - | ||
| 121 | - | ||
| 122 | - | ||
| 131 | + | ```text | |
| 132 | + | Offset Size Content | |
| 133 | + | ------ ---- ---------------------------- | |
| 134 | + | 0 4 Size of the compressed data (P) | |
| 135 | + | 4 P Compressed data | |
| 136 | + | ``` | |
| 123 | 137 | ||
| 124 | 138 | The data is a series of unpacking instructions. Each element of the instruction is a one byte value. | |
| 125 | 139 | ||
| 126 | 140 | Special instructions: | |
| 127 | 141 | ||
| 128 | - | ||
| 129 | - | ||
| 130 | - | ||
| 131 | - | ||
| 132 | - | ||
| 133 | - | ||
| 134 | - | ||
| 142 | + | ```text | |
| 143 | + | Sequence Meaning | |
| 144 | + | -------------------------- --------------------------------------------------------------- | |
| 145 | + | $C1 Reserved (the reader must stop and say the format is not supported) | |
| 146 | + | $C2 Reserved (the reader must stop and say the format is not supported) | |
| 147 | + | $E0 Ends of stream | |
| 148 | + | $E1 Reserved (the reader must stop and say the format is not supported) | |
| 149 | + | $FF The reader must ignore and continue to read | |
| 150 | + | ``` | |
| 135 | 151 | ||
| 136 | 152 | Big repetitions instructions: | |
| 137 | 153 | ||
| 138 | - | ||
| 139 | - | ||
| 140 | - | ||
| 141 | - | ||
| 142 | - | ||
| 143 | - | ||
| 154 | + | ```text | |
| 155 | + | Sequence Meaning | |
| 156 | + | -------------------------- --------------------------------------------------------------- | |
| 157 | + | $00 Hi Lo Val Repeats (Hi*256 + Lo) times the value Val | |
| 158 | + | $80 Val Repeats the value Val the same number of times that in the previous $00 instruction | |
| 159 | + | $40 Count Val Repeats (Count+64) times the value Val | |
| 160 | + | $C0 Val Repeats the value Val the same number of times that in the previous $40 instruction | |
| 161 | + | ``` | |
| 144 | 162 | ||
| 145 | 163 | Small repetitions: | |
| 146 | 164 | ||
| 147 | - | ||
| 148 | - | ||
| 149 | - | ||
| 150 | - | ||
| 151 | - | ||
| 152 | - | ||
| 165 | + | ```text | |
| 166 | + | Sequence Meaning | |
| 167 | + | -------------------------- --------------------------------------------------------------- | |
| 168 | + | $01..$3f Val Repeats Val, the number of times being the value of the instruction ($01..$3f) | |
| 169 | + | $40+n (n = 1..31) Uncompress n group of packed repetition (see below) | |
| 170 | + | $60+n (n = 0..15) Repeats (n+1) times the value 0 | |
| 171 | + | $70+n (n = 0..15) Repeats (n+1) times the value 255 | |
| 172 | + | ``` | |
| 153 | 173 | ||
| 154 | 174 | Dump: | |
| 155 | 175 | ||
| 156 | - | ||
| 157 | - | ||
| 158 | - | ||
| 159 | - | ||
| 160 | - | ||
| 161 | - | ||
| 162 | - | ||
| 163 | - | ||
| 164 | - | ||
| 165 | - | ||
| 176 | + | ```text | |
| 177 | + | Sequence Meaning | |
| 178 | + | -------------------------- --------------------------------------------------------------- | |
| 179 | + | $81 Count The next (Count+64) bytes are copied as they are | |
| 180 | + | $80+n (n = 2..63) The next n bytes are copied as they are | |
| 181 | + | $C0+n Val (n = 3..31) The next (n-1)/2 bytes must be unpacked to yield n uncompressed bytes (see below) | |
| 182 | + | An initial value Val is provided and is put as it is in the uncompressed stream. | |
| 183 | + | $E0+n (n = 2..30) The next n/2 bytes must be unpacked to yield n uncompressed bytes (see below) | |
| 184 | + | The last unpacked value with instruction $C0 and $E0 is used as an implicit initial value. | |
| 185 | + | This initial value is however not put in the uncompressed stream. Its default value is $80 | |
| 186 | + | in the case there is no previous unpacking instruction. | |
| 187 | + | ``` | |
| 166 | 188 | ||
| 167 | 189 | ##### Simple instructions examples | |
| 168 | 190 | ||
| 169 | - | ||
| 191 | + | ```text | |
| 192 | + | 07 FF 06 FE | |
| 193 | + | ``` | |
| 170 | 194 | ||
| 171 | 195 | repeats 7 times the value \$FF then 6 times the value \$FE. | |
| 172 | 196 | ||
| 173 | - | ||
| 197 | + | ```text | |
| 198 | + | 63 73 | |
| 199 | + | ``` | |
| 174 | 200 | ||
| 175 | 201 | repeat 4 times the value \$00 then 4 times the value \$FF. | |
| 176 | 202 | ||
| 177 | - | ||
| 203 | + | ```text | |
| 204 | + | 00 01 00 23 E0 | |
| 205 | + | ``` | |
| 178 | 206 | ||
| 179 | 207 | Stream containing \$100 times the value \$23. The \$E0 instruction indicates the end of the stream. | |
| 180 | 208 | ||
| 181 | - | ||
| 209 | + | ```text | |
| 210 | + | 00 01 00 C0 80 FF 80 C0 80 FF | |
| 211 | + | ``` | |
| 182 | 212 | ||
| 183 | 213 | Stream containing: | |
| 184 | 214 | ||
| 185 | - | ||
| 186 | - | ||
| 187 | - | ||
| 188 | - | ||
| 189 | - | ||
| 215 | + | ``` pascal | |
| 216 | + | - $100 times the value $C0 | |
| 217 | + | - $100 times the value $FF | |
| 218 | + | - $100 times the value $C0 | |
| 219 | + | - $100 times the value $FF | |
| 220 | + | - the end of the stream instruction is missing | |
| 221 | + | ``` | |
| 190 | 222 | ||
| 191 | - | ||
| 223 | + | ```text | |
| 224 | + | 40 03 FF C0 00 | |
| 225 | + | ``` | |
| 192 | 226 | ||
| 193 | 227 | repeats 67 times the value \$FF, then 67 times the value \$00. | |
| 194 | 228 | ||
| 195 | - | ||
| 229 | + | ```text | |
| 230 | + | 83 01 02 03 | |
| 231 | + | ``` | |
| 196 | 232 | ||
| 197 | 233 | writes 01 02 03 into the output stream (a total of 3 bytes). | |
| 198 | 234 | ||
| 199 | - | ||
| 235 | + | ```text | |
| 236 | + | 81 03 01 02 03 04 ... 40 41 42 43 | |
| 237 | + | ``` | |
| 200 | 238 | ||
| 201 | 239 | writes 01 02 03 04 ... 40 41 42 43 into the output stream (a total of 67 bytes). | |
| 202 | 240 | ||
| 203 | 241 | ##### Packed repetitions | |
| 204 | 242 | ||
| 205 | 243 | The instructions \$41..\$5F (= \$40 + *n*) represent a group of *n* packed repetition. So the structure of one instruction is: | |
| 206 | 244 | ||
| 207 | - | ||
| 208 | - | ||
| 209 | - | ||
| 210 | - | ||
| 211 | - | ||
| 245 | + | ```text | |
| 246 | + | Instruction $40+n | |
| 247 | + | Packed repetition number 1 | |
| 248 | + | Packed repetition number 2 | |
| 249 | + | ... | |
| 250 | + | Packed repetition number n | |
| 251 | + | ``` | |
| 212 | 252 | ||
| 213 | 253 | One entry of packed repetition: | |
| 214 | 254 | ||
| 215 | - | ||
| 216 | - | ||
| 217 | - | ||
| 218 | - | ||
| 219 | - | ||
| 220 | - | ||
| 221 | - | ||
| 255 | + | ```text | |
| 256 | + | Offset Size Content | |
| 257 | + | ----- ---- ----------------- | |
| 258 | + | 0 1 Packed number P of repetitions to apply | |
| 259 | + | 1 1 First byte to repeat N1 times | |
| 260 | + | 2 1 2nd byte to repeat N2 times | |
| 261 | + | 3 1 3rd byte to repeat N3 times | |
| 262 | + | 4 1 4th byte to repeat N4 times | |
| 222 | 263 | ||
| 223 | - | ||
| 264 | + | P = N1-1 + ((N2-1) shl 2) + ((N3-1) shl 4) + ((N4-1) shl 6) | |
| 265 | + | ``` | |
| 224 | 266 | ||
| 225 | 267 | Example: | |
| 226 | 268 | ||
| 227 | - | ||
| 269 | + | ```text | |
| 270 | + | 42 FF 01 02 03 04 55 01 02 03 04 | |
| 228 | 271 | ||
| 229 | - | ||
| 272 | + | is unpacked as | |
| 230 | 273 | ||
| 231 | - | ||
| 274 | + | 01 01 01 01 02 02 02 02 03 03 03 03 04 04 04 04 01 01 02 02 03 03 04 04 | |
| 275 | + | ``` | |
| 232 | 276 | ||
| 233 | 277 | ##### Packed dumps | |
| 234 | 278 | ||
| 235 | 279 | In a packed dumps, it is the values themselves that are packed. To do that, an initial value is defined. By default it is \$80. Then each subsequent value is computed by adding an offset from -7 to +7. This offset is stored as a number between 1 and 15 (by adding 8 to it). | |
| 236 | 280 | ||
| 237 | 281 | For example: | |
| 238 | 282 | ||
| 239 | - | ||
| 283 | + | ```text | |
| 284 | + | 20 22 25 | |
| 240 | 285 | ||
| 241 | - | ||
| 286 | + | can be packed as | |
| 242 | 287 | ||
| 243 | - | ||
| 288 | + | initial value $20, then offset +2, then offset +3 | |
| 289 | + | ``` | |
| 244 | 290 | ||
| 245 | 291 | So it would be written in the compressed stream as: | |
| 246 | 292 | ||
| 247 | - | ||
| 293 | + | ```text | |
| 294 | + | C3 20 AB | |
| 295 | + | ``` | |
| 248 | 296 | ||
| 249 | 297 | Where \$A = 10 = 2 + 8 is the first offset and \$B = 11 = 3 + 8 is the second offset. | |
| 250 | 298 | ||
| 251 | 299 | Another example: | |
| 252 | 300 | ||
| 253 | - | ||
| 301 | + | ```text | |
| 302 | + | 00 01 02 03 04 05 ... 0E 0F (a total of 16 bytes) | |
| 254 | 303 | ||
| 255 | - | ||
| 304 | + | can be packed as | |
| 256 | 305 | ||
| 257 | - | ||
| 306 | + | D0 00 99 99 99 99 99 99 99 90 (a total of 10 bytes) | |
| 307 | + | ``` | |
| 258 | 308 | ||
| 259 | 309 | Note that the initial value may be omitted by using the instructions \$E2..\$FE. | |
| 260 | 310 | ||
| 261 | 311 | --- | |
| 262 | 312 | *From the Free Pascal wiki, page "LazPaint LZP Format Single Image", as it stood on 2020-12-04 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).* | |
| 263 | 313 | Category: LazPaint |
