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