LazPaint LZP Format Single Image: what changed
Revision 1 to 2. 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 | 15 | `Offset Size Content`\ | |
| 16 | 16 | `------ ---- ----------------------------`\ | |
| 17 | 17 | `0 4 Width`\ | |
| 18 | 18 | `4 4 Height`\ | |
| 19 | 19 | `8 4 Length of the caption (N)`\ | |
| 20 | 20 | `12 N Caption (UTF8 encoded)`\ | |
| 21 | 21 | `12+N 4 Left bound of the content`\ | |
| 22 | 22 | `16+N 4 Top bound of the content`\ | |
| 23 | 23 | `20+N 4 Right bound of the content`\ | |
| 24 | 24 | `24+N 4 Bottom bound of the content`\ | |
| 25 | 25 | `28+N 4 Line order (0: top to bottom, 1: bottom to top)`\ | |
| 26 | 26 | `32+N 4 Number of chunks` | |
| 27 | 27 | ||
| 28 | 28 | 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 | 29 | ||
| 30 | 30 | The chunks follow directly the header. Here is the structure for one chunk: | |
| 31 | 31 | ||
| 32 | 32 | `Offset Size Content`\ | |
| 33 | 33 | `------ ---- ----------------------------`\ | |
| 34 | 34 | `0 4 Size of the compressed data in the chunk (M)`\ | |
| 35 | 35 | `4 M Compressed data using ZStream (without ZStream header)` | |
| 36 | 36 | ||
| 37 | 37 | #### RLE | |
| 38 | 38 | ||
| 39 | 39 | The header of the RLE image is as follows: | |
| 40 | 40 | ||
| 41 | 41 | `Offset Size Content`\ | |
| 42 | 42 | `------ ---- ----------------------------`\ | |
| 43 | 43 | `0 4 Width`\ | |
| 44 | 44 | `4 4 Height`\ | |
| 45 | 45 | `8 4 Length of the caption (N)`\ | |
| 46 | 46 | `12 N Caption (UTF8 encoded)`\ | |
| 47 | 47 | `12+N 1 Plane flags` | |
| 48 | 48 | ||
| 49 | 49 | The following plane flags are defined: | |
| 50 | 50 | ||
| 51 | 51 | \- \$01: the green channel is a copy of the red channel | |
| 52 | 52 | ||
| 53 | 53 | \- \$02: the blue channel is a copy of the red channel | |
| 54 | 54 | ||
| 55 | 55 | \- \$04: the blue channel is a copy of the green channel | |
| 56 | 56 | ||
| 57 | 57 | \- \$08: there is no alpha channel (the image is opaque) | |
| 58 | 58 | ||
| 59 | 59 | \- \$10: there is a palette and colors are expressed as indices in this palette | |
| 60 | 60 | ||
| 61 | 61 | There is then a variable number of RLE streams. The pixels are scanned from top to bottom and from left to right. | |
| 62 | 62 | ||
| 63 | 63 | ##### Full RLE structure for empty images | |
| 64 | 64 | ||
| 65 | 65 | If the image is completely transparent, there are no stream for color channels. | |
| 66 | 66 | ||
| 67 | 67 | `Offset Size Content`\ | |
| 68 | 68 | `------ ---- -------------------------`\ | |
| 69 | 69 | `0 13+N Header`\ | |
| 70 | 70 | `13+N ? Alpha channel stream` | |
| 71 | 71 | ||
| 72 | 72 | 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 | 73 | ||
| 74 | 74 | ##### Full RLE structure for non paletted images | |
| 75 | 75 | ||
| 76 | 76 | `Offset Size Content`\ | |
| 77 | 77 | `------ ---- -------------------------`\ | |
| 78 | 78 | `0 13+N Header`\ | |
| 79 | 79 | `13+N ? Alpha channel stream (optional)`\ | |
| 80 | 80 | `? ? Red channel stream`\ | |
| 81 | 81 | `? ? Green channel stream (optional)`\ | |
| 82 | 82 | `? ? Blue channel stream (optional)` | |
| 83 | 83 | ||
| 84 | 84 | 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 | 85 | ||
| 86 | 86 | The uncompressed stream for the red channel contains one byte for each **non-transparent** pixel. | |
| 87 | 87 | ||
| 88 | 88 | Green channel is not present if \$01 flag is used. Otherwise the uncompressed stream contains one byte for each **non-transparent** pixel. | |
| 89 | 89 | ||
| 90 | 90 | 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 | 91 | ||
| 92 | 92 | ##### Full RLE structure for paletted images | |
| 93 | 93 | ||
| 94 | 94 | `Offset Size Content`\ | |
| 95 | 95 | `------ ---- -------------------------`\ | |
| 96 | 96 | `0 13+N Header`\ | |
| 97 | 97 | `13+N ? Alpha channel stream (optional)`\ | |
| 98 | 98 | `? ? Red channel palette stream`\ | |
| 99 | 99 | `? ? Green channel palette stream (optional)`\ | |
| 100 | 100 | `? ? Blue channel palette stream (optional)`\ | |
| 101 | 101 | `? ? Indexed color stream` | |
| 102 | 102 | ||
| 103 | 103 | 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 | 104 | ||
| 105 | 105 | 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 | 106 | ||
| 107 | 107 | 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 | 108 | ||
| 109 | 109 | Green channel palette is not present if \$01 flag is used. Otherwise the stream contains the green values for the palette. | |
| 110 | 110 | ||
| 111 | 111 | Blue channel palette is not present if \$02 or \$04 flag is used. Otherwise the stream contains the blue values for the palette. | |
| 112 | 112 | ||
| 113 | 113 | The uncompressed indexed color stream contains one byte for each **non-transparent** pixel, that is the index into the palette. | |
| 114 | 114 | ||
| 115 | 115 | ##### RLE stream | |
| 116 | 116 | ||
| 117 | 117 | The structure of one RLE stream is as follows: | |
| 118 | 118 | ||
| 119 | 119 | `Offset Size Content`\ | |
| 120 | 120 | `------ ---- ----------------------------`\ | |
| 121 | 121 | `0 4 Size of the compressed data (P)`\ | |
| 122 | 122 | `4 P Compressed data` | |
| 123 | 123 | ||
| 124 | 124 | The data is a series of unpacking instructions. Each element of the instruction is a one byte value. | |
| 125 | 125 | ||
| 126 | 126 | Special instructions: | |
| 127 | 127 | ||
| 128 | 128 | `Sequence Meaning`\ | |
| 129 | 129 | `-------------------------- ---------------------------------------------------------------`\ | |
| 130 | 130 | `$C1 Reserved (the reader must stop and say the format is not supported)`\ | |
| 131 | 131 | `$C2 Reserved (the reader must stop and say the format is not supported)`\ | |
| 132 | 132 | `$E0 Ends of stream`\ | |
| 133 | 133 | `$E1 Reserved (the reader must stop and say the format is not supported)`\ | |
| 134 | 134 | `$FF The reader must ignore and continue to read` | |
| 135 | 135 | ||
| 136 | 136 | Big repetitions instructions: | |
| 137 | 137 | ||
| 138 | 138 | `Sequence Meaning`\ | |
| 139 | 139 | `-------------------------- ---------------------------------------------------------------`\ | |
| 140 | 140 | `$00 `*`Hi`*` `*`Lo`*` `*`Val`*` Repeats (`*`Hi`*`*256 + `*`Lo`*`) times the value `*`Val`*\ | |
| 141 | 141 | `$80 `*`Val`*` Repeats the value `*`Val`*` the same number of times that in the previous $00 instruction`\ | |
| 142 | 142 | `$40 `*`Count`*` `*`Val`*` Repeats (`*`Count`*`+64) times the value `*`Val`*\ | |
| 143 | 143 | `$C0 `*`Val`*` Repeats the value `*`Val`*` the same number of times that in the previous $40 instruction` | |
| 144 | 144 | ||
| 145 | 145 | Small repetitions: | |
| 146 | 146 | ||
| 147 | 147 | `Sequence Meaning`\ | |
| 148 | 148 | `-------------------------- ---------------------------------------------------------------`\ | |
| 149 | 149 | `$01..$3f `*`Val`*` Repeats `*`Val`*`, the number of times being the value of the instruction ($01..$3f)`\ | |
| 150 | 150 | `$40+n (`*`n`*` = 1..31) Uncompress `*`n`*` group of packed repetition (see below)`\ | |
| 151 | 151 | `$60+`*`n`*` (`*`n`*` = 0..15) Repeats (`*`n`*`+1) times the value 0`\ | |
| 152 | 152 | `$70+`*`n`*` (`*`n`*` = 0..15) Repeats (`*`n`*`+1) times the value 255` | |
| 153 | 153 | ||
| 154 | 154 | Dump: | |
| 155 | 155 | ||
| 156 | 156 | `Sequence Meaning`\ | |
| 157 | 157 | `-------------------------- ---------------------------------------------------------------`\ | |
| 158 | 158 | `$81 `*`Count`*` The next (`*`Count`*`+64) bytes are copied as they are`\ | |
| 159 | 159 | `$80+`*`n`*` (`*`n`*` = 2..63) The next `*`n`*` bytes are copied as they are`\ | |
| 160 | 160 | `$C0+`*`n`*` `*`Val`*` (`*`n`*` = 3..31) The next `*`(n-1)/2`*` bytes must be unpacked to yield `*`n`*` uncompressed bytes (see below)`\ | |
| 161 | 161 | ` An initial value `*`Val`*` is provided and is put as it is in the uncompressed stream.`\ | |
| 162 | 162 | `$E0+`*`n`*` (`*`n`*` = 2..30) The next `*`n/2`*` bytes must be unpacked to yield `*`n`*` uncompressed bytes (see below)`\ | |
| 163 | 163 | ` The last unpacked value with instruction $C0 and $E0 is used as an implicit initial value.`\ | |
| 164 | 164 | ` This initial value is however not put in the uncompressed stream. Its default value is $80`\ | |
| 165 | 165 | ` in the case there is no previous unpacking instruction.` | |
| 166 | 166 | ||
| 167 | 167 | ##### Simple instructions examples | |
| 168 | 168 | ||
| 169 | 169 | `07 FF 06 FE` | |
| 170 | 170 | ||
| 171 | 171 | repeats 7 times the value \$FF then 6 times the value \$FE. | |
| 172 | 172 | ||
| 173 | 173 | `63 73` | |
| 174 | 174 | ||
| 175 | 175 | repeat 4 times the value \$00 then 4 times the value \$FF. | |
| 176 | 176 | ||
| 177 | 177 | `00 01 00 23 E0` | |
| 178 | 178 | ||
| 179 | 179 | Stream containing \$100 times the value \$23. The \$E0 instruction indicates the end of the stream. | |
| 180 | 180 | ||
| 181 | 181 | `00 01 00 C0 80 FF 80 C0 80 FF` | |
| 182 | 182 | ||
| 183 | 183 | Stream containing: | |
| 184 | 184 | ||
| 185 | 185 | `- $100 times the value $C0`\ | |
| 186 | 186 | `- $100 times the value $FF`\ | |
| 187 | 187 | `- $100 times the value $C0`\ | |
| 188 | 188 | `- $100 times the value $FF`\ | |
| 189 | 189 | `- the end of the stream instruction is missing` | |
| 190 | 190 | ||
| 191 | 191 | `40 03 FF C0 00` | |
| 192 | 192 | ||
| 193 | 193 | repeats 67 times the value \$FF, then 67 times the value \$00. | |
| 194 | 194 | ||
| 195 | 195 | `83 01 02 03` | |
| 196 | 196 | ||
| 197 | 197 | writes 01 02 03 into the output stream (a total of 3 bytes). | |
| 198 | 198 | ||
| 199 | 199 | `81 03 01 02 03 04 ... 40 41 42 43` | |
| 200 | 200 | ||
| 201 | 201 | writes 01 02 03 04 ... 40 41 42 43 into the output stream (a total of 67 bytes). | |
| 202 | 202 | ||
| 203 | 203 | ##### Packed repetitions | |
| 204 | 204 | ||
| 205 | 205 | The instructions \$41..\$5F (= \$40 + *n*) represent a group of *n* packed repetition. So the structure of one instruction is: | |
| 206 | 206 | ||
| 207 | 207 | `Instruction $40+`*`n`*` `\ | |
| 208 | 208 | `Packed repetition number 1`\ | |
| 209 | 209 | `Packed repetition number 2`\ | |
| 210 | 210 | `...`\ | |
| 211 | 211 | `Packed repetition number `*`n`* | |
| 212 | 212 | ||
| 213 | 213 | One entry of packed repetition: | |
| 214 | 214 | ||
| 215 | 215 | `Offset Size Content`\ | |
| 216 | 216 | `----- ---- -----------------`\ | |
| 217 | 217 | `0 1 Packed number P of repetitions to apply`\ | |
| 218 | 218 | `1 1 First byte to repeat N1 times`\ | |
| 219 | 219 | `2 1 2nd byte to repeat N2 times`\ | |
| 220 | 220 | `3 1 3rd byte to repeat N3 times`\ | |
| 221 | 221 | `4 1 4th byte to repeat N4 times`\ | |
| 222 | - | ||
| 222 | + | ||
| 223 | 223 | `P = N1-1 + ((N2-1) `*`shl`*` 2) + ((N3-1) `*`shl`*` 4) + ((N4-1) `*`shl`*` 6)` | |
| 224 | 224 | ||
| 225 | 225 | Example: | |
| 226 | 226 | ||
| 227 | 227 | `42 FF 01 02 03 04 55 01 02 03 04`\ | |
| 228 | - | ||
| 228 | + | ||
| 229 | 229 | `is unpacked as`\ | |
| 230 | - | ||
| 230 | + | ||
| 231 | 231 | `01 01 01 01 02 02 02 02 03 03 03 03 04 04 04 04 01 01 02 02 03 03 04 04` | |
| 232 | 232 | ||
| 233 | 233 | ##### Packed dumps | |
| 234 | 234 | ||
| 235 | 235 | 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 | 236 | ||
| 237 | 237 | For example: | |
| 238 | 238 | ||
| 239 | 239 | `20 22 25`\ | |
| 240 | - | ||
| 240 | + | ||
| 241 | 241 | `can be packed as`\ | |
| 242 | - | ||
| 242 | + | ||
| 243 | 243 | `initial value $20, then offset +2, then offset +3` | |
| 244 | 244 | ||
| 245 | 245 | So it would be written in the compressed stream as: | |
| 246 | 246 | ||
| 247 | 247 | `C3 20 AB` | |
| 248 | 248 | ||
| 249 | 249 | Where \$A = 10 = 2 + 8 is the first offset and \$B = 11 = 3 + 8 is the second offset. | |
| 250 | 250 | ||
| 251 | 251 | Another example: | |
| 252 | 252 | ||
| 253 | 253 | `00 01 02 03 04 05 ... 0E 0F (a total of 16 bytes)`\ | |
| 254 | - | ||
| 254 | + | ||
| 255 | 255 | `can be packed as`\ | |
| 256 | - | ||
| 256 | + | ||
| 257 | 257 | `D0 00 99 99 99 99 99 99 99 90 (a total of 10 bytes)` | |
| 258 | 258 | ||
| 259 | 259 | Note that the initial value may be omitted by using the instructions \$E2..\$FE. | |
| 260 | 260 | ||
| 261 | 261 | --- | |
| 262 | 262 | *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 | 263 | Category: LazPaint |
