1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
|
"""
Portable Network Graphics (PNG) file format
Official spec: http://www.w3.org/TR/PNG
Original code contributed by Robin Munn (rmunn at pobox dot com)
(although the code has been extensively reorganized to meet Construct's
coding conventions)
"""
from construct_legacy import *
#===============================================================================
# utils
#===============================================================================
def Coord(name, field=UBInt8):
return Struct(name,
field("x"),
field("y"),
)
compression_method = Enum(UBInt8("compression_method"),
deflate = 0,
_default_ = Pass
)
#===============================================================================
# 11.2.3: PLTE - Palette
#===============================================================================
plte_info = Struct("plte_info",
Value("num_entries", lambda ctx: ctx._.length / 3),
Array(lambda ctx: ctx.num_entries,
Struct("palette_entries",
UBInt8("red"),
UBInt8("green"),
UBInt8("blue"),
),
),
)
#===============================================================================
# 11.2.4: IDAT - Image data
#===============================================================================
idat_info = OnDemand(
Field("idat_info", lambda ctx: ctx.length),
)
#===============================================================================
# 11.3.2.1: tRNS - Transparency
#===============================================================================
trns_info = Switch("trns_info", lambda ctx: ctx._.image_header.color_type,
{
"greyscale": Struct("data",
UBInt16("grey_sample")
),
"truecolor": Struct("data",
UBInt16("red_sample"),
UBInt16("blue_sample"),
UBInt16("green_sample"),
),
"indexed": Array(lambda ctx: ctx.length,
UBInt8("alpha"),
),
}
)
#===============================================================================
# 11.3.3.1: cHRM - Primary chromacities and white point
#===============================================================================
chrm_info = Struct("chrm_info",
Coord("white_point", UBInt32),
Coord("red", UBInt32),
Coord("green", UBInt32),
Coord("blue", UBInt32),
)
#===============================================================================
# 11.3.3.2: gAMA - Image gamma
#===============================================================================
gama_info = Struct("gama_info",
UBInt32("gamma"),
)
#===============================================================================
# 11.3.3.3: iCCP - Embedded ICC profile
#===============================================================================
iccp_info = Struct("iccp_info",
CString("name"),
compression_method,
Field("compressed_profile",
lambda ctx: ctx._.length - (len(ctx.name) + 2)
),
)
#===============================================================================
# 11.3.3.4: sBIT - Significant bits
#===============================================================================
sbit_info = Switch("sbit_info", lambda ctx: ctx._.image_header.color_type,
{
"greyscale": Struct("data",
UBInt8("significant_grey_bits"),
),
"truecolor": Struct("data",
UBInt8("significant_red_bits"),
UBInt8("significant_green_bits"),
UBInt8("significant_blue_bits"),
),
"indexed": Struct("data",
UBInt8("significant_red_bits"),
UBInt8("significant_green_bits"),
UBInt8("significant_blue_bits"),
),
"greywithalpha": Struct("data",
UBInt8("significant_grey_bits"),
UBInt8("significant_alpha_bits"),
),
"truewithalpha": Struct("data",
UBInt8("significant_red_bits"),
UBInt8("significant_green_bits"),
UBInt8("significant_blue_bits"),
UBInt8("significant_alpha_bits"),
),
}
)
#===============================================================================
# 11.3.3.5: sRGB - Standard RPG color space
#===============================================================================
srgb_info = Struct("srgb_info",
Enum(UBInt8("rendering_intent"),
perceptual = 0,
relative_colorimetric = 1,
saturation = 2,
absolute_colorimetric = 3,
_default_ = Pass,
),
)
#===============================================================================
# 11.3.4.3: tEXt - Textual data
#===============================================================================
text_info = Struct("text_info",
CString("keyword"),
Field("text", lambda ctx: ctx._.length - (len(ctx.keyword) + 1)),
)
#===============================================================================
# 11.3.4.4: zTXt - Compressed textual data
#===============================================================================
ztxt_info = Struct("ztxt_info",
CString("keyword"),
compression_method,
OnDemand(
Field("compressed_text",
# As with iCCP, length is chunk length, minus length of
# keyword, minus two: one byte for the null terminator,
# and one byte for the compression method.
lambda ctx: ctx._.length - (len(ctx.keyword) + 2),
),
),
)
#===============================================================================
# 11.3.4.5: iTXt - International textual data
#===============================================================================
itxt_info = Struct("itxt_info",
CString("keyword"),
UBInt8("compression_flag"),
compression_method,
CString("language_tag"),
CString("translated_keyword"),
OnDemand(
Field("text",
lambda ctx: ctx._.length - (len(ctx.keyword) +
len(ctx.language_tag) + len(ctx.translated_keyword) + 5),
),
),
)
#===============================================================================
# 11.3.5.1: bKGD - Background color
#===============================================================================
bkgd_info = Switch("bkgd_info", lambda ctx: ctx._.image_header.color_type,
{
"greyscale": Struct("data",
UBInt16("background_greyscale_value"),
Alias("grey", "background_greyscale_value"),
),
"greywithalpha": Struct("data",
UBInt16("background_greyscale_value"),
Alias("grey", "background_greyscale_value"),
),
"truecolor": Struct("data",
UBInt16("background_red_value"),
UBInt16("background_green_value"),
UBInt16("background_blue_value"),
Alias("red", "background_red_value"),
Alias("green", "background_green_value"),
Alias("blue", "background_blue_value"),
),
"truewithalpha": Struct("data",
UBInt16("background_red_value"),
UBInt16("background_green_value"),
UBInt16("background_blue_value"),
Alias("red", "background_red_value"),
Alias("green", "background_green_value"),
Alias("blue", "background_blue_value"),
),
"indexed": Struct("data",
UBInt16("background_palette_index"),
Alias("index", "background_palette_index"),
),
}
)
#===============================================================================
# 11.3.5.2: hIST - Image histogram
#===============================================================================
hist_info = Array(lambda ctx: ctx._.length / 2,
UBInt16("frequency"),
)
#===============================================================================
# 11.3.5.3: pHYs - Physical pixel dimensions
#===============================================================================
phys_info = Struct("phys_info",
UBInt32("pixels_per_unit_x"),
UBInt32("pixels_per_unit_y"),
Enum(UBInt8("unit"),
unknown = 0,
meter = 1,
_default_ = Pass
),
)
#===============================================================================
# 11.3.5.4: sPLT - Suggested palette
#===============================================================================
def splt_info_data_length(ctx):
if ctx.sample_depth == 8:
entry_size = 6
else:
entry_size = 10
return (ctx._.length - len(ctx.name) - 2) / entry_size
splt_info = Struct("data",
CString("name"),
UBInt8("sample_depth"),
Array(lambda ctx: splt_info_data_length,
IfThenElse("table", lambda ctx: ctx.sample_depth == 8,
# Sample depth 8
Struct("table",
UBInt8("red"),
UBInt8("green"),
UBInt8("blue"),
UBInt8("alpha"),
UBInt16("frequency"),
),
# Sample depth 16
Struct("table",
UBInt16("red"),
UBInt16("green"),
UBInt16("blue"),
UBInt16("alpha"),
UBInt16("frequency"),
),
),
),
)
#===============================================================================
# 11.3.6.1: tIME - Image last-modification time
#===============================================================================
time_info = Struct("data",
UBInt16("year"),
UBInt8("month"),
UBInt8("day"),
UBInt8("hour"),
UBInt8("minute"),
UBInt8("second"),
)
#===============================================================================
# chunks
#===============================================================================
default_chunk_info = OnDemand(
HexDumpAdapter(Field(None, lambda ctx: ctx.length))
)
chunk = Struct("chunk",
UBInt32("length"),
String("type", 4),
Switch("data", lambda ctx: ctx.type,
{
"PLTE" : plte_info,
"IEND" : Pass,
"IDAT" : idat_info,
"tRNS" : trns_info,
"cHRM" : chrm_info,
"gAMA" : gama_info,
"iCCP" : iccp_info,
"sBIT" : sbit_info,
"sRGB" : srgb_info,
"tEXt" : text_info,
"zTXt" : ztxt_info,
"iTXt" : itxt_info,
"bKGD" : bkgd_info,
"hIST" : hist_info,
"pHYs" : phys_info,
"sPLT" : splt_info,
"tIME" : time_info,
},
default = default_chunk_info,
),
UBInt32("crc"),
)
image_header_chunk = Struct("image_header",
UBInt32("length"),
Magic(b"IHDR"),
UBInt32("width"),
UBInt32("height"),
UBInt8("bit_depth"),
Enum(UBInt8("color_type"),
greyscale = 0,
truecolor = 2,
indexed = 3,
greywithalpha = 4,
truewithalpha = 6,
_default_ = Pass,
),
compression_method,
Enum(UBInt8("filter_method"),
# "adaptive filtering with five basic filter types"
adaptive5 = 0,
_default_ = Pass,
),
Enum(UBInt8("interlace_method"),
none = 0,
adam7 = 1,
_default_ = Pass,
),
UBInt32("crc"),
)
#===============================================================================
# the complete PNG file
#===============================================================================
png_file = Struct("png",
Magic(b"\x89PNG\r\n\x1a\n"),
image_header_chunk,
Rename("chunks", GreedyRange(chunk)),
)
|