OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 |
| 3 # $URL$ |
| 4 # $Rev$ |
| 5 |
| 6 # png.py - PNG encoder/decoder in pure Python |
| 7 # |
| 8 # Copyright (C) 2006 Johann C. Rocholl <johann@browsershots.org> |
| 9 # Portions Copyright (C) 2009 David Jones <drj@pobox.com> |
| 10 # And probably portions Copyright (C) 2006 Nicko van Someren <nicko@nicko.org> |
| 11 # |
| 12 # Original concept by Johann C. Rocholl. |
| 13 # |
| 14 # LICENSE (The MIT License) |
| 15 # |
| 16 # Permission is hereby granted, free of charge, to any person |
| 17 # obtaining a copy of this software and associated documentation files |
| 18 # (the "Software"), to deal in the Software without restriction, |
| 19 # including without limitation the rights to use, copy, modify, merge, |
| 20 # publish, distribute, sublicense, and/or sell copies of the Software, |
| 21 # and to permit persons to whom the Software is furnished to do so, |
| 22 # subject to the following conditions: |
| 23 # |
| 24 # The above copyright notice and this permission notice shall be |
| 25 # included in all copies or substantial portions of the Software. |
| 26 # |
| 27 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 28 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 29 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 30 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 31 # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 32 # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 33 # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 34 # SOFTWARE. |
| 35 # |
| 36 # Changelog (recent first): |
| 37 # 2009-03-11 David: interlaced bit depth < 8 (writing). |
| 38 # 2009-03-10 David: interlaced bit depth < 8 (reading). |
| 39 # 2009-03-04 David: Flat and Boxed pixel formats. |
| 40 # 2009-02-26 David: Palette support (writing). |
| 41 # 2009-02-23 David: Bit-depths < 8; better PNM support. |
| 42 # 2006-06-17 Nicko: Reworked into a class, faster interlacing. |
| 43 # 2006-06-17 Johann: Very simple prototype PNG decoder. |
| 44 # 2006-06-17 Nicko: Test suite with various image generators. |
| 45 # 2006-06-17 Nicko: Alpha-channel, grey-scale, 16-bit/plane support. |
| 46 # 2006-06-15 Johann: Scanline iterator interface for large input files. |
| 47 # 2006-06-09 Johann: Very simple prototype PNG encoder. |
| 48 |
| 49 # Incorporated into Bangai-O Development Tools by drj on 2009-02-11 from |
| 50 # http://trac.browsershots.org/browser/trunk/pypng/lib/png.py?rev=2885 |
| 51 |
| 52 # Incorporated into pypng by drj on 2009-03-12 from |
| 53 # //depot/prj/bangaio/master/code/png.py#67 |
| 54 |
| 55 |
| 56 """ |
| 57 Pure Python PNG Reader/Writer |
| 58 |
| 59 This Python module implements support for PNG images (see PNG |
| 60 specification at http://www.w3.org/TR/2003/REC-PNG-20031110/ ). It reads |
| 61 and writes PNG files with all allowable bit depths (1/2/4/8/16/24/32/48/64 |
| 62 bits per pixel) and colour combinations: greyscale (1/2/4/8/16 bit); RGB, |
| 63 RGBA, LA (greyscale with alpha) with 8/16 bits per channel; colour mapped |
| 64 images (1/2/4/8 bit). Adam7 interlacing is supported for reading and |
| 65 writing. A number of optional chunks can be specified (when writing) |
| 66 and understood (when reading): ``tRNS``, ``bKGD``, ``gAMA``. |
| 67 |
| 68 For help, type ``import png; help(png)`` in your python interpreter. |
| 69 |
| 70 A good place to start is the :class:`Reader` and :class:`Writer` classes. |
| 71 |
| 72 Requires Python 2.3. Limited support is available for Python 2.2, but |
| 73 not everything works. Best with Python 2.4 and higher. Installation is |
| 74 trivial, but see the ``README.txt`` file (with the source distribution) |
| 75 for details. |
| 76 |
| 77 This file can also be used as a command-line utility to convert |
| 78 `Netpbm <http://netpbm.sourceforge.net/>`_ PNM files to PNG, and the reverse con
version from PNG to |
| 79 PNM. The interface is similar to that of the ``pnmtopng`` program from |
| 80 Netpbm. Type ``python png.py --help`` at the shell prompt |
| 81 for usage and a list of options. |
| 82 |
| 83 A note on spelling and terminology |
| 84 ---------------------------------- |
| 85 |
| 86 Generally British English spelling is used in the documentation. So |
| 87 that's "greyscale" and "colour". This not only matches the author's |
| 88 native language, it's also used by the PNG specification. |
| 89 |
| 90 The major colour models supported by PNG (and hence by PyPNG) are: |
| 91 greyscale, RGB, greyscale--alpha, RGB--alpha. These are sometimes |
| 92 referred to using the abbreviations: L, RGB, LA, RGBA. In this case |
| 93 each letter abbreviates a single channel: *L* is for Luminance or Luma or |
| 94 Lightness which is the channel used in greyscale images; *R*, *G*, *B* stand |
| 95 for Red, Green, Blue, the components of a colour image; *A* stands for |
| 96 Alpha, the opacity channel (used for transparency effects, but higher |
| 97 values are more opaque, so it makes sense to call it opacity). |
| 98 |
| 99 A note on formats |
| 100 ----------------- |
| 101 |
| 102 When getting pixel data out of this module (reading) and presenting |
| 103 data to this module (writing) there are a number of ways the data could |
| 104 be represented as a Python value. Generally this module uses one of |
| 105 three formats called "flat row flat pixel", "boxed row flat pixel", and |
| 106 "boxed row boxed pixel". Basically the concern is whether each pixel |
| 107 and each row comes in its own little tuple (box), or not. |
| 108 |
| 109 Consider an image that is 3 pixels wide by 2 pixels high, and each pixel |
| 110 has RGB components: |
| 111 |
| 112 Boxed row flat pixel:: |
| 113 |
| 114 list([R,G,B, R,G,B, R,G,B], |
| 115 [R,G,B, R,G,B, R,G,B]) |
| 116 |
| 117 Each row appears as its own list, but the pixels are flattened so that |
| 118 three values for one pixel simply follow the three values for the previous |
| 119 pixel. This is the most common format used, because it provides a good |
| 120 compromise between space and convenience. PyPNG regards itself as |
| 121 at liberty to replace any sequence type with any sufficiently compatible |
| 122 other sequence type; in practice each row is an array (from the array |
| 123 module), and the outer list is sometimes an iterator rather than an |
| 124 explicit list (so that streaming is possible). |
| 125 |
| 126 Flat row flat pixel:: |
| 127 |
| 128 [R,G,B, R,G,B, R,G,B, |
| 129 R,G,B, R,G,B, R,G,B] |
| 130 |
| 131 The entire image is one single giant sequence of colour values. |
| 132 Generally an array will be used (to save space), not a list. |
| 133 |
| 134 Boxed row boxed pixel:: |
| 135 |
| 136 list([ (R,G,B), (R,G,B), (R,G,B) ], |
| 137 [ (R,G,B), (R,G,B), (R,G,B) ]) |
| 138 |
| 139 Each row appears in its own list, but each pixel also appears in its own |
| 140 tuple. A serious memory burn in Python. |
| 141 |
| 142 In all cases the top row comes first, and for each row the pixels are |
| 143 ordered from left-to-right. Within a pixel the values appear in the |
| 144 order, R-G-B-A (or L-A for greyscale--alpha). |
| 145 |
| 146 There is a fourth format, mentioned because it is used internally, |
| 147 is close to what lies inside a PNG file itself, and has some support |
| 148 from the public API. This format is called packed. When packed, |
| 149 each row is a sequence of bytes (integers from 0 to 255), just as |
| 150 it is before PNG scanline filtering is applied. When the bit depth |
| 151 is 8 this is essentially the same as boxed row flat pixel; when the |
| 152 bit depth is less than 8, several pixels are packed into each byte; |
| 153 when the bit depth is 16 (the only value more than 8 that is supported |
| 154 by the PNG image format) each pixel value is decomposed into 2 bytes |
| 155 (and `packed` is a misnomer). This format is used by the |
| 156 :meth:`Writer.write_packed` method. It isn't usually a convenient |
| 157 format, but may be just right if the source data for the PNG image |
| 158 comes from something that uses a similar format (for example, 1-bit |
| 159 BMPs, or another PNG file). |
| 160 |
| 161 And now, my famous members |
| 162 -------------------------- |
| 163 """ |
| 164 |
| 165 # http://www.python.org/doc/2.2.3/whatsnew/node5.html |
| 166 from __future__ import generators |
| 167 |
| 168 __version__ = "$URL$ $Rev$" |
| 169 |
| 170 from array import array |
| 171 try: # See :pyver:old |
| 172 import itertools |
| 173 except: |
| 174 pass |
| 175 import math |
| 176 # http://www.python.org/doc/2.4.4/lib/module-operator.html |
| 177 import operator |
| 178 import struct |
| 179 import sys |
| 180 import zlib |
| 181 # http://www.python.org/doc/2.4.4/lib/module-warnings.html |
| 182 import warnings |
| 183 |
| 184 |
| 185 __all__ = ['Image', 'Reader', 'Writer', 'write_chunks', 'from_array'] |
| 186 |
| 187 |
| 188 # The PNG signature. |
| 189 # http://www.w3.org/TR/PNG/#5PNG-file-signature |
| 190 _signature = struct.pack('8B', 137, 80, 78, 71, 13, 10, 26, 10) |
| 191 |
| 192 _adam7 = ((0, 0, 8, 8), |
| 193 (4, 0, 8, 8), |
| 194 (0, 4, 4, 8), |
| 195 (2, 0, 4, 4), |
| 196 (0, 2, 2, 4), |
| 197 (1, 0, 2, 2), |
| 198 (0, 1, 1, 2)) |
| 199 |
| 200 def group(s, n): |
| 201 # See |
| 202 # http://www.python.org/doc/2.6/library/functions.html#zip |
| 203 return zip(*[iter(s)]*n) |
| 204 |
| 205 def isarray(x): |
| 206 """Same as ``isinstance(x, array)`` except on Python 2.2, where it |
| 207 always returns ``False``. This helps PyPNG work on Python 2.2. |
| 208 """ |
| 209 |
| 210 try: |
| 211 return isinstance(x, array) |
| 212 except: |
| 213 return False |
| 214 |
| 215 try: # see :pyver:old |
| 216 array.tostring |
| 217 except: |
| 218 def tostring(row): |
| 219 l = len(row) |
| 220 return struct.pack('%dB' % l, *row) |
| 221 else: |
| 222 def tostring(row): |
| 223 """Convert row of bytes to string. Expects `row` to be an |
| 224 ``array``. |
| 225 """ |
| 226 return row.tostring() |
| 227 |
| 228 # Conditionally convert to bytes. Works on Python 2 and Python 3. |
| 229 try: |
| 230 bytes('', 'ascii') |
| 231 def strtobytes(x): return bytes(x, 'iso8859-1') |
| 232 def bytestostr(x): return str(x, 'iso8859-1') |
| 233 except: |
| 234 strtobytes = str |
| 235 bytestostr = str |
| 236 |
| 237 def interleave_planes(ipixels, apixels, ipsize, apsize): |
| 238 """ |
| 239 Interleave (colour) planes, e.g. RGB + A = RGBA. |
| 240 |
| 241 Return an array of pixels consisting of the `ipsize` elements of data |
| 242 from each pixel in `ipixels` followed by the `apsize` elements of data |
| 243 from each pixel in `apixels`. Conventionally `ipixels` and |
| 244 `apixels` are byte arrays so the sizes are bytes, but it actually |
| 245 works with any arrays of the same type. The returned array is the |
| 246 same type as the input arrays which should be the same type as each other. |
| 247 """ |
| 248 |
| 249 itotal = len(ipixels) |
| 250 atotal = len(apixels) |
| 251 newtotal = itotal + atotal |
| 252 newpsize = ipsize + apsize |
| 253 # Set up the output buffer |
| 254 # See http://www.python.org/doc/2.4.4/lib/module-array.html#l2h-1356 |
| 255 out = array(ipixels.typecode) |
| 256 # It's annoying that there is no cheap way to set the array size :-( |
| 257 out.extend(ipixels) |
| 258 out.extend(apixels) |
| 259 # Interleave in the pixel data |
| 260 for i in range(ipsize): |
| 261 out[i:newtotal:newpsize] = ipixels[i:itotal:ipsize] |
| 262 for i in range(apsize): |
| 263 out[i+ipsize:newtotal:newpsize] = apixels[i:atotal:apsize] |
| 264 return out |
| 265 |
| 266 def check_palette(palette): |
| 267 """Check a palette argument (to the :class:`Writer` class) for validity. |
| 268 Returns the palette as a list if okay; raises an exception otherwise. |
| 269 """ |
| 270 |
| 271 # None is the default and is allowed. |
| 272 if palette is None: |
| 273 return None |
| 274 |
| 275 p = list(palette) |
| 276 if not (0 < len(p) <= 256): |
| 277 raise ValueError("a palette must have between 1 and 256 entries") |
| 278 seen_triple = False |
| 279 for i,t in enumerate(p): |
| 280 if len(t) not in (3,4): |
| 281 raise ValueError( |
| 282 "palette entry %d: entries must be 3- or 4-tuples." % i) |
| 283 if len(t) == 3: |
| 284 seen_triple = True |
| 285 if seen_triple and len(t) == 4: |
| 286 raise ValueError( |
| 287 "palette entry %d: all 4-tuples must precede all 3-tuples" % i) |
| 288 for x in t: |
| 289 if int(x) != x or not(0 <= x <= 255): |
| 290 raise ValueError( |
| 291 "palette entry %d: values must be integer: 0 <= x <= 255" % i) |
| 292 return p |
| 293 |
| 294 class Error(Exception): |
| 295 prefix = 'Error' |
| 296 def __str__(self): |
| 297 return self.prefix + ': ' + ' '.join(self.args) |
| 298 |
| 299 class FormatError(Error): |
| 300 """Problem with input file format. In other words, PNG file does |
| 301 not conform to the specification in some way and is invalid. |
| 302 """ |
| 303 |
| 304 prefix = 'FormatError' |
| 305 |
| 306 class ChunkError(FormatError): |
| 307 prefix = 'ChunkError' |
| 308 |
| 309 |
| 310 class Writer: |
| 311 """ |
| 312 PNG encoder in pure Python. |
| 313 """ |
| 314 |
| 315 def __init__(self, width=None, height=None, |
| 316 size=None, |
| 317 greyscale=False, |
| 318 alpha=False, |
| 319 bitdepth=8, |
| 320 palette=None, |
| 321 transparent=None, |
| 322 background=None, |
| 323 gamma=None, |
| 324 compression=None, |
| 325 interlace=False, |
| 326 bytes_per_sample=None, # deprecated |
| 327 planes=None, |
| 328 colormap=None, |
| 329 maxval=None, |
| 330 chunk_limit=2**20): |
| 331 """ |
| 332 Create a PNG encoder object. |
| 333 |
| 334 Arguments: |
| 335 |
| 336 width, height |
| 337 Image size in pixels, as two separate arguments. |
| 338 size |
| 339 Image size (w,h) in pixels, as single argument. |
| 340 greyscale |
| 341 Input data is greyscale, not RGB. |
| 342 alpha |
| 343 Input data has alpha channel (RGBA or LA). |
| 344 bitdepth |
| 345 Bit depth: from 1 to 16. |
| 346 palette |
| 347 Create a palette for a colour mapped image (colour type 3). |
| 348 transparent |
| 349 Specify a transparent colour (create a ``tRNS`` chunk). |
| 350 background |
| 351 Specify a default background colour (create a ``bKGD`` chunk). |
| 352 gamma |
| 353 Specify a gamma value (create a ``gAMA`` chunk). |
| 354 compression |
| 355 zlib compression level (1-9). |
| 356 interlace |
| 357 Create an interlaced image. |
| 358 chunk_limit |
| 359 Write multiple ``IDAT`` chunks to save memory. |
| 360 |
| 361 The image size (in pixels) can be specified either by using the |
| 362 `width` and `height` arguments, or with the single `size` |
| 363 argument. If `size` is used it should be a pair (*width*, |
| 364 *height*). |
| 365 |
| 366 `greyscale` and `alpha` are booleans that specify whether |
| 367 an image is greyscale (or colour), and whether it has an |
| 368 alpha channel (or not). |
| 369 |
| 370 `bitdepth` specifies the bit depth of the source pixel values. |
| 371 Each source pixel value must be an integer between 0 and |
| 372 ``2**bitdepth-1``. For example, 8-bit images have values |
| 373 between 0 and 255. PNG only stores images with bit depths of |
| 374 1,2,4,8, or 16. When `bitdepth` is not one of these values, |
| 375 the next highest valid bit depth is selected, and an ``sBIT`` |
| 376 (significant bits) chunk is generated that specifies the original |
| 377 precision of the source image. In this case the supplied pixel |
| 378 values will be rescaled to fit the range of the selected bit depth. |
| 379 |
| 380 The details of which bit depth / colour model combinations the |
| 381 PNG file format supports directly, are somewhat arcane |
| 382 (refer to the PNG specification for full details). Briefly: |
| 383 "small" bit depths (1,2,4) are only allowed with greyscale and |
| 384 colour mapped images; colour mapped images cannot have bit depth |
| 385 16. |
| 386 |
| 387 For colour mapped images (in other words, when the `palette` |
| 388 argument is specified) the `bitdepth` argument must match one of |
| 389 the valid PNG bit depths: 1, 2, 4, or 8. (It is valid to have a |
| 390 PNG image with a palette and an ``sBIT`` chunk, but the meaning |
| 391 is slightly different; it would be awkward to press the |
| 392 `bitdepth` argument into service for this.) |
| 393 |
| 394 The `palette` option, when specified, causes a colour mapped image |
| 395 to be created: the PNG colour type is set to 3; greyscale |
| 396 must not be set; alpha must not be set; transparent must |
| 397 not be set; the bit depth must be 1,2,4, or 8. When a colour |
| 398 mapped image is created, the pixel values are palette indexes |
| 399 and the `bitdepth` argument specifies the size of these indexes |
| 400 (not the size of the colour values in the palette). |
| 401 |
| 402 The palette argument value should be a sequence of 3- or |
| 403 4-tuples. 3-tuples specify RGB palette entries; 4-tuples |
| 404 specify RGBA palette entries. If both 4-tuples and 3-tuples |
| 405 appear in the sequence then all the 4-tuples must come |
| 406 before all the 3-tuples. A ``PLTE`` chunk is created; if there |
| 407 are 4-tuples then a ``tRNS`` chunk is created as well. The |
| 408 ``PLTE`` chunk will contain all the RGB triples in the same |
| 409 sequence; the ``tRNS`` chunk will contain the alpha channel for |
| 410 all the 4-tuples, in the same sequence. Palette entries |
| 411 are always 8-bit. |
| 412 |
| 413 If specified, the `transparent` and `background` parameters must |
| 414 be a tuple with three integer values for red, green, blue, or |
| 415 a simple integer (or singleton tuple) for a greyscale image. |
| 416 |
| 417 If specified, the `gamma` parameter must be a positive number |
| 418 (generally, a float). A ``gAMA`` chunk will be created. Note that |
| 419 this will not change the values of the pixels as they appear in |
| 420 the PNG file, they are assumed to have already been converted |
| 421 appropriately for the gamma specified. |
| 422 |
| 423 The `compression` argument specifies the compression level |
| 424 to be used by the ``zlib`` module. Higher values are likely |
| 425 to compress better, but will be slower to compress. The |
| 426 default for this argument is ``None``; this does not mean |
| 427 no compression, rather it means that the default from the |
| 428 ``zlib`` module is used (which is generally acceptable). |
| 429 |
| 430 If `interlace` is true then an interlaced image is created |
| 431 (using PNG's so far only interace method, *Adam7*). This does not |
| 432 affect how the pixels should be presented to the encoder, rather |
| 433 it changes how they are arranged into the PNG file. On slow |
| 434 connexions interlaced images can be partially decoded by the |
| 435 browser to give a rough view of the image that is successively |
| 436 refined as more image data appears. |
| 437 |
| 438 .. note :: |
| 439 |
| 440 Enabling the `interlace` option requires the entire image |
| 441 to be processed in working memory. |
| 442 |
| 443 `chunk_limit` is used to limit the amount of memory used whilst |
| 444 compressing the image. In order to avoid using large amounts of |
| 445 memory, multiple ``IDAT`` chunks may be created. |
| 446 """ |
| 447 |
| 448 # At the moment the `planes` argument is ignored; |
| 449 # its purpose is to act as a dummy so that |
| 450 # ``Writer(x, y, **info)`` works, where `info` is a dictionary |
| 451 # returned by Reader.read and friends. |
| 452 # Ditto for `colormap`. |
| 453 |
| 454 # A couple of helper functions come first. Best skipped if you |
| 455 # are reading through. |
| 456 |
| 457 def isinteger(x): |
| 458 try: |
| 459 return int(x) == x |
| 460 except: |
| 461 return False |
| 462 |
| 463 def check_color(c, which): |
| 464 """Checks that a colour argument for transparent or |
| 465 background options is the right form. Also "corrects" bare |
| 466 integers to 1-tuples. |
| 467 """ |
| 468 |
| 469 if c is None: |
| 470 return c |
| 471 if greyscale: |
| 472 try: |
| 473 l = len(c) |
| 474 except TypeError: |
| 475 c = (c,) |
| 476 if len(c) != 1: |
| 477 raise ValueError("%s for greyscale must be 1-tuple" % |
| 478 which) |
| 479 if not isinteger(c[0]): |
| 480 raise ValueError( |
| 481 "%s colour for greyscale must be integer" % |
| 482 which) |
| 483 else: |
| 484 if not (len(c) == 3 and |
| 485 isinteger(c[0]) and |
| 486 isinteger(c[1]) and |
| 487 isinteger(c[2])): |
| 488 raise ValueError( |
| 489 "%s colour must be a triple of integers" % |
| 490 which) |
| 491 return c |
| 492 |
| 493 if size: |
| 494 if len(size) != 2: |
| 495 raise ValueError( |
| 496 "size argument should be a pair (width, height)") |
| 497 if width is not None and width != size[0]: |
| 498 raise ValueError( |
| 499 "size[0] (%r) and width (%r) should match when both are used." |
| 500 % (size[0], width)) |
| 501 if height is not None and height != size[1]: |
| 502 raise ValueError( |
| 503 "size[1] (%r) and height (%r) should match when both are used.
" |
| 504 % (size[1], height)) |
| 505 width,height = size |
| 506 del size |
| 507 |
| 508 if width <= 0 or height <= 0: |
| 509 raise ValueError("width and height must be greater than zero") |
| 510 if not isinteger(width) or not isinteger(height): |
| 511 raise ValueError("width and height must be integers") |
| 512 # http://www.w3.org/TR/PNG/#7Integers-and-byte-order |
| 513 if width > 2**32-1 or height > 2**32-1: |
| 514 raise ValueError("width and height cannot exceed 2**32-1") |
| 515 |
| 516 if alpha and transparent is not None: |
| 517 raise ValueError( |
| 518 "transparent colour not allowed with alpha channel") |
| 519 |
| 520 if bytes_per_sample is not None: |
| 521 warnings.warn('please use bitdepth instead of bytes_per_sample', |
| 522 DeprecationWarning) |
| 523 if bytes_per_sample not in (0.125, 0.25, 0.5, 1, 2): |
| 524 raise ValueError( |
| 525 "bytes per sample must be .125, .25, .5, 1, or 2") |
| 526 bitdepth = int(8*bytes_per_sample) |
| 527 del bytes_per_sample |
| 528 if not isinteger(bitdepth) or bitdepth < 1 or 16 < bitdepth: |
| 529 raise ValueError("bitdepth (%r) must be a postive integer <= 16" % |
| 530 bitdepth) |
| 531 |
| 532 self.rescale = None |
| 533 if palette: |
| 534 if bitdepth not in (1,2,4,8): |
| 535 raise ValueError("with palette, bitdepth must be 1, 2, 4, or 8") |
| 536 if transparent is not None: |
| 537 raise ValueError("transparent and palette not compatible") |
| 538 if alpha: |
| 539 raise ValueError("alpha and palette not compatible") |
| 540 if greyscale: |
| 541 raise ValueError("greyscale and palette not compatible") |
| 542 else: |
| 543 # No palette, check for sBIT chunk generation. |
| 544 if alpha or not greyscale: |
| 545 if bitdepth not in (8,16): |
| 546 targetbitdepth = (8,16)[bitdepth > 8] |
| 547 self.rescale = (bitdepth, targetbitdepth) |
| 548 bitdepth = targetbitdepth |
| 549 del targetbitdepth |
| 550 else: |
| 551 assert greyscale |
| 552 assert not alpha |
| 553 if bitdepth not in (1,2,4,8,16): |
| 554 if bitdepth > 8: |
| 555 targetbitdepth = 16 |
| 556 elif bitdepth == 3: |
| 557 targetbitdepth = 4 |
| 558 else: |
| 559 assert bitdepth in (5,6,7) |
| 560 targetbitdepth = 8 |
| 561 self.rescale = (bitdepth, targetbitdepth) |
| 562 bitdepth = targetbitdepth |
| 563 del targetbitdepth |
| 564 |
| 565 if bitdepth < 8 and (alpha or not greyscale and not palette): |
| 566 raise ValueError( |
| 567 "bitdepth < 8 only permitted with greyscale or palette") |
| 568 if bitdepth > 8 and palette: |
| 569 raise ValueError( |
| 570 "bit depth must be 8 or less for images with palette") |
| 571 |
| 572 transparent = check_color(transparent, 'transparent') |
| 573 background = check_color(background, 'background') |
| 574 |
| 575 # It's important that the true boolean values (greyscale, alpha, |
| 576 # colormap, interlace) are converted to bool because Iverson's |
| 577 # convention is relied upon later on. |
| 578 self.width = width |
| 579 self.height = height |
| 580 self.transparent = transparent |
| 581 self.background = background |
| 582 self.gamma = gamma |
| 583 self.greyscale = bool(greyscale) |
| 584 self.alpha = bool(alpha) |
| 585 self.colormap = bool(palette) |
| 586 self.bitdepth = int(bitdepth) |
| 587 self.compression = compression |
| 588 self.chunk_limit = chunk_limit |
| 589 self.interlace = bool(interlace) |
| 590 self.palette = check_palette(palette) |
| 591 |
| 592 self.color_type = 4*self.alpha + 2*(not greyscale) + 1*self.colormap |
| 593 assert self.color_type in (0,2,3,4,6) |
| 594 |
| 595 self.color_planes = (3,1)[self.greyscale or self.colormap] |
| 596 self.planes = self.color_planes + self.alpha |
| 597 # :todo: fix for bitdepth < 8 |
| 598 self.psize = (self.bitdepth/8) * self.planes |
| 599 |
| 600 def make_palette(self): |
| 601 """Create the byte sequences for a ``PLTE`` and if necessary a |
| 602 ``tRNS`` chunk. Returned as a pair (*p*, *t*). *t* will be |
| 603 ``None`` if no ``tRNS`` chunk is necessary. |
| 604 """ |
| 605 |
| 606 p = array('B') |
| 607 t = array('B') |
| 608 |
| 609 for x in self.palette: |
| 610 p.extend(x[0:3]) |
| 611 if len(x) > 3: |
| 612 t.append(x[3]) |
| 613 p = tostring(p) |
| 614 t = tostring(t) |
| 615 if t: |
| 616 return p,t |
| 617 return p,None |
| 618 |
| 619 def write(self, outfile, rows): |
| 620 """Write a PNG image to the output file. `rows` should be |
| 621 an iterable that yields each row in boxed row flat pixel format. |
| 622 The rows should be the rows of the original image, so there |
| 623 should be ``self.height`` rows of ``self.width * self.planes`` values. |
| 624 If `interlace` is specified (when creating the instance), then |
| 625 an interlaced PNG file will be written. Supply the rows in the |
| 626 normal image order; the interlacing is carried out internally. |
| 627 |
| 628 .. note :: |
| 629 |
| 630 Interlacing will require the entire image to be in working memory. |
| 631 """ |
| 632 |
| 633 if self.interlace: |
| 634 fmt = 'BH'[self.bitdepth > 8] |
| 635 a = array(fmt, itertools.chain(*rows)) |
| 636 return self.write_array(outfile, a) |
| 637 else: |
| 638 nrows = self.write_passes(outfile, rows) |
| 639 if nrows != self.height: |
| 640 raise ValueError( |
| 641 "rows supplied (%d) does not match height (%d)" % |
| 642 (nrows, self.height)) |
| 643 |
| 644 def write_passes(self, outfile, rows, packed=False): |
| 645 """ |
| 646 Write a PNG image to the output file. |
| 647 |
| 648 Most users are expected to find the :meth:`write` or |
| 649 :meth:`write_array` method more convenient. |
| 650 |
| 651 The rows should be given to this method in the order that |
| 652 they appear in the output file. For straightlaced images, |
| 653 this is the usual top to bottom ordering, but for interlaced |
| 654 images the rows should have already been interlaced before |
| 655 passing them to this function. |
| 656 |
| 657 `rows` should be an iterable that yields each row. When |
| 658 `packed` is ``False`` the rows should be in boxed row flat pixel |
| 659 format; when `packed` is ``True`` each row should be a packed |
| 660 sequence of bytes. |
| 661 |
| 662 """ |
| 663 |
| 664 # http://www.w3.org/TR/PNG/#5PNG-file-signature |
| 665 outfile.write(_signature) |
| 666 |
| 667 # http://www.w3.org/TR/PNG/#11IHDR |
| 668 write_chunk(outfile, 'IHDR', |
| 669 struct.pack("!2I5B", self.width, self.height, |
| 670 self.bitdepth, self.color_type, |
| 671 0, 0, self.interlace)) |
| 672 |
| 673 # See :chunk:order |
| 674 # http://www.w3.org/TR/PNG/#11gAMA |
| 675 if self.gamma is not None: |
| 676 write_chunk(outfile, 'gAMA', |
| 677 struct.pack("!L", int(round(self.gamma*1e5)))) |
| 678 |
| 679 # See :chunk:order |
| 680 # http://www.w3.org/TR/PNG/#11sBIT |
| 681 if self.rescale: |
| 682 write_chunk(outfile, 'sBIT', |
| 683 struct.pack('%dB' % self.planes, |
| 684 *[self.rescale[0]]*self.planes)) |
| 685 |
| 686 # :chunk:order: Without a palette (PLTE chunk), ordering is |
| 687 # relatively relaxed. With one, gAMA chunk must precede PLTE |
| 688 # chunk which must precede tRNS and bKGD. |
| 689 # See http://www.w3.org/TR/PNG/#5ChunkOrdering |
| 690 if self.palette: |
| 691 p,t = self.make_palette() |
| 692 write_chunk(outfile, 'PLTE', p) |
| 693 if t: |
| 694 # tRNS chunk is optional. Only needed if palette entries |
| 695 # have alpha. |
| 696 write_chunk(outfile, 'tRNS', t) |
| 697 |
| 698 # http://www.w3.org/TR/PNG/#11tRNS |
| 699 if self.transparent is not None: |
| 700 if self.greyscale: |
| 701 write_chunk(outfile, 'tRNS', |
| 702 struct.pack("!1H", *self.transparent)) |
| 703 else: |
| 704 write_chunk(outfile, 'tRNS', |
| 705 struct.pack("!3H", *self.transparent)) |
| 706 |
| 707 # http://www.w3.org/TR/PNG/#11bKGD |
| 708 if self.background is not None: |
| 709 if self.greyscale: |
| 710 write_chunk(outfile, 'bKGD', |
| 711 struct.pack("!1H", *self.background)) |
| 712 else: |
| 713 write_chunk(outfile, 'bKGD', |
| 714 struct.pack("!3H", *self.background)) |
| 715 |
| 716 # http://www.w3.org/TR/PNG/#11IDAT |
| 717 if self.compression is not None: |
| 718 compressor = zlib.compressobj(self.compression) |
| 719 else: |
| 720 compressor = zlib.compressobj() |
| 721 |
| 722 # Choose an extend function based on the bitdepth. The extend |
| 723 # function packs/decomposes the pixel values into bytes and |
| 724 # stuffs them onto the data array. |
| 725 data = array('B') |
| 726 if self.bitdepth == 8 or packed: |
| 727 extend = data.extend |
| 728 elif self.bitdepth == 16: |
| 729 # Decompose into bytes |
| 730 def extend(sl): |
| 731 fmt = '!%dH' % len(sl) |
| 732 data.extend(array('B', struct.pack(fmt, *sl))) |
| 733 else: |
| 734 # Pack into bytes |
| 735 assert self.bitdepth < 8 |
| 736 # samples per byte |
| 737 spb = int(8/self.bitdepth) |
| 738 def extend(sl): |
| 739 a = array('B', sl) |
| 740 # Adding padding bytes so we can group into a whole |
| 741 # number of spb-tuples. |
| 742 l = float(len(a)) |
| 743 extra = math.ceil(l / float(spb))*spb - l |
| 744 a.extend([0]*int(extra)) |
| 745 # Pack into bytes |
| 746 l = group(a, spb) |
| 747 l = map(lambda e: reduce(lambda x,y: |
| 748 (x << self.bitdepth) + y, e), l) |
| 749 data.extend(l) |
| 750 if self.rescale: |
| 751 oldextend = extend |
| 752 factor = \ |
| 753 float(2**self.rescale[1]-1) / float(2**self.rescale[0]-1) |
| 754 def extend(sl): |
| 755 oldextend(map(lambda x: int(round(factor*x)), sl)) |
| 756 |
| 757 # Build the first row, testing mostly to see if we need to |
| 758 # changed the extend function to cope with NumPy integer types |
| 759 # (they cause our ordinary definition of extend to fail, so we |
| 760 # wrap it). See |
| 761 # http://code.google.com/p/pypng/issues/detail?id=44 |
| 762 enumrows = enumerate(rows) |
| 763 del rows |
| 764 |
| 765 # First row's filter type. |
| 766 data.append(0) |
| 767 # :todo: Certain exceptions in the call to ``.next()`` or the |
| 768 # following try would indicate no row data supplied. |
| 769 # Should catch. |
| 770 i,row = enumrows.next() |
| 771 try: |
| 772 # If this fails... |
| 773 extend(row) |
| 774 except: |
| 775 # ... try a version that converts the values to int first. |
| 776 # Not only does this work for the (slightly broken) NumPy |
| 777 # types, there are probably lots of other, unknown, "nearly" |
| 778 # int types it works for. |
| 779 def wrapmapint(f): |
| 780 return lambda sl: f(map(int, sl)) |
| 781 extend = wrapmapint(extend) |
| 782 del wrapmapint |
| 783 extend(row) |
| 784 |
| 785 for i,row in enumrows: |
| 786 # Add "None" filter type. Currently, it's essential that |
| 787 # this filter type be used for every scanline as we do not |
| 788 # mark the first row of a reduced pass image; that means we |
| 789 # could accidentally compute the wrong filtered scanline if |
| 790 # we used "up", "average", or "paeth" on such a line. |
| 791 data.append(0) |
| 792 extend(row) |
| 793 if len(data) > self.chunk_limit: |
| 794 compressed = compressor.compress(tostring(data)) |
| 795 if len(compressed): |
| 796 # print >> sys.stderr, len(data), len(compressed) |
| 797 write_chunk(outfile, 'IDAT', compressed) |
| 798 # Because of our very witty definition of ``extend``, |
| 799 # above, we must re-use the same ``data`` object. Hence |
| 800 # we use ``del`` to empty this one, rather than create a |
| 801 # fresh one (which would be my natural FP instinct). |
| 802 del data[:] |
| 803 if len(data): |
| 804 compressed = compressor.compress(tostring(data)) |
| 805 else: |
| 806 compressed = '' |
| 807 flushed = compressor.flush() |
| 808 if len(compressed) or len(flushed): |
| 809 # print >> sys.stderr, len(data), len(compressed), len(flushed) |
| 810 write_chunk(outfile, 'IDAT', compressed + flushed) |
| 811 # http://www.w3.org/TR/PNG/#11IEND |
| 812 write_chunk(outfile, 'IEND') |
| 813 return i+1 |
| 814 |
| 815 def write_array(self, outfile, pixels): |
| 816 """ |
| 817 Write an array in flat row flat pixel format as a PNG file on |
| 818 the output file. See also :meth:`write` method. |
| 819 """ |
| 820 |
| 821 if self.interlace: |
| 822 self.write_passes(outfile, self.array_scanlines_interlace(pixels)) |
| 823 else: |
| 824 self.write_passes(outfile, self.array_scanlines(pixels)) |
| 825 |
| 826 def write_packed(self, outfile, rows): |
| 827 """ |
| 828 Write PNG file to `outfile`. The pixel data comes from `rows` |
| 829 which should be in boxed row packed format. Each row should be |
| 830 a sequence of packed bytes. |
| 831 |
| 832 Technically, this method does work for interlaced images but it |
| 833 is best avoided. For interlaced images, the rows should be |
| 834 presented in the order that they appear in the file. |
| 835 |
| 836 This method should not be used when the source image bit depth |
| 837 is not one naturally supported by PNG; the bit depth should be |
| 838 1, 2, 4, 8, or 16. |
| 839 """ |
| 840 |
| 841 if self.rescale: |
| 842 raise Error("write_packed method not suitable for bit depth %d" % |
| 843 self.rescale[0]) |
| 844 return self.write_passes(outfile, rows, packed=True) |
| 845 |
| 846 def convert_pnm(self, infile, outfile): |
| 847 """ |
| 848 Convert a PNM file containing raw pixel data into a PNG file |
| 849 with the parameters set in the writer object. Works for |
| 850 (binary) PGM, PPM, and PAM formats. |
| 851 """ |
| 852 |
| 853 if self.interlace: |
| 854 pixels = array('B') |
| 855 pixels.fromfile(infile, |
| 856 (self.bitdepth/8) * self.color_planes * |
| 857 self.width * self.height) |
| 858 self.write_passes(outfile, self.array_scanlines_interlace(pixels)) |
| 859 else: |
| 860 self.write_passes(outfile, self.file_scanlines(infile)) |
| 861 |
| 862 def convert_ppm_and_pgm(self, ppmfile, pgmfile, outfile): |
| 863 """ |
| 864 Convert a PPM and PGM file containing raw pixel data into a |
| 865 PNG outfile with the parameters set in the writer object. |
| 866 """ |
| 867 pixels = array('B') |
| 868 pixels.fromfile(ppmfile, |
| 869 (self.bitdepth/8) * self.color_planes * |
| 870 self.width * self.height) |
| 871 apixels = array('B') |
| 872 apixels.fromfile(pgmfile, |
| 873 (self.bitdepth/8) * |
| 874 self.width * self.height) |
| 875 pixels = interleave_planes(pixels, apixels, |
| 876 (self.bitdepth/8) * self.color_planes, |
| 877 (self.bitdepth/8)) |
| 878 if self.interlace: |
| 879 self.write_passes(outfile, self.array_scanlines_interlace(pixels)) |
| 880 else: |
| 881 self.write_passes(outfile, self.array_scanlines(pixels)) |
| 882 |
| 883 def file_scanlines(self, infile): |
| 884 """ |
| 885 Generates boxed rows in flat pixel format, from the input file |
| 886 `infile`. It assumes that the input file is in a "Netpbm-like" |
| 887 binary format, and is positioned at the beginning of the first |
| 888 pixel. The number of pixels to read is taken from the image |
| 889 dimensions (`width`, `height`, `planes`) and the number of bytes |
| 890 per value is implied by the image `bitdepth`. |
| 891 """ |
| 892 |
| 893 # Values per row |
| 894 vpr = self.width * self.planes |
| 895 row_bytes = vpr |
| 896 if self.bitdepth > 8: |
| 897 assert self.bitdepth == 16 |
| 898 row_bytes *= 2 |
| 899 fmt = '>%dH' % vpr |
| 900 def line(): |
| 901 return array('H', struct.unpack(fmt, infile.read(row_bytes))) |
| 902 else: |
| 903 def line(): |
| 904 scanline = array('B', infile.read(row_bytes)) |
| 905 return scanline |
| 906 for y in range(self.height): |
| 907 yield line() |
| 908 |
| 909 def array_scanlines(self, pixels): |
| 910 """ |
| 911 Generates boxed rows (flat pixels) from flat rows (flat pixels) |
| 912 in an array. |
| 913 """ |
| 914 |
| 915 # Values per row |
| 916 vpr = self.width * self.planes |
| 917 stop = 0 |
| 918 for y in range(self.height): |
| 919 start = stop |
| 920 stop = start + vpr |
| 921 yield pixels[start:stop] |
| 922 |
| 923 def array_scanlines_interlace(self, pixels): |
| 924 """ |
| 925 Generator for interlaced scanlines from an array. `pixels` is |
| 926 the full source image in flat row flat pixel format. The |
| 927 generator yields each scanline of the reduced passes in turn, in |
| 928 boxed row flat pixel format. |
| 929 """ |
| 930 |
| 931 # http://www.w3.org/TR/PNG/#8InterlaceMethods |
| 932 # Array type. |
| 933 fmt = 'BH'[self.bitdepth > 8] |
| 934 # Value per row |
| 935 vpr = self.width * self.planes |
| 936 for xstart, ystart, xstep, ystep in _adam7: |
| 937 if xstart >= self.width: |
| 938 continue |
| 939 # Pixels per row (of reduced image) |
| 940 ppr = int(math.ceil((self.width-xstart)/float(xstep))) |
| 941 # number of values in reduced image row. |
| 942 row_len = ppr*self.planes |
| 943 for y in range(ystart, self.height, ystep): |
| 944 if xstep == 1: |
| 945 offset = y * vpr |
| 946 yield pixels[offset:offset+vpr] |
| 947 else: |
| 948 row = array(fmt) |
| 949 # There's no easier way to set the length of an array |
| 950 row.extend(pixels[0:row_len]) |
| 951 offset = y * vpr + xstart * self.planes |
| 952 end_offset = (y+1) * vpr |
| 953 skip = self.planes * xstep |
| 954 for i in range(self.planes): |
| 955 row[i::self.planes] = \ |
| 956 pixels[offset+i:end_offset:skip] |
| 957 yield row |
| 958 |
| 959 def write_chunk(outfile, tag, data=strtobytes('')): |
| 960 """ |
| 961 Write a PNG chunk to the output file, including length and |
| 962 checksum. |
| 963 """ |
| 964 |
| 965 # http://www.w3.org/TR/PNG/#5Chunk-layout |
| 966 outfile.write(struct.pack("!I", len(data))) |
| 967 tag = strtobytes(tag) |
| 968 outfile.write(tag) |
| 969 outfile.write(data) |
| 970 checksum = zlib.crc32(tag) |
| 971 checksum = zlib.crc32(data, checksum) |
| 972 checksum &= 2**32-1 |
| 973 outfile.write(struct.pack("!I", checksum)) |
| 974 |
| 975 def write_chunks(out, chunks): |
| 976 """Create a PNG file by writing out the chunks.""" |
| 977 |
| 978 out.write(_signature) |
| 979 for chunk in chunks: |
| 980 write_chunk(out, *chunk) |
| 981 |
| 982 def filter_scanline(type, line, fo, prev=None): |
| 983 """Apply a scanline filter to a scanline. `type` specifies the |
| 984 filter type (0 to 4); `line` specifies the current (unfiltered) |
| 985 scanline as a sequence of bytes; `prev` specifies the previous |
| 986 (unfiltered) scanline as a sequence of bytes. `fo` specifies the |
| 987 filter offset; normally this is size of a pixel in bytes (the number |
| 988 of bytes per sample times the number of channels), but when this is |
| 989 < 1 (for bit depths < 8) then the filter offset is 1. |
| 990 """ |
| 991 |
| 992 assert 0 <= type < 5 |
| 993 |
| 994 # The output array. Which, pathetically, we extend one-byte at a |
| 995 # time (fortunately this is linear). |
| 996 out = array('B', [type]) |
| 997 |
| 998 def sub(): |
| 999 ai = -fo |
| 1000 for x in line: |
| 1001 if ai >= 0: |
| 1002 x = (x - line[ai]) & 0xff |
| 1003 out.append(x) |
| 1004 ai += 1 |
| 1005 def up(): |
| 1006 for i,x in enumerate(line): |
| 1007 x = (x - prev[i]) & 0xff |
| 1008 out.append(x) |
| 1009 def average(): |
| 1010 ai = -fo |
| 1011 for i,x in enumerate(line): |
| 1012 if ai >= 0: |
| 1013 x = (x - ((line[ai] + prev[i]) >> 1)) & 0xff |
| 1014 else: |
| 1015 x = (x - (prev[i] >> 1)) & 0xff |
| 1016 out.append(x) |
| 1017 ai += 1 |
| 1018 def paeth(): |
| 1019 # http://www.w3.org/TR/PNG/#9Filter-type-4-Paeth |
| 1020 ai = -fo # also used for ci |
| 1021 for i,x in enumerate(line): |
| 1022 a = 0 |
| 1023 b = prev[i] |
| 1024 c = 0 |
| 1025 |
| 1026 if ai >= 0: |
| 1027 a = line[ai] |
| 1028 c = prev[ai] |
| 1029 p = a + b - c |
| 1030 pa = abs(p - a) |
| 1031 pb = abs(p - b) |
| 1032 pc = abs(p - c) |
| 1033 if pa <= pb and pa <= pc: Pr = a |
| 1034 elif pb <= pc: Pr = b |
| 1035 else: Pr = c |
| 1036 |
| 1037 x = (x - Pr) & 0xff |
| 1038 out.append(x) |
| 1039 ai += 1 |
| 1040 |
| 1041 if not prev: |
| 1042 # We're on the first line. Some of the filters can be reduced |
| 1043 # to simpler cases which makes handling the line "off the top" |
| 1044 # of the image simpler. "up" becomes "none"; "paeth" becomes |
| 1045 # "left" (non-trivial, but true). "average" needs to be handled |
| 1046 # specially. |
| 1047 if type == 2: # "up" |
| 1048 return line # type = 0 |
| 1049 elif type == 3: |
| 1050 prev = [0]*len(line) |
| 1051 elif type == 4: # "paeth" |
| 1052 type = 1 |
| 1053 if type == 0: |
| 1054 out.extend(line) |
| 1055 elif type == 1: |
| 1056 sub() |
| 1057 elif type == 2: |
| 1058 up() |
| 1059 elif type == 3: |
| 1060 average() |
| 1061 else: # type == 4 |
| 1062 paeth() |
| 1063 return out |
| 1064 |
| 1065 |
| 1066 def from_array(a, mode=None, info={}): |
| 1067 """Create a PNG :class:`Image` object from a 2- or 3-dimensional array. |
| 1068 One application of this function is easy PIL-style saving: |
| 1069 ``png.from_array(pixels, 'L').save('foo.png')``. |
| 1070 |
| 1071 .. note : |
| 1072 |
| 1073 The use of the term *3-dimensional* is for marketing purposes |
| 1074 only. It doesn't actually work. Please bear with us. Meanwhile |
| 1075 enjoy the complimentary snacks (on request) and please use a |
| 1076 2-dimensional array. |
| 1077 |
| 1078 Unless they are specified using the *info* parameter, the PNG's |
| 1079 height and width are taken from the array size. For a 3 dimensional |
| 1080 array the first axis is the height; the second axis is the width; |
| 1081 and the third axis is the channel number. Thus an RGB image that is |
| 1082 16 pixels high and 8 wide will use an array that is 16x8x3. For 2 |
| 1083 dimensional arrays the first axis is the height, but the second axis |
| 1084 is ``width*channels``, so an RGB image that is 16 pixels high and 8 |
| 1085 wide will use a 2-dimensional array that is 16x24 (each row will be |
| 1086 8*3==24 sample values). |
| 1087 |
| 1088 *mode* is a string that specifies the image colour format in a |
| 1089 PIL-style mode. It can be: |
| 1090 |
| 1091 ``'L'`` |
| 1092 greyscale (1 channel) |
| 1093 ``'LA'`` |
| 1094 greyscale with alpha (2 channel) |
| 1095 ``'RGB'`` |
| 1096 colour image (3 channel) |
| 1097 ``'RGBA'`` |
| 1098 colour image with alpha (4 channel) |
| 1099 |
| 1100 The mode string can also specify the bit depth (overriding how this |
| 1101 function normally derives the bit depth, see below). Appending |
| 1102 ``';16'`` to the mode will cause the PNG to be 16 bits per channel; |
| 1103 any decimal from 1 to 16 can be used to specify the bit depth. |
| 1104 |
| 1105 When a 2-dimensional array is used *mode* determines how many |
| 1106 channels the image has, and so allows the width to be derived from |
| 1107 the second array dimension. |
| 1108 |
| 1109 The array is expected to be a ``numpy`` array, but it can be any |
| 1110 suitable Python sequence. For example, a list of lists can be used: |
| 1111 ``png.from_array([[0, 255, 0], [255, 0, 255]], 'L')``. The exact |
| 1112 rules are: ``len(a)`` gives the first dimension, height; |
| 1113 ``len(a[0])`` gives the second dimension; ``len(a[0][0])`` gives the |
| 1114 third dimension, unless an exception is raised in which case a |
| 1115 2-dimensional array is assumed. It's slightly more complicated than |
| 1116 that because an iterator of rows can be used, and it all still |
| 1117 works. Using an iterator allows data to be streamed efficiently. |
| 1118 |
| 1119 The bit depth of the PNG is normally taken from the array element's |
| 1120 datatype (but if *mode* specifies a bitdepth then that is used |
| 1121 instead). The array element's datatype is determined in a way which |
| 1122 is supposed to work both for ``numpy`` arrays and for Python |
| 1123 ``array.array`` objects. A 1 byte datatype will give a bit depth of |
| 1124 8, a 2 byte datatype will give a bit depth of 16. If the datatype |
| 1125 does not have an implicit size, for example it is a plain Python |
| 1126 list of lists, as above, then a default of 8 is used. |
| 1127 |
| 1128 The *info* parameter is a dictionary that can be used to specify |
| 1129 metadata (in the same style as the arguments to the |
| 1130 :class:``png.Writer`` class). For this function the keys that are |
| 1131 useful are: |
| 1132 |
| 1133 height |
| 1134 overrides the height derived from the array dimensions and allows |
| 1135 *a* to be an iterable. |
| 1136 width |
| 1137 overrides the width derived from the array dimensions. |
| 1138 bitdepth |
| 1139 overrides the bit depth derived from the element datatype (but |
| 1140 must match *mode* if that also specifies a bit depth). |
| 1141 |
| 1142 Generally anything specified in the |
| 1143 *info* dictionary will override any implicit choices that this |
| 1144 function would otherwise make, but must match any explicit ones. |
| 1145 For example, if the *info* dictionary has a ``greyscale`` key then |
| 1146 this must be true when mode is ``'L'`` or ``'LA'`` and false when |
| 1147 mode is ``'RGB'`` or ``'RGBA'``. |
| 1148 """ |
| 1149 |
| 1150 # We abuse the *info* parameter by modifying it. Take a copy here. |
| 1151 # (Also typechecks *info* to some extent). |
| 1152 info = dict(info) |
| 1153 |
| 1154 # Syntax check mode string. |
| 1155 bitdepth = None |
| 1156 try: |
| 1157 mode = mode.split(';') |
| 1158 if len(mode) not in (1,2): |
| 1159 raise Error() |
| 1160 if mode[0] not in ('L', 'LA', 'RGB', 'RGBA'): |
| 1161 raise Error() |
| 1162 if len(mode) == 2: |
| 1163 try: |
| 1164 bitdepth = int(mode[1]) |
| 1165 except: |
| 1166 raise Error() |
| 1167 except Error: |
| 1168 raise Error("mode string should be 'RGB' or 'L;16' or similar.") |
| 1169 mode = mode[0] |
| 1170 |
| 1171 # Get bitdepth from *mode* if possible. |
| 1172 if bitdepth: |
| 1173 if info.get('bitdepth') and bitdepth != info['bitdepth']: |
| 1174 raise Error("mode bitdepth (%d) should match info bitdepth (%d)." % |
| 1175 (bitdepth, info['bitdepth'])) |
| 1176 info['bitdepth'] = bitdepth |
| 1177 |
| 1178 # Fill in and/or check entries in *info*. |
| 1179 # Dimensions. |
| 1180 if 'size' in info: |
| 1181 # Check width, height, size all match where used. |
| 1182 for dimension,axis in [('width', 0), ('height', 1)]: |
| 1183 if dimension in info: |
| 1184 if info[dimension] != info['size'][axis]: |
| 1185 raise Error( |
| 1186 "info[%r] shhould match info['size'][%r]." % |
| 1187 (dimension, axis)) |
| 1188 info['width'],info['height'] = info['size'] |
| 1189 if 'height' not in info: |
| 1190 try: |
| 1191 l = len(a) |
| 1192 except: |
| 1193 raise Error( |
| 1194 "len(a) does not work, supply info['height'] instead.") |
| 1195 info['height'] = l |
| 1196 # Colour format. |
| 1197 if 'greyscale' in info: |
| 1198 if bool(info['greyscale']) != ('L' in mode): |
| 1199 raise Error("info['greyscale'] should match mode.") |
| 1200 info['greyscale'] = 'L' in mode |
| 1201 if 'alpha' in info: |
| 1202 if bool(info['alpha']) != ('A' in mode): |
| 1203 raise Error("info['alpha'] should match mode.") |
| 1204 info['alpha'] = 'A' in mode |
| 1205 |
| 1206 planes = len(mode) |
| 1207 if 'planes' in info: |
| 1208 if info['planes'] != planes: |
| 1209 raise Error("info['planes'] should match mode.") |
| 1210 |
| 1211 # In order to work out whether we the array is 2D or 3D we need its |
| 1212 # first row, which requires that we take a copy of its iterator. |
| 1213 # We may also need the first row to derive width and bitdepth. |
| 1214 a,t = itertools.tee(a) |
| 1215 row = t.next() |
| 1216 del t |
| 1217 try: |
| 1218 row[0][0] |
| 1219 threed = True |
| 1220 testelement = row[0] |
| 1221 except: |
| 1222 threed = False |
| 1223 testelement = row |
| 1224 if 'width' not in info: |
| 1225 if threed: |
| 1226 width = len(row) |
| 1227 else: |
| 1228 width = len(row) // planes |
| 1229 info['width'] = width |
| 1230 |
| 1231 # Not implemented yet |
| 1232 assert not threed |
| 1233 |
| 1234 if 'bitdepth' not in info: |
| 1235 try: |
| 1236 dtype = testelement.dtype |
| 1237 # goto the "else:" clause. Sorry. |
| 1238 except: |
| 1239 try: |
| 1240 # Try a Python array.array. |
| 1241 bitdepth = 8 * testelement.itemsize |
| 1242 except: |
| 1243 # We can't determine it from the array element's |
| 1244 # datatype, use a default of 8. |
| 1245 bitdepth = 8 |
| 1246 else: |
| 1247 # If we got here without exception, we now assume that |
| 1248 # the array is a numpy array. |
| 1249 if dtype.kind == 'b': |
| 1250 bitdepth = 1 |
| 1251 else: |
| 1252 bitdepth = 8 * dtype.itemsize |
| 1253 info['bitdepth'] = bitdepth |
| 1254 |
| 1255 for thing in 'width height bitdepth greyscale alpha'.split(): |
| 1256 assert thing in info |
| 1257 return Image(a, info) |
| 1258 |
| 1259 # So that refugee's from PIL feel more at home. Not documented. |
| 1260 fromarray = from_array |
| 1261 |
| 1262 class Image: |
| 1263 """A PNG image. |
| 1264 You can create an :class:`Image` object from an array of pixels by calling |
| 1265 :meth:`png.from_array`. It can be saved to disk with the |
| 1266 :meth:`save` method.""" |
| 1267 def __init__(self, rows, info): |
| 1268 """ |
| 1269 .. note :: |
| 1270 |
| 1271 The constructor is not public. Please do not call it. |
| 1272 """ |
| 1273 |
| 1274 self.rows = rows |
| 1275 self.info = info |
| 1276 |
| 1277 def save(self, file): |
| 1278 """Save the image to *file*. If *file* looks like an open file |
| 1279 descriptor then it is used, otherwise it is treated as a |
| 1280 filename and a fresh file is opened. |
| 1281 |
| 1282 In general, you can only call this method once; after it has |
| 1283 been called the first time and the PNG image has been saved, the |
| 1284 source data will have been streamed, and cannot be streamed |
| 1285 again. |
| 1286 """ |
| 1287 |
| 1288 w = Writer(**self.info) |
| 1289 |
| 1290 try: |
| 1291 file.write |
| 1292 def close(): pass |
| 1293 except: |
| 1294 file = open(file, 'wb') |
| 1295 def close(): file.close() |
| 1296 |
| 1297 try: |
| 1298 w.write(file, self.rows) |
| 1299 finally: |
| 1300 close() |
| 1301 |
| 1302 class _readable: |
| 1303 """ |
| 1304 A simple file-like interface for strings and arrays. |
| 1305 """ |
| 1306 |
| 1307 def __init__(self, buf): |
| 1308 self.buf = buf |
| 1309 self.offset = 0 |
| 1310 |
| 1311 def read(self, n): |
| 1312 r = self.buf[self.offset:self.offset+n] |
| 1313 if isarray(r): |
| 1314 r = r.tostring() |
| 1315 self.offset += n |
| 1316 return r |
| 1317 |
| 1318 |
| 1319 class Reader: |
| 1320 """ |
| 1321 PNG decoder in pure Python. |
| 1322 """ |
| 1323 |
| 1324 def __init__(self, _guess=None, **kw): |
| 1325 """ |
| 1326 Create a PNG decoder object. |
| 1327 |
| 1328 The constructor expects exactly one keyword argument. If you |
| 1329 supply a positional argument instead, it will guess the input |
| 1330 type. You can choose among the following keyword arguments: |
| 1331 |
| 1332 filename |
| 1333 Name of input file (a PNG file). |
| 1334 file |
| 1335 A file-like object (object with a read() method). |
| 1336 bytes |
| 1337 ``array`` or ``string`` with PNG data. |
| 1338 |
| 1339 """ |
| 1340 if ((_guess is not None and len(kw) != 0) or |
| 1341 (_guess is None and len(kw) != 1)): |
| 1342 raise TypeError("Reader() takes exactly 1 argument") |
| 1343 |
| 1344 # Will be the first 8 bytes, later on. See validate_signature. |
| 1345 self.signature = None |
| 1346 self.transparent = None |
| 1347 # A pair of (len,type) if a chunk has been read but its data and |
| 1348 # checksum have not (in other words the file position is just |
| 1349 # past the 4 bytes that specify the chunk type). See preamble |
| 1350 # method for how this is used. |
| 1351 self.atchunk = None |
| 1352 |
| 1353 if _guess is not None: |
| 1354 if isarray(_guess): |
| 1355 kw["bytes"] = _guess |
| 1356 elif isinstance(_guess, str): |
| 1357 kw["filename"] = _guess |
| 1358 elif isinstance(_guess, file): |
| 1359 kw["file"] = _guess |
| 1360 |
| 1361 if "filename" in kw: |
| 1362 self.file = open(kw["filename"], "rb") |
| 1363 elif "file" in kw: |
| 1364 self.file = kw["file"] |
| 1365 elif "bytes" in kw: |
| 1366 self.file = _readable(kw["bytes"]) |
| 1367 else: |
| 1368 raise TypeError("expecting filename, file or bytes array") |
| 1369 |
| 1370 |
| 1371 def chunk(self, seek=None, lenient=False): |
| 1372 """ |
| 1373 Read the next PNG chunk from the input file; returns a |
| 1374 (*type*,*data*) tuple. *type* is the chunk's type as a string |
| 1375 (all PNG chunk types are 4 characters long). *data* is the |
| 1376 chunk's data content, as a string. |
| 1377 |
| 1378 If the optional `seek` argument is |
| 1379 specified then it will keep reading chunks until it either runs |
| 1380 out of file or finds the type specified by the argument. Note |
| 1381 that in general the order of chunks in PNGs is unspecified, so |
| 1382 using `seek` can cause you to miss chunks. |
| 1383 |
| 1384 If the optional `lenient` argument evaluates to True, |
| 1385 checksum failures will raise warnings rather than exceptions. |
| 1386 """ |
| 1387 |
| 1388 self.validate_signature() |
| 1389 |
| 1390 while True: |
| 1391 # http://www.w3.org/TR/PNG/#5Chunk-layout |
| 1392 if not self.atchunk: |
| 1393 self.atchunk = self.chunklentype() |
| 1394 length,type = self.atchunk |
| 1395 self.atchunk = None |
| 1396 data = self.file.read(length) |
| 1397 if len(data) != length: |
| 1398 raise ChunkError('Chunk %s too short for required %i octets.' |
| 1399 % (type, length)) |
| 1400 checksum = self.file.read(4) |
| 1401 if len(checksum) != 4: |
| 1402 raise ValueError('Chunk %s too short for checksum.', tag) |
| 1403 if seek and type != seek: |
| 1404 continue |
| 1405 verify = zlib.crc32(strtobytes(type)) |
| 1406 verify = zlib.crc32(data, verify) |
| 1407 # Whether the output from zlib.crc32 is signed or not varies |
| 1408 # according to hideous implementation details, see |
| 1409 # http://bugs.python.org/issue1202 . |
| 1410 # We coerce it to be positive here (in a way which works on |
| 1411 # Python 2.3 and older). |
| 1412 verify &= 2**32 - 1 |
| 1413 verify = struct.pack('!I', verify) |
| 1414 if checksum != verify: |
| 1415 # print repr(checksum) |
| 1416 (a, ) = struct.unpack('!I', checksum) |
| 1417 (b, ) = struct.unpack('!I', verify) |
| 1418 message = "Checksum error in %s chunk: 0x%08X != 0x%08X." % (typ
e, a, b) |
| 1419 if lenient: |
| 1420 warnings.warn(message, RuntimeWarning) |
| 1421 else: |
| 1422 raise ChunkError(message) |
| 1423 return type, data |
| 1424 |
| 1425 def chunks(self): |
| 1426 """Return an iterator that will yield each chunk as a |
| 1427 (*chunktype*, *content*) pair. |
| 1428 """ |
| 1429 |
| 1430 while True: |
| 1431 t,v = self.chunk() |
| 1432 yield t,v |
| 1433 if t == 'IEND': |
| 1434 break |
| 1435 |
| 1436 def undo_filter(self, filter_type, scanline, previous): |
| 1437 """Undo the filter for a scanline. `scanline` is a sequence of |
| 1438 bytes that does not include the initial filter type byte. |
| 1439 `previous` is decoded previous scanline (for straightlaced |
| 1440 images this is the previous pixel row, but for interlaced |
| 1441 images, it is the previous scanline in the reduced image, which |
| 1442 in general is not the previous pixel row in the final image). |
| 1443 When there is no previous scanline (the first row of a |
| 1444 straightlaced image, or the first row in one of the passes in an |
| 1445 interlaced image), then this argument should be ``None``. |
| 1446 |
| 1447 The scanline will have the effects of filtering removed, and the |
| 1448 result will be returned as a fresh sequence of bytes. |
| 1449 """ |
| 1450 |
| 1451 # :todo: Would it be better to update scanline in place? |
| 1452 |
| 1453 # Create the result byte array. It seems that the best way to |
| 1454 # create the array to be the right size is to copy from an |
| 1455 # existing sequence. *sigh* |
| 1456 # If we fill the result with scanline, then this allows a |
| 1457 # micro-optimisation in the "null" and "sub" cases. |
| 1458 result = array('B', scanline) |
| 1459 |
| 1460 if filter_type == 0: |
| 1461 # And here, we _rely_ on filling the result with scanline, |
| 1462 # above. |
| 1463 return result |
| 1464 |
| 1465 if filter_type not in (1,2,3,4): |
| 1466 raise FormatError('Invalid PNG Filter Type.' |
| 1467 ' See http://www.w3.org/TR/2003/REC-PNG-20031110/#9Filters .') |
| 1468 |
| 1469 # Filter unit. The stride from one pixel to the corresponding |
| 1470 # byte from the previous previous. Normally this is the pixel |
| 1471 # size in bytes, but when this is smaller than 1, the previous |
| 1472 # byte is used instead. |
| 1473 fu = max(1, self.psize) |
| 1474 |
| 1475 # For the first line of a pass, synthesize a dummy previous |
| 1476 # line. An alternative approach would be to observe that on the |
| 1477 # first line 'up' is the same as 'null', 'paeth' is the same |
| 1478 # as 'sub', with only 'average' requiring any special case. |
| 1479 if not previous: |
| 1480 previous = array('B', [0]*len(scanline)) |
| 1481 |
| 1482 def sub(): |
| 1483 """Undo sub filter.""" |
| 1484 |
| 1485 ai = 0 |
| 1486 # Loops starts at index fu. Observe that the initial part |
| 1487 # of the result is already filled in correctly with |
| 1488 # scanline. |
| 1489 for i in range(fu, len(result)): |
| 1490 x = scanline[i] |
| 1491 a = result[ai] |
| 1492 result[i] = (x + a) & 0xff |
| 1493 ai += 1 |
| 1494 |
| 1495 def up(): |
| 1496 """Undo up filter.""" |
| 1497 |
| 1498 for i in range(len(result)): |
| 1499 x = scanline[i] |
| 1500 b = previous[i] |
| 1501 result[i] = (x + b) & 0xff |
| 1502 |
| 1503 def average(): |
| 1504 """Undo average filter.""" |
| 1505 |
| 1506 ai = -fu |
| 1507 for i in range(len(result)): |
| 1508 x = scanline[i] |
| 1509 if ai < 0: |
| 1510 a = 0 |
| 1511 else: |
| 1512 a = result[ai] |
| 1513 b = previous[i] |
| 1514 result[i] = (x + ((a + b) >> 1)) & 0xff |
| 1515 ai += 1 |
| 1516 |
| 1517 def paeth(): |
| 1518 """Undo Paeth filter.""" |
| 1519 |
| 1520 # Also used for ci. |
| 1521 ai = -fu |
| 1522 for i in range(len(result)): |
| 1523 x = scanline[i] |
| 1524 if ai < 0: |
| 1525 a = c = 0 |
| 1526 else: |
| 1527 a = result[ai] |
| 1528 c = previous[ai] |
| 1529 b = previous[i] |
| 1530 p = a + b - c |
| 1531 pa = abs(p - a) |
| 1532 pb = abs(p - b) |
| 1533 pc = abs(p - c) |
| 1534 if pa <= pb and pa <= pc: |
| 1535 pr = a |
| 1536 elif pb <= pc: |
| 1537 pr = b |
| 1538 else: |
| 1539 pr = c |
| 1540 result[i] = (x + pr) & 0xff |
| 1541 ai += 1 |
| 1542 |
| 1543 # Call appropriate filter algorithm. Note that 0 has already |
| 1544 # been dealt with. |
| 1545 (None, sub, up, average, paeth)[filter_type]() |
| 1546 return result |
| 1547 |
| 1548 def deinterlace(self, raw): |
| 1549 """ |
| 1550 Read raw pixel data, undo filters, deinterlace, and flatten. |
| 1551 Return in flat row flat pixel format. |
| 1552 """ |
| 1553 |
| 1554 # print >> sys.stderr, ("Reading interlaced, w=%s, r=%s, planes=%s," + |
| 1555 # " bpp=%s") % (self.width, self.height, self.planes, self.bps) |
| 1556 # Values per row (of the target image) |
| 1557 vpr = self.width * self.planes |
| 1558 |
| 1559 # Make a result array, and make it big enough. Interleaving |
| 1560 # writes to the output array randomly (well, not quite), so the |
| 1561 # entire output array must be in memory. |
| 1562 fmt = 'BH'[self.bitdepth > 8] |
| 1563 a = array(fmt, [0]*vpr*self.height) |
| 1564 source_offset = 0 |
| 1565 |
| 1566 for xstart, ystart, xstep, ystep in _adam7: |
| 1567 # print >> sys.stderr, "Adam7: start=%s,%s step=%s,%s" % ( |
| 1568 # xstart, ystart, xstep, ystep) |
| 1569 if xstart >= self.width: |
| 1570 continue |
| 1571 # The previous (reconstructed) scanline. None at the |
| 1572 # beginning of a pass to indicate that there is no previous |
| 1573 # line. |
| 1574 recon = None |
| 1575 # Pixels per row (reduced pass image) |
| 1576 ppr = int(math.ceil((self.width-xstart)/float(xstep))) |
| 1577 # Row size in bytes for this pass. |
| 1578 row_size = int(math.ceil(self.psize * ppr)) |
| 1579 for y in range(ystart, self.height, ystep): |
| 1580 filter_type = raw[source_offset] |
| 1581 source_offset += 1 |
| 1582 scanline = raw[source_offset:source_offset+row_size] |
| 1583 source_offset += row_size |
| 1584 recon = self.undo_filter(filter_type, scanline, recon) |
| 1585 # Convert so that there is one element per pixel value |
| 1586 flat = self.serialtoflat(recon, ppr) |
| 1587 if xstep == 1: |
| 1588 assert xstart == 0 |
| 1589 offset = y * vpr |
| 1590 a[offset:offset+vpr] = flat |
| 1591 else: |
| 1592 offset = y * vpr + xstart * self.planes |
| 1593 end_offset = (y+1) * vpr |
| 1594 skip = self.planes * xstep |
| 1595 for i in range(self.planes): |
| 1596 a[offset+i:end_offset:skip] = \ |
| 1597 flat[i::self.planes] |
| 1598 return a |
| 1599 |
| 1600 def iterboxed(self, rows): |
| 1601 """Iterator that yields each scanline in boxed row flat pixel |
| 1602 format. `rows` should be an iterator that yields the bytes of |
| 1603 each row in turn. |
| 1604 """ |
| 1605 |
| 1606 def asvalues(raw): |
| 1607 """Convert a row of raw bytes into a flat row. Result may |
| 1608 or may not share with argument""" |
| 1609 |
| 1610 if self.bitdepth == 8: |
| 1611 return raw |
| 1612 if self.bitdepth == 16: |
| 1613 raw = tostring(raw) |
| 1614 return array('H', struct.unpack('!%dH' % (len(raw)//2), raw)) |
| 1615 assert self.bitdepth < 8 |
| 1616 width = self.width |
| 1617 # Samples per byte |
| 1618 spb = 8//self.bitdepth |
| 1619 out = array('B') |
| 1620 mask = 2**self.bitdepth - 1 |
| 1621 shifts = map(self.bitdepth.__mul__, reversed(range(spb))) |
| 1622 for o in raw: |
| 1623 out.extend(map(lambda i: mask&(o>>i), shifts)) |
| 1624 return out[:width] |
| 1625 |
| 1626 return itertools.imap(asvalues, rows) |
| 1627 |
| 1628 def serialtoflat(self, bytes, width=None): |
| 1629 """Convert serial format (byte stream) pixel data to flat row |
| 1630 flat pixel. |
| 1631 """ |
| 1632 |
| 1633 if self.bitdepth == 8: |
| 1634 return bytes |
| 1635 if self.bitdepth == 16: |
| 1636 bytes = tostring(bytes) |
| 1637 return array('H', |
| 1638 struct.unpack('!%dH' % (len(bytes)//2), bytes)) |
| 1639 assert self.bitdepth < 8 |
| 1640 if width is None: |
| 1641 width = self.width |
| 1642 # Samples per byte |
| 1643 spb = 8//self.bitdepth |
| 1644 out = array('B') |
| 1645 mask = 2**self.bitdepth - 1 |
| 1646 shifts = map(self.bitdepth.__mul__, reversed(range(spb))) |
| 1647 l = width |
| 1648 for o in bytes: |
| 1649 out.extend([(mask&(o>>s)) for s in shifts][:l]) |
| 1650 l -= spb |
| 1651 if l <= 0: |
| 1652 l = width |
| 1653 return out |
| 1654 |
| 1655 def iterstraight(self, raw): |
| 1656 """Iterator that undoes the effect of filtering, and yields each |
| 1657 row in serialised format (as a sequence of bytes). Assumes input |
| 1658 is straightlaced. `raw` should be an iterable that yields the |
| 1659 raw bytes in chunks of arbitrary size.""" |
| 1660 |
| 1661 # length of row, in bytes |
| 1662 rb = self.row_bytes |
| 1663 a = array('B') |
| 1664 # The previous (reconstructed) scanline. None indicates first |
| 1665 # line of image. |
| 1666 recon = None |
| 1667 for some in raw: |
| 1668 a.extend(some) |
| 1669 while len(a) >= rb + 1: |
| 1670 filter_type = a[0] |
| 1671 scanline = a[1:rb+1] |
| 1672 del a[:rb+1] |
| 1673 recon = self.undo_filter(filter_type, scanline, recon) |
| 1674 yield recon |
| 1675 if len(a) != 0: |
| 1676 # :file:format We get here with a file format error: when the |
| 1677 # available bytes (after decompressing) do not pack into exact |
| 1678 # rows. |
| 1679 raise FormatError( |
| 1680 'Wrong size for decompressed IDAT chunk.') |
| 1681 assert len(a) == 0 |
| 1682 |
| 1683 def validate_signature(self): |
| 1684 """If signature (header) has not been read then read and |
| 1685 validate it; otherwise do nothing. |
| 1686 """ |
| 1687 |
| 1688 if self.signature: |
| 1689 return |
| 1690 self.signature = self.file.read(8) |
| 1691 if self.signature != _signature: |
| 1692 raise FormatError("PNG file has invalid signature.") |
| 1693 |
| 1694 def preamble(self, lenient=False): |
| 1695 """ |
| 1696 Extract the image metadata by reading the initial part of the PNG |
| 1697 file up to the start of the ``IDAT`` chunk. All the chunks that |
| 1698 precede the ``IDAT`` chunk are read and either processed for |
| 1699 metadata or discarded. |
| 1700 |
| 1701 If the optional `lenient` argument evaluates to True, |
| 1702 checksum failures will raise warnings rather than exceptions. |
| 1703 """ |
| 1704 |
| 1705 self.validate_signature() |
| 1706 |
| 1707 while True: |
| 1708 if not self.atchunk: |
| 1709 self.atchunk = self.chunklentype() |
| 1710 if self.atchunk is None: |
| 1711 raise FormatError( |
| 1712 'This PNG file has no IDAT chunks.') |
| 1713 if self.atchunk[1] == 'IDAT': |
| 1714 return |
| 1715 self.process_chunk(lenient=lenient) |
| 1716 |
| 1717 def chunklentype(self): |
| 1718 """Reads just enough of the input to determine the next |
| 1719 chunk's length and type, returned as a (*length*, *type*) pair |
| 1720 where *type* is a string. If there are no more chunks, ``None`` |
| 1721 is returned. |
| 1722 """ |
| 1723 |
| 1724 x = self.file.read(8) |
| 1725 if not x: |
| 1726 return None |
| 1727 if len(x) != 8: |
| 1728 raise FormatError( |
| 1729 'End of file whilst reading chunk length and type.') |
| 1730 length,type = struct.unpack('!I4s', x) |
| 1731 type = bytestostr(type) |
| 1732 if length > 2**31-1: |
| 1733 raise FormatError('Chunk %s is too large: %d.' % (type,length)) |
| 1734 return length,type |
| 1735 |
| 1736 def process_chunk(self, lenient=False): |
| 1737 """Process the next chunk and its data. This only processes the |
| 1738 following chunk types, all others are ignored: ``IHDR``, |
| 1739 ``PLTE``, ``bKGD``, ``tRNS``, ``gAMA``, ``sBIT``. |
| 1740 |
| 1741 If the optional `lenient` argument evaluates to True, |
| 1742 checksum failures will raise warnings rather than exceptions. |
| 1743 """ |
| 1744 |
| 1745 type, data = self.chunk(lenient=lenient) |
| 1746 if type == 'IHDR': |
| 1747 # http://www.w3.org/TR/PNG/#11IHDR |
| 1748 if len(data) != 13: |
| 1749 raise FormatError('IHDR chunk has incorrect length.') |
| 1750 (self.width, self.height, self.bitdepth, self.color_type, |
| 1751 self.compression, self.filter, |
| 1752 self.interlace) = struct.unpack("!2I5B", data) |
| 1753 |
| 1754 # Check that the header specifies only valid combinations. |
| 1755 if self.bitdepth not in (1,2,4,8,16): |
| 1756 raise Error("invalid bit depth %d" % self.bitdepth) |
| 1757 if self.color_type not in (0,2,3,4,6): |
| 1758 raise Error("invalid colour type %d" % self.color_type) |
| 1759 # Check indexed (palettized) images have 8 or fewer bits |
| 1760 # per pixel; check only indexed or greyscale images have |
| 1761 # fewer than 8 bits per pixel. |
| 1762 if ((self.color_type & 1 and self.bitdepth > 8) or |
| 1763 (self.bitdepth < 8 and self.color_type not in (0,3))): |
| 1764 raise FormatError("Illegal combination of bit depth (%d)" |
| 1765 " and colour type (%d)." |
| 1766 " See http://www.w3.org/TR/2003/REC-PNG-20031110/#table111 ." |
| 1767 % (self.bitdepth, self.color_type)) |
| 1768 if self.compression != 0: |
| 1769 raise Error("unknown compression method %d" % self.compression) |
| 1770 if self.filter != 0: |
| 1771 raise FormatError("Unknown filter method %d," |
| 1772 " see http://www.w3.org/TR/2003/REC-PNG-20031110/#9Filters ." |
| 1773 % self.filter) |
| 1774 if self.interlace not in (0,1): |
| 1775 raise FormatError("Unknown interlace method %d," |
| 1776 " see http://www.w3.org/TR/2003/REC-PNG-20031110/#8InterlaceMe
thods ." |
| 1777 % self.interlace) |
| 1778 |
| 1779 # Derived values |
| 1780 # http://www.w3.org/TR/PNG/#6Colour-values |
| 1781 colormap = bool(self.color_type & 1) |
| 1782 greyscale = not (self.color_type & 2) |
| 1783 alpha = bool(self.color_type & 4) |
| 1784 color_planes = (3,1)[greyscale or colormap] |
| 1785 planes = color_planes + alpha |
| 1786 |
| 1787 self.colormap = colormap |
| 1788 self.greyscale = greyscale |
| 1789 self.alpha = alpha |
| 1790 self.color_planes = color_planes |
| 1791 self.planes = planes |
| 1792 self.psize = float(self.bitdepth)/float(8) * planes |
| 1793 if int(self.psize) == self.psize: |
| 1794 self.psize = int(self.psize) |
| 1795 self.row_bytes = int(math.ceil(self.width * self.psize)) |
| 1796 # Stores PLTE chunk if present, and is used to check |
| 1797 # chunk ordering constraints. |
| 1798 self.plte = None |
| 1799 # Stores tRNS chunk if present, and is used to check chunk |
| 1800 # ordering constraints. |
| 1801 self.trns = None |
| 1802 # Stores sbit chunk if present. |
| 1803 self.sbit = None |
| 1804 elif type == 'PLTE': |
| 1805 # http://www.w3.org/TR/PNG/#11PLTE |
| 1806 if self.plte: |
| 1807 warnings.warn("Multiple PLTE chunks present.") |
| 1808 self.plte = data |
| 1809 if len(data) % 3 != 0: |
| 1810 raise FormatError( |
| 1811 "PLTE chunk's length should be a multiple of 3.") |
| 1812 if len(data) > (2**self.bitdepth)*3: |
| 1813 raise FormatError("PLTE chunk is too long.") |
| 1814 if len(data) == 0: |
| 1815 raise FormatError("Empty PLTE is not allowed.") |
| 1816 elif type == 'bKGD': |
| 1817 try: |
| 1818 if self.colormap: |
| 1819 if not self.plte: |
| 1820 warnings.warn( |
| 1821 "PLTE chunk is required before bKGD chunk.") |
| 1822 self.background = struct.unpack('B', data) |
| 1823 else: |
| 1824 self.background = struct.unpack("!%dH" % self.color_planes, |
| 1825 data) |
| 1826 except struct.error: |
| 1827 raise FormatError("bKGD chunk has incorrect length.") |
| 1828 elif type == 'tRNS': |
| 1829 # http://www.w3.org/TR/PNG/#11tRNS |
| 1830 self.trns = data |
| 1831 if self.colormap: |
| 1832 if not self.plte: |
| 1833 warnings.warn("PLTE chunk is required before tRNS chunk.") |
| 1834 else: |
| 1835 if len(data) > len(self.plte)/3: |
| 1836 # Was warning, but promoted to Error as it |
| 1837 # would otherwise cause pain later on. |
| 1838 raise FormatError("tRNS chunk is too long.") |
| 1839 else: |
| 1840 if self.alpha: |
| 1841 raise FormatError( |
| 1842 "tRNS chunk is not valid with colour type %d." % |
| 1843 self.color_type) |
| 1844 try: |
| 1845 self.transparent = \ |
| 1846 struct.unpack("!%dH" % self.color_planes, data) |
| 1847 except struct.error: |
| 1848 raise FormatError("tRNS chunk has incorrect length.") |
| 1849 elif type == 'gAMA': |
| 1850 try: |
| 1851 self.gamma = struct.unpack("!L", data)[0] / 100000.0 |
| 1852 except struct.error: |
| 1853 raise FormatError("gAMA chunk has incorrect length.") |
| 1854 elif type == 'sBIT': |
| 1855 self.sbit = data |
| 1856 if (self.colormap and len(data) != 3 or |
| 1857 not self.colormap and len(data) != self.planes): |
| 1858 raise FormatError("sBIT chunk has incorrect length.") |
| 1859 |
| 1860 def read(self, lenient=False): |
| 1861 """ |
| 1862 Read the PNG file and decode it. Returns (`width`, `height`, |
| 1863 `pixels`, `metadata`). |
| 1864 |
| 1865 May use excessive memory. |
| 1866 |
| 1867 `pixels` are returned in boxed row flat pixel format. |
| 1868 |
| 1869 If the optional `lenient` argument evaluates to True, |
| 1870 checksum failures will raise warnings rather than exceptions. |
| 1871 """ |
| 1872 |
| 1873 def iteridat(): |
| 1874 """Iterator that yields all the ``IDAT`` chunks as strings.""" |
| 1875 while True: |
| 1876 try: |
| 1877 type, data = self.chunk(lenient=lenient) |
| 1878 except ValueError, e: |
| 1879 raise ChunkError(e.args[0]) |
| 1880 if type == 'IEND': |
| 1881 # http://www.w3.org/TR/PNG/#11IEND |
| 1882 break |
| 1883 if type != 'IDAT': |
| 1884 continue |
| 1885 # type == 'IDAT' |
| 1886 # http://www.w3.org/TR/PNG/#11IDAT |
| 1887 if self.colormap and not self.plte: |
| 1888 warnings.warn("PLTE chunk is required before IDAT chunk") |
| 1889 yield data |
| 1890 |
| 1891 def iterdecomp(idat): |
| 1892 """Iterator that yields decompressed strings. `idat` should |
| 1893 be an iterator that yields the ``IDAT`` chunk data. |
| 1894 """ |
| 1895 |
| 1896 # Currently, with no max_length paramter to decompress, this |
| 1897 # routine will do one yield per IDAT chunk. So not very |
| 1898 # incremental. |
| 1899 d = zlib.decompressobj() |
| 1900 # Each IDAT chunk is passed to the decompressor, then any |
| 1901 # remaining state is decompressed out. |
| 1902 for data in idat: |
| 1903 # :todo: add a max_length argument here to limit output |
| 1904 # size. |
| 1905 yield array('B', d.decompress(data)) |
| 1906 yield array('B', d.flush()) |
| 1907 |
| 1908 self.preamble(lenient=lenient) |
| 1909 raw = iterdecomp(iteridat()) |
| 1910 |
| 1911 if self.interlace: |
| 1912 raw = array('B', itertools.chain(*raw)) |
| 1913 arraycode = 'BH'[self.bitdepth>8] |
| 1914 # Like :meth:`group` but producing an array.array object for |
| 1915 # each row. |
| 1916 pixels = itertools.imap(lambda *row: array(arraycode, row), |
| 1917 *[iter(self.deinterlace(raw))]*self.width*self.planes) |
| 1918 else: |
| 1919 pixels = self.iterboxed(self.iterstraight(raw)) |
| 1920 meta = dict() |
| 1921 for attr in 'greyscale alpha planes bitdepth interlace'.split(): |
| 1922 meta[attr] = getattr(self, attr) |
| 1923 meta['size'] = (self.width, self.height) |
| 1924 for attr in 'gamma transparent background'.split(): |
| 1925 a = getattr(self, attr, None) |
| 1926 if a is not None: |
| 1927 meta[attr] = a |
| 1928 return self.width, self.height, pixels, meta |
| 1929 |
| 1930 |
| 1931 def read_flat(self): |
| 1932 """ |
| 1933 Read a PNG file and decode it into flat row flat pixel format. |
| 1934 Returns (*width*, *height*, *pixels*, *metadata*). |
| 1935 |
| 1936 May use excessive memory. |
| 1937 |
| 1938 `pixels` are returned in flat row flat pixel format. |
| 1939 |
| 1940 See also the :meth:`read` method which returns pixels in the |
| 1941 more stream-friendly boxed row flat pixel format. |
| 1942 """ |
| 1943 |
| 1944 x, y, pixel, meta = self.read() |
| 1945 arraycode = 'BH'[meta['bitdepth']>8] |
| 1946 pixel = array(arraycode, itertools.chain(*pixel)) |
| 1947 return x, y, pixel, meta |
| 1948 |
| 1949 def palette(self, alpha='natural'): |
| 1950 """Returns a palette that is a sequence of 3-tuples or 4-tuples, |
| 1951 synthesizing it from the ``PLTE`` and ``tRNS`` chunks. These |
| 1952 chunks should have already been processed (for example, by |
| 1953 calling the :meth:`preamble` method). All the tuples are the |
| 1954 same size: 3-tuples if there is no ``tRNS`` chunk, 4-tuples when |
| 1955 there is a ``tRNS`` chunk. Assumes that the image is colour type |
| 1956 3 and therefore a ``PLTE`` chunk is required. |
| 1957 |
| 1958 If the `alpha` argument is ``'force'`` then an alpha channel is |
| 1959 always added, forcing the result to be a sequence of 4-tuples. |
| 1960 """ |
| 1961 |
| 1962 if not self.plte: |
| 1963 raise FormatError( |
| 1964 "Required PLTE chunk is missing in colour type 3 image.") |
| 1965 plte = group(array('B', self.plte), 3) |
| 1966 if self.trns or alpha == 'force': |
| 1967 trns = array('B', self.trns or '') |
| 1968 trns.extend([255]*(len(plte)-len(trns))) |
| 1969 plte = map(operator.add, plte, group(trns, 1)) |
| 1970 return plte |
| 1971 |
| 1972 def asDirect(self): |
| 1973 """Returns the image data as a direct representation of an |
| 1974 ``x * y * planes`` array. This method is intended to remove the |
| 1975 need for callers to deal with palettes and transparency |
| 1976 themselves. Images with a palette (colour type 3) |
| 1977 are converted to RGB or RGBA; images with transparency (a |
| 1978 ``tRNS`` chunk) are converted to LA or RGBA as appropriate. |
| 1979 When returned in this format the pixel values represent the |
| 1980 colour value directly without needing to refer to palettes or |
| 1981 transparency information. |
| 1982 |
| 1983 Like the :meth:`read` method this method returns a 4-tuple: |
| 1984 |
| 1985 (*width*, *height*, *pixels*, *meta*) |
| 1986 |
| 1987 This method normally returns pixel values with the bit depth |
| 1988 they have in the source image, but when the source PNG has an |
| 1989 ``sBIT`` chunk it is inspected and can reduce the bit depth of |
| 1990 the result pixels; pixel values will be reduced according to |
| 1991 the bit depth specified in the ``sBIT`` chunk (PNG nerds should |
| 1992 note a single result bit depth is used for all channels; the |
| 1993 maximum of the ones specified in the ``sBIT`` chunk. An RGB565 |
| 1994 image will be rescaled to 6-bit RGB666). |
| 1995 |
| 1996 The *meta* dictionary that is returned reflects the `direct` |
| 1997 format and not the original source image. For example, an RGB |
| 1998 source image with a ``tRNS`` chunk to represent a transparent |
| 1999 colour, will have ``planes=3`` and ``alpha=False`` for the |
| 2000 source image, but the *meta* dictionary returned by this method |
| 2001 will have ``planes=4`` and ``alpha=True`` because an alpha |
| 2002 channel is synthesized and added. |
| 2003 |
| 2004 *pixels* is the pixel data in boxed row flat pixel format (just |
| 2005 like the :meth:`read` method). |
| 2006 |
| 2007 All the other aspects of the image data are not changed. |
| 2008 """ |
| 2009 |
| 2010 self.preamble() |
| 2011 |
| 2012 # Simple case, no conversion necessary. |
| 2013 if not self.colormap and not self.trns and not self.sbit: |
| 2014 return self.read() |
| 2015 |
| 2016 x,y,pixels,meta = self.read() |
| 2017 |
| 2018 if self.colormap: |
| 2019 meta['colormap'] = False |
| 2020 meta['alpha'] = bool(self.trns) |
| 2021 meta['bitdepth'] = 8 |
| 2022 meta['planes'] = 3 + bool(self.trns) |
| 2023 plte = self.palette() |
| 2024 def iterpal(pixels): |
| 2025 for row in pixels: |
| 2026 row = map(plte.__getitem__, row) |
| 2027 yield array('B', itertools.chain(*row)) |
| 2028 pixels = iterpal(pixels) |
| 2029 elif self.trns: |
| 2030 # It would be nice if there was some reasonable way of doing |
| 2031 # this without generating a whole load of intermediate tuples. |
| 2032 # But tuples does seem like the easiest way, with no other way |
| 2033 # clearly much simpler or much faster. (Actually, the L to LA |
| 2034 # conversion could perhaps go faster (all those 1-tuples!), but |
| 2035 # I still wonder whether the code proliferation is worth it) |
| 2036 it = self.transparent |
| 2037 maxval = 2**meta['bitdepth']-1 |
| 2038 planes = meta['planes'] |
| 2039 meta['alpha'] = True |
| 2040 meta['planes'] += 1 |
| 2041 typecode = 'BH'[meta['bitdepth']>8] |
| 2042 def itertrns(pixels): |
| 2043 for row in pixels: |
| 2044 # For each row we group it into pixels, then form a |
| 2045 # characterisation vector that says whether each pixel |
| 2046 # is opaque or not. Then we convert True/False to |
| 2047 # 0/maxval (by multiplication), and add it as the extra |
| 2048 # channel. |
| 2049 row = group(row, planes) |
| 2050 opa = map(it.__ne__, row) |
| 2051 opa = map(maxval.__mul__, opa) |
| 2052 opa = zip(opa) # convert to 1-tuples |
| 2053 yield array(typecode, |
| 2054 itertools.chain(*map(operator.add, row, opa))) |
| 2055 pixels = itertrns(pixels) |
| 2056 targetbitdepth = None |
| 2057 if self.sbit: |
| 2058 sbit = struct.unpack('%dB' % len(self.sbit), self.sbit) |
| 2059 targetbitdepth = max(sbit) |
| 2060 if targetbitdepth > meta['bitdepth']: |
| 2061 raise Error('sBIT chunk %r exceeds bitdepth %d' % |
| 2062 (sbit,self.bitdepth)) |
| 2063 if min(sbit) <= 0: |
| 2064 raise Error('sBIT chunk %r has a 0-entry' % sbit) |
| 2065 if targetbitdepth == meta['bitdepth']: |
| 2066 targetbitdepth = None |
| 2067 if targetbitdepth: |
| 2068 shift = meta['bitdepth'] - targetbitdepth |
| 2069 meta['bitdepth'] = targetbitdepth |
| 2070 def itershift(pixels): |
| 2071 for row in pixels: |
| 2072 yield map(shift.__rrshift__, row) |
| 2073 pixels = itershift(pixels) |
| 2074 return x,y,pixels,meta |
| 2075 |
| 2076 def asFloat(self, maxval=1.0): |
| 2077 """Return image pixels as per :meth:`asDirect` method, but scale |
| 2078 all pixel values to be floating point values between 0.0 and |
| 2079 *maxval*. |
| 2080 """ |
| 2081 |
| 2082 x,y,pixels,info = self.asDirect() |
| 2083 sourcemaxval = 2**info['bitdepth']-1 |
| 2084 del info['bitdepth'] |
| 2085 info['maxval'] = float(maxval) |
| 2086 factor = float(maxval)/float(sourcemaxval) |
| 2087 def iterfloat(): |
| 2088 for row in pixels: |
| 2089 yield map(factor.__mul__, row) |
| 2090 return x,y,iterfloat(),info |
| 2091 |
| 2092 def _as_rescale(self, get, targetbitdepth): |
| 2093 """Helper used by :meth:`asRGB8` and :meth:`asRGBA8`.""" |
| 2094 |
| 2095 width,height,pixels,meta = get() |
| 2096 maxval = 2**meta['bitdepth'] - 1 |
| 2097 targetmaxval = 2**targetbitdepth - 1 |
| 2098 factor = float(targetmaxval) / float(maxval) |
| 2099 meta['bitdepth'] = targetbitdepth |
| 2100 def iterscale(): |
| 2101 for row in pixels: |
| 2102 yield map(lambda x: int(round(x*factor)), row) |
| 2103 return width, height, iterscale(), meta |
| 2104 |
| 2105 def asRGB8(self): |
| 2106 """Return the image data as an RGB pixels with 8-bits per |
| 2107 sample. This is like the :meth:`asRGB` method except that |
| 2108 this method additionally rescales the values so that they |
| 2109 are all between 0 and 255 (8-bit). In the case where the |
| 2110 source image has a bit depth < 8 the transformation preserves |
| 2111 all the information; where the source image has bit depth |
| 2112 > 8, then rescaling to 8-bit values loses precision. No |
| 2113 dithering is performed. Like :meth:`asRGB`, an alpha channel |
| 2114 in the source image will raise an exception. |
| 2115 |
| 2116 This function returns a 4-tuple: |
| 2117 (*width*, *height*, *pixels*, *metadata*). |
| 2118 *width*, *height*, *metadata* are as per the :meth:`read` method. |
| 2119 |
| 2120 *pixels* is the pixel data in boxed row flat pixel format. |
| 2121 """ |
| 2122 |
| 2123 return self._as_rescale(self.asRGB, 8) |
| 2124 |
| 2125 def asRGBA8(self): |
| 2126 """Return the image data as RGBA pixels with 8-bits per |
| 2127 sample. This method is similar to :meth:`asRGB8` and |
| 2128 :meth:`asRGBA`: The result pixels have an alpha channel, *and* |
| 2129 values are rescaled to the range 0 to 255. The alpha channel is |
| 2130 synthesized if necessary (with a small speed penalty). |
| 2131 """ |
| 2132 |
| 2133 return self._as_rescale(self.asRGBA, 8) |
| 2134 |
| 2135 def asRGB(self): |
| 2136 """Return image as RGB pixels. RGB colour images are passed |
| 2137 through unchanged; greyscales are expanded into RGB |
| 2138 triplets (there is a small speed overhead for doing this). |
| 2139 |
| 2140 An alpha channel in the source image will raise an |
| 2141 exception. |
| 2142 |
| 2143 The return values are as for the :meth:`read` method |
| 2144 except that the *metadata* reflect the returned pixels, not the |
| 2145 source image. In particular, for this method |
| 2146 ``metadata['greyscale']`` will be ``False``. |
| 2147 """ |
| 2148 |
| 2149 width,height,pixels,meta = self.asDirect() |
| 2150 if meta['alpha']: |
| 2151 raise Error("will not convert image with alpha channel to RGB") |
| 2152 if not meta['greyscale']: |
| 2153 return width,height,pixels,meta |
| 2154 meta['greyscale'] = False |
| 2155 typecode = 'BH'[meta['bitdepth'] > 8] |
| 2156 def iterrgb(): |
| 2157 for row in pixels: |
| 2158 a = array(typecode, [0]) * 3 * width |
| 2159 for i in range(3): |
| 2160 a[i::3] = row |
| 2161 yield a |
| 2162 return width,height,iterrgb(),meta |
| 2163 |
| 2164 def asRGBA(self): |
| 2165 """Return image as RGBA pixels. Greyscales are expanded into |
| 2166 RGB triplets; an alpha channel is synthesized if necessary. |
| 2167 The return values are as for the :meth:`read` method |
| 2168 except that the *metadata* reflect the returned pixels, not the |
| 2169 source image. In particular, for this method |
| 2170 ``metadata['greyscale']`` will be ``False``, and |
| 2171 ``metadata['alpha']`` will be ``True``. |
| 2172 """ |
| 2173 |
| 2174 width,height,pixels,meta = self.asDirect() |
| 2175 if meta['alpha'] and not meta['greyscale']: |
| 2176 return width,height,pixels,meta |
| 2177 typecode = 'BH'[meta['bitdepth'] > 8] |
| 2178 maxval = 2**meta['bitdepth'] - 1 |
| 2179 def newarray(): |
| 2180 return array(typecode, [0]) * 4 * width |
| 2181 if meta['alpha'] and meta['greyscale']: |
| 2182 # LA to RGBA |
| 2183 def convert(): |
| 2184 for row in pixels: |
| 2185 # Create a fresh target row, then copy L channel |
| 2186 # into first three target channels, and A channel |
| 2187 # into fourth channel. |
| 2188 a = newarray() |
| 2189 for i in range(3): |
| 2190 a[i::4] = row[0::2] |
| 2191 a[3::4] = row[1::2] |
| 2192 yield a |
| 2193 elif meta['greyscale']: |
| 2194 # L to RGBA |
| 2195 def convert(): |
| 2196 for row in pixels: |
| 2197 a = newarray() |
| 2198 for i in range(3): |
| 2199 a[i::4] = row |
| 2200 a[3::4] = array(typecode, [maxval]) * width |
| 2201 yield a |
| 2202 else: |
| 2203 assert not meta['alpha'] and not meta['greyscale'] |
| 2204 # RGB to RGBA |
| 2205 def convert(): |
| 2206 for row in pixels: |
| 2207 a = newarray() |
| 2208 for i in range(3): |
| 2209 a[i::4] = row[i::3] |
| 2210 a[3::4] = array(typecode, [maxval]) * width |
| 2211 yield a |
| 2212 meta['alpha'] = True |
| 2213 meta['greyscale'] = False |
| 2214 return width,height,convert(),meta |
| 2215 |
| 2216 |
| 2217 # === Legacy Version Support === |
| 2218 |
| 2219 # :pyver:old: PyPNG works on Python versions 2.3 and 2.2, but not |
| 2220 # without some awkward problems. Really PyPNG works on Python 2.4 (and |
| 2221 # above); it works on Pythons 2.3 and 2.2 by virtue of fixing up |
| 2222 # problems here. It's a bit ugly (which is why it's hidden down here). |
| 2223 # |
| 2224 # Generally the strategy is one of pretending that we're running on |
| 2225 # Python 2.4 (or above), and patching up the library support on earlier |
| 2226 # versions so that it looks enough like Python 2.4. When it comes to |
| 2227 # Python 2.2 there is one thing we cannot patch: extended slices |
| 2228 # http://www.python.org/doc/2.3/whatsnew/section-slices.html. |
| 2229 # Instead we simply declare that features that are implemented using |
| 2230 # extended slices will not work on Python 2.2. |
| 2231 # |
| 2232 # In order to work on Python 2.3 we fix up a recurring annoyance involving |
| 2233 # the array type. In Python 2.3 an array cannot be initialised with an |
| 2234 # array, and it cannot be extended with a list (or other sequence). |
| 2235 # Both of those are repeated issues in the code. Whilst I would not |
| 2236 # normally tolerate this sort of behaviour, here we "shim" a replacement |
| 2237 # for array into place (and hope no-ones notices). You never read this. |
| 2238 # |
| 2239 # In an amusing case of warty hacks on top of warty hacks... the array |
| 2240 # shimming we try and do only works on Python 2.3 and above (you can't |
| 2241 # subclass array.array in Python 2.2). So to get it working on Python |
| 2242 # 2.2 we go for something much simpler and (probably) way slower. |
| 2243 try: |
| 2244 array('B').extend([]) |
| 2245 array('B', array('B')) |
| 2246 except: |
| 2247 # Expect to get here on Python 2.3 |
| 2248 try: |
| 2249 class _array_shim(array): |
| 2250 true_array = array |
| 2251 def __new__(cls, typecode, init=None): |
| 2252 super_new = super(_array_shim, cls).__new__ |
| 2253 it = super_new(cls, typecode) |
| 2254 if init is None: |
| 2255 return it |
| 2256 it.extend(init) |
| 2257 return it |
| 2258 def extend(self, extension): |
| 2259 super_extend = super(_array_shim, self).extend |
| 2260 if isinstance(extension, self.true_array): |
| 2261 return super_extend(extension) |
| 2262 if not isinstance(extension, (list, str)): |
| 2263 # Convert to list. Allows iterators to work. |
| 2264 extension = list(extension) |
| 2265 return super_extend(self.true_array(self.typecode, extension)) |
| 2266 array = _array_shim |
| 2267 except: |
| 2268 # Expect to get here on Python 2.2 |
| 2269 def array(typecode, init=()): |
| 2270 if type(init) == str: |
| 2271 return map(ord, init) |
| 2272 return list(init) |
| 2273 |
| 2274 # Further hacks to get it limping along on Python 2.2 |
| 2275 try: |
| 2276 enumerate |
| 2277 except: |
| 2278 def enumerate(seq): |
| 2279 i=0 |
| 2280 for x in seq: |
| 2281 yield i,x |
| 2282 i += 1 |
| 2283 |
| 2284 try: |
| 2285 reversed |
| 2286 except: |
| 2287 def reversed(l): |
| 2288 l = list(l) |
| 2289 l.reverse() |
| 2290 for x in l: |
| 2291 yield x |
| 2292 |
| 2293 try: |
| 2294 itertools |
| 2295 except: |
| 2296 class _dummy_itertools: |
| 2297 pass |
| 2298 itertools = _dummy_itertools() |
| 2299 def _itertools_imap(f, seq): |
| 2300 for x in seq: |
| 2301 yield f(x) |
| 2302 itertools.imap = _itertools_imap |
| 2303 def _itertools_chain(*iterables): |
| 2304 for it in iterables: |
| 2305 for element in it: |
| 2306 yield element |
| 2307 itertools.chain = _itertools_chain |
| 2308 |
| 2309 |
| 2310 |
| 2311 # === Internal Test Support === |
| 2312 |
| 2313 # This section comprises the tests that are internally validated (as |
| 2314 # opposed to tests which produce output files that are externally |
| 2315 # validated). Primarily they are unittests. |
| 2316 |
| 2317 # Note that it is difficult to internally validate the results of |
| 2318 # writing a PNG file. The only thing we can do is read it back in |
| 2319 # again, which merely checks consistency, not that the PNG file we |
| 2320 # produce is valid. |
| 2321 |
| 2322 # Run the tests from the command line: |
| 2323 # python -c 'import png;png.test()' |
| 2324 |
| 2325 # (For an in-memory binary file IO object) We use BytesIO where |
| 2326 # available, otherwise we use StringIO, but name it BytesIO. |
| 2327 try: |
| 2328 from io import BytesIO |
| 2329 except: |
| 2330 from StringIO import StringIO as BytesIO |
| 2331 import tempfile |
| 2332 # http://www.python.org/doc/2.4.4/lib/module-unittest.html |
| 2333 import unittest |
| 2334 |
| 2335 |
| 2336 def test(): |
| 2337 unittest.main(__name__) |
| 2338 |
| 2339 def topngbytes(name, rows, x, y, **k): |
| 2340 """Convenience function for creating a PNG file "in memory" as a |
| 2341 string. Creates a :class:`Writer` instance using the keyword arguments, |
| 2342 then passes `rows` to its :meth:`Writer.write` method. The resulting |
| 2343 PNG file is returned as a string. `name` is used to identify the file for |
| 2344 debugging. |
| 2345 """ |
| 2346 |
| 2347 import os |
| 2348 |
| 2349 print name |
| 2350 f = BytesIO() |
| 2351 w = Writer(x, y, **k) |
| 2352 w.write(f, rows) |
| 2353 if os.environ.get('PYPNG_TEST_TMP'): |
| 2354 w = open(name, 'wb') |
| 2355 w.write(f.getvalue()) |
| 2356 w.close() |
| 2357 return f.getvalue() |
| 2358 |
| 2359 def testWithIO(inp, out, f): |
| 2360 """Calls the function `f` with ``sys.stdin`` changed to `inp` |
| 2361 and ``sys.stdout`` changed to `out`. They are restored when `f` |
| 2362 returns. This function returns whatever `f` returns. |
| 2363 """ |
| 2364 |
| 2365 import os |
| 2366 |
| 2367 try: |
| 2368 oldin,sys.stdin = sys.stdin,inp |
| 2369 oldout,sys.stdout = sys.stdout,out |
| 2370 x = f() |
| 2371 finally: |
| 2372 sys.stdin = oldin |
| 2373 sys.stdout = oldout |
| 2374 if os.environ.get('PYPNG_TEST_TMP') and hasattr(out,'getvalue'): |
| 2375 name = mycallersname() |
| 2376 if name: |
| 2377 w = open(name+'.png', 'wb') |
| 2378 w.write(out.getvalue()) |
| 2379 w.close() |
| 2380 return x |
| 2381 |
| 2382 def mycallersname(): |
| 2383 """Returns the name of the caller of the caller of this function |
| 2384 (hence the name of the caller of the function in which |
| 2385 "mycallersname()" textually appears). Returns None if this cannot |
| 2386 be determined.""" |
| 2387 |
| 2388 # http://docs.python.org/library/inspect.html#the-interpreter-stack |
| 2389 import inspect |
| 2390 |
| 2391 frame = inspect.currentframe() |
| 2392 if not frame: |
| 2393 return None |
| 2394 frame_,filename_,lineno_,funname,linelist_,listi_ = ( |
| 2395 inspect.getouterframes(frame)[2]) |
| 2396 return funname |
| 2397 |
| 2398 def seqtobytes(s): |
| 2399 """Convert a sequence of integers to a *bytes* instance. Good for |
| 2400 plastering over Python 2 / Python 3 cracks. |
| 2401 """ |
| 2402 |
| 2403 return strtobytes(''.join(chr(x) for x in s)) |
| 2404 |
| 2405 class Test(unittest.TestCase): |
| 2406 # This member is used by the superclass. If we don't define a new |
| 2407 # class here then when we use self.assertRaises() and the PyPNG code |
| 2408 # raises an assertion then we get no proper traceback. I can't work |
| 2409 # out why, but defining a new class here means we get a proper |
| 2410 # traceback. |
| 2411 class failureException(Exception): |
| 2412 pass |
| 2413 |
| 2414 def helperLN(self, n): |
| 2415 mask = (1 << n) - 1 |
| 2416 # Use small chunk_limit so that multiple chunk writing is |
| 2417 # tested. Making it a test for Issue 20. |
| 2418 w = Writer(15, 17, greyscale=True, bitdepth=n, chunk_limit=99) |
| 2419 f = BytesIO() |
| 2420 w.write_array(f, array('B', map(mask.__and__, range(1, 256)))) |
| 2421 r = Reader(bytes=f.getvalue()) |
| 2422 x,y,pixels,meta = r.read() |
| 2423 self.assertEqual(x, 15) |
| 2424 self.assertEqual(y, 17) |
| 2425 self.assertEqual(list(itertools.chain(*pixels)), |
| 2426 map(mask.__and__, range(1,256))) |
| 2427 def testL8(self): |
| 2428 return self.helperLN(8) |
| 2429 def testL4(self): |
| 2430 return self.helperLN(4) |
| 2431 def testL2(self): |
| 2432 "Also tests asRGB8." |
| 2433 w = Writer(1, 4, greyscale=True, bitdepth=2) |
| 2434 f = BytesIO() |
| 2435 w.write_array(f, array('B', range(4))) |
| 2436 r = Reader(bytes=f.getvalue()) |
| 2437 x,y,pixels,meta = r.asRGB8() |
| 2438 self.assertEqual(x, 1) |
| 2439 self.assertEqual(y, 4) |
| 2440 for i,row in enumerate(pixels): |
| 2441 self.assertEqual(len(row), 3) |
| 2442 self.assertEqual(list(row), [0x55*i]*3) |
| 2443 def testP2(self): |
| 2444 "2-bit palette." |
| 2445 a = (255,255,255) |
| 2446 b = (200,120,120) |
| 2447 c = (50,99,50) |
| 2448 w = Writer(1, 4, bitdepth=2, palette=[a,b,c]) |
| 2449 f = BytesIO() |
| 2450 w.write_array(f, array('B', (0,1,1,2))) |
| 2451 r = Reader(bytes=f.getvalue()) |
| 2452 x,y,pixels,meta = r.asRGB8() |
| 2453 self.assertEqual(x, 1) |
| 2454 self.assertEqual(y, 4) |
| 2455 self.assertEqual(list(pixels), map(list, [a, b, b, c])) |
| 2456 def testPtrns(self): |
| 2457 "Test colour type 3 and tRNS chunk (and 4-bit palette)." |
| 2458 a = (50,99,50,50) |
| 2459 b = (200,120,120,80) |
| 2460 c = (255,255,255) |
| 2461 d = (200,120,120) |
| 2462 e = (50,99,50) |
| 2463 w = Writer(3, 3, bitdepth=4, palette=[a,b,c,d,e]) |
| 2464 f = BytesIO() |
| 2465 w.write_array(f, array('B', (4, 3, 2, 3, 2, 0, 2, 0, 1))) |
| 2466 r = Reader(bytes=f.getvalue()) |
| 2467 x,y,pixels,meta = r.asRGBA8() |
| 2468 self.assertEqual(x, 3) |
| 2469 self.assertEqual(y, 3) |
| 2470 c = c+(255,) |
| 2471 d = d+(255,) |
| 2472 e = e+(255,) |
| 2473 boxed = [(e,d,c),(d,c,a),(c,a,b)] |
| 2474 flat = map(lambda row: itertools.chain(*row), boxed) |
| 2475 self.assertEqual(map(list, pixels), map(list, flat)) |
| 2476 def testRGBtoRGBA(self): |
| 2477 "asRGBA8() on colour type 2 source.""" |
| 2478 # Test for Issue 26 |
| 2479 r = Reader(bytes=_pngsuite['basn2c08']) |
| 2480 x,y,pixels,meta = r.asRGBA8() |
| 2481 # Test the pixels at row 9 columns 0 and 1. |
| 2482 row9 = list(pixels)[9] |
| 2483 self.assertEqual(row9[0:8], |
| 2484 [0xff, 0xdf, 0xff, 0xff, 0xff, 0xde, 0xff, 0xff]) |
| 2485 def testLtoRGBA(self): |
| 2486 "asRGBA() on grey source.""" |
| 2487 # Test for Issue 60 |
| 2488 r = Reader(bytes=_pngsuite['basi0g08']) |
| 2489 x,y,pixels,meta = r.asRGBA() |
| 2490 row9 = list(list(pixels)[9]) |
| 2491 self.assertEqual(row9[0:8], |
| 2492 [222, 222, 222, 255, 221, 221, 221, 255]) |
| 2493 def testCtrns(self): |
| 2494 "Test colour type 2 and tRNS chunk." |
| 2495 # Test for Issue 25 |
| 2496 r = Reader(bytes=_pngsuite['tbrn2c08']) |
| 2497 x,y,pixels,meta = r.asRGBA8() |
| 2498 # I just happen to know that the first pixel is transparent. |
| 2499 # In particular it should be #7f7f7f00 |
| 2500 row0 = list(pixels)[0] |
| 2501 self.assertEqual(tuple(row0[0:4]), (0x7f, 0x7f, 0x7f, 0x00)) |
| 2502 def testAdam7read(self): |
| 2503 """Adam7 interlace reading. |
| 2504 Specifically, test that for images in the PngSuite that |
| 2505 have both an interlaced and straightlaced pair that both |
| 2506 images from the pair produce the same array of pixels.""" |
| 2507 for candidate in _pngsuite: |
| 2508 if not candidate.startswith('basn'): |
| 2509 continue |
| 2510 candi = candidate.replace('n', 'i') |
| 2511 if candi not in _pngsuite: |
| 2512 continue |
| 2513 print 'adam7 read', candidate |
| 2514 straight = Reader(bytes=_pngsuite[candidate]) |
| 2515 adam7 = Reader(bytes=_pngsuite[candi]) |
| 2516 # Just compare the pixels. Ignore x,y (because they're |
| 2517 # likely to be correct?); metadata is ignored because the |
| 2518 # "interlace" member differs. Lame. |
| 2519 straight = straight.read()[2] |
| 2520 adam7 = adam7.read()[2] |
| 2521 self.assertEqual(map(list, straight), map(list, adam7)) |
| 2522 def testAdam7write(self): |
| 2523 """Adam7 interlace writing. |
| 2524 For each test image in the PngSuite, write an interlaced |
| 2525 and a straightlaced version. Decode both, and compare results. |
| 2526 """ |
| 2527 # Not such a great test, because the only way we can check what |
| 2528 # we have written is to read it back again. |
| 2529 |
| 2530 for name,bytes in _pngsuite.items(): |
| 2531 # Only certain colour types supported for this test. |
| 2532 if name[3:5] not in ['n0', 'n2', 'n4', 'n6']: |
| 2533 continue |
| 2534 it = Reader(bytes=bytes) |
| 2535 x,y,pixels,meta = it.read() |
| 2536 pngi = topngbytes('adam7wn'+name+'.png', pixels, |
| 2537 x=x, y=y, bitdepth=it.bitdepth, |
| 2538 greyscale=it.greyscale, alpha=it.alpha, |
| 2539 transparent=it.transparent, |
| 2540 interlace=False) |
| 2541 x,y,ps,meta = Reader(bytes=pngi).read() |
| 2542 it = Reader(bytes=bytes) |
| 2543 x,y,pixels,meta = it.read() |
| 2544 pngs = topngbytes('adam7wi'+name+'.png', pixels, |
| 2545 x=x, y=y, bitdepth=it.bitdepth, |
| 2546 greyscale=it.greyscale, alpha=it.alpha, |
| 2547 transparent=it.transparent, |
| 2548 interlace=True) |
| 2549 x,y,pi,meta = Reader(bytes=pngs).read() |
| 2550 self.assertEqual(map(list, ps), map(list, pi)) |
| 2551 def testPGMin(self): |
| 2552 """Test that the command line tool can read PGM files.""" |
| 2553 def do(): |
| 2554 return _main(['testPGMin']) |
| 2555 s = BytesIO() |
| 2556 s.write(strtobytes('P5 2 2 3\n')) |
| 2557 s.write(strtobytes('\x00\x01\x02\x03')) |
| 2558 s.flush() |
| 2559 s.seek(0) |
| 2560 o = BytesIO() |
| 2561 testWithIO(s, o, do) |
| 2562 r = Reader(bytes=o.getvalue()) |
| 2563 x,y,pixels,meta = r.read() |
| 2564 self.assertTrue(r.greyscale) |
| 2565 self.assertEqual(r.bitdepth, 2) |
| 2566 def testPAMin(self): |
| 2567 """Test that the command line tool can read PAM file.""" |
| 2568 def do(): |
| 2569 return _main(['testPAMin']) |
| 2570 s = BytesIO() |
| 2571 s.write(strtobytes('P7\nWIDTH 3\nHEIGHT 1\nDEPTH 4\nMAXVAL 255\n' |
| 2572 'TUPLTYPE RGB_ALPHA\nENDHDR\n')) |
| 2573 # The pixels in flat row flat pixel format |
| 2574 flat = [255,0,0,255, 0,255,0,120, 0,0,255,30] |
| 2575 asbytes = seqtobytes(flat) |
| 2576 s.write(asbytes) |
| 2577 s.flush() |
| 2578 s.seek(0) |
| 2579 o = BytesIO() |
| 2580 testWithIO(s, o, do) |
| 2581 r = Reader(bytes=o.getvalue()) |
| 2582 x,y,pixels,meta = r.read() |
| 2583 self.assertTrue(r.alpha) |
| 2584 self.assertTrue(not r.greyscale) |
| 2585 self.assertEqual(list(itertools.chain(*pixels)), flat) |
| 2586 def testLA4(self): |
| 2587 """Create an LA image with bitdepth 4.""" |
| 2588 bytes = topngbytes('la4.png', [[5, 12]], 1, 1, |
| 2589 greyscale=True, alpha=True, bitdepth=4) |
| 2590 sbit = Reader(bytes=bytes).chunk('sBIT')[1] |
| 2591 self.assertEqual(sbit, strtobytes('\x04\x04')) |
| 2592 def testPNMsbit(self): |
| 2593 """Test that PNM files can generates sBIT chunk.""" |
| 2594 def do(): |
| 2595 return _main(['testPNMsbit']) |
| 2596 s = BytesIO() |
| 2597 s.write(strtobytes('P6 8 1 1\n')) |
| 2598 for pixel in range(8): |
| 2599 s.write(struct.pack('<I', (0x4081*pixel)&0x10101)[:3]) |
| 2600 s.flush() |
| 2601 s.seek(0) |
| 2602 o = BytesIO() |
| 2603 testWithIO(s, o, do) |
| 2604 r = Reader(bytes=o.getvalue()) |
| 2605 sbit = r.chunk('sBIT')[1] |
| 2606 self.assertEqual(sbit, strtobytes('\x01\x01\x01')) |
| 2607 def testLtrns0(self): |
| 2608 """Create greyscale image with tRNS chunk.""" |
| 2609 return self.helperLtrns(0) |
| 2610 def testLtrns1(self): |
| 2611 """Using 1-tuple for transparent arg.""" |
| 2612 return self.helperLtrns((0,)) |
| 2613 def helperLtrns(self, transparent): |
| 2614 """Helper used by :meth:`testLtrns*`.""" |
| 2615 pixels = zip([0x00, 0x38, 0x4c, 0x54, 0x5c, 0x40, 0x38, 0x00]) |
| 2616 o = BytesIO() |
| 2617 w = Writer(8, 8, greyscale=True, bitdepth=1, transparent=transparent) |
| 2618 w.write_packed(o, pixels) |
| 2619 r = Reader(bytes=o.getvalue()) |
| 2620 x,y,pixels,meta = r.asDirect() |
| 2621 self.assertTrue(meta['alpha']) |
| 2622 self.assertTrue(meta['greyscale']) |
| 2623 self.assertEqual(meta['bitdepth'], 1) |
| 2624 def testWinfo(self): |
| 2625 """Test the dictionary returned by a `read` method can be used |
| 2626 as args for :meth:`Writer`. |
| 2627 """ |
| 2628 r = Reader(bytes=_pngsuite['basn2c16']) |
| 2629 info = r.read()[3] |
| 2630 w = Writer(**info) |
| 2631 def testPackedIter(self): |
| 2632 """Test iterator for row when using write_packed. |
| 2633 |
| 2634 Indicative for Issue 47. |
| 2635 """ |
| 2636 w = Writer(16, 2, greyscale=True, alpha=False, bitdepth=1) |
| 2637 o = BytesIO() |
| 2638 w.write_packed(o, [itertools.chain([0x0a], [0xaa]), |
| 2639 itertools.chain([0x0f], [0xff])]) |
| 2640 r = Reader(bytes=o.getvalue()) |
| 2641 x,y,pixels,info = r.asDirect() |
| 2642 pixels = list(pixels) |
| 2643 self.assertEqual(len(pixels), 2) |
| 2644 self.assertEqual(len(pixels[0]), 16) |
| 2645 def testInterlacedArray(self): |
| 2646 """Test that reading an interlaced PNG yields each row as an |
| 2647 array.""" |
| 2648 r = Reader(bytes=_pngsuite['basi0g08']) |
| 2649 list(r.read()[2])[0].tostring |
| 2650 def testTrnsArray(self): |
| 2651 """Test that reading a type 2 PNG with tRNS chunk yields each |
| 2652 row as an array (using asDirect).""" |
| 2653 r = Reader(bytes=_pngsuite['tbrn2c08']) |
| 2654 list(r.asDirect()[2])[0].tostring |
| 2655 |
| 2656 # Invalid file format tests. These construct various badly |
| 2657 # formatted PNG files, then feed them into a Reader. When |
| 2658 # everything is working properly, we should get FormatError |
| 2659 # exceptions raised. |
| 2660 def testEmpty(self): |
| 2661 """Test empty file.""" |
| 2662 |
| 2663 r = Reader(bytes='') |
| 2664 self.assertRaises(FormatError, r.asDirect) |
| 2665 def testSigOnly(self): |
| 2666 """Test file containing just signature bytes.""" |
| 2667 |
| 2668 r = Reader(bytes=_signature) |
| 2669 self.assertRaises(FormatError, r.asDirect) |
| 2670 def testExtraPixels(self): |
| 2671 """Test file that contains too many pixels.""" |
| 2672 |
| 2673 def eachchunk(chunk): |
| 2674 if chunk[0] != 'IDAT': |
| 2675 return chunk |
| 2676 data = zlib.decompress(chunk[1]) |
| 2677 data += strtobytes('\x00garbage') |
| 2678 data = zlib.compress(data) |
| 2679 chunk = (chunk[0], data) |
| 2680 return chunk |
| 2681 self.assertRaises(FormatError, self.helperFormat, eachchunk) |
| 2682 def testNotEnoughPixels(self): |
| 2683 def eachchunk(chunk): |
| 2684 if chunk[0] != 'IDAT': |
| 2685 return chunk |
| 2686 # Remove last byte. |
| 2687 data = zlib.decompress(chunk[1]) |
| 2688 data = data[:-1] |
| 2689 data = zlib.compress(data) |
| 2690 return (chunk[0], data) |
| 2691 self.assertRaises(FormatError, self.helperFormat, eachchunk) |
| 2692 def helperFormat(self, f): |
| 2693 r = Reader(bytes=_pngsuite['basn0g01']) |
| 2694 o = BytesIO() |
| 2695 def newchunks(): |
| 2696 for chunk in r.chunks(): |
| 2697 yield f(chunk) |
| 2698 write_chunks(o, newchunks()) |
| 2699 r = Reader(bytes=o.getvalue()) |
| 2700 return list(r.asDirect()[2]) |
| 2701 def testBadFilter(self): |
| 2702 def eachchunk(chunk): |
| 2703 if chunk[0] != 'IDAT': |
| 2704 return chunk |
| 2705 data = zlib.decompress(chunk[1]) |
| 2706 # Corrupt the first filter byte |
| 2707 data = strtobytes('\x99') + data[1:] |
| 2708 data = zlib.compress(data) |
| 2709 return (chunk[0], data) |
| 2710 self.assertRaises(FormatError, self.helperFormat, eachchunk) |
| 2711 def testFlat(self): |
| 2712 """Test read_flat.""" |
| 2713 import hashlib |
| 2714 |
| 2715 r = Reader(bytes=_pngsuite['basn0g02']) |
| 2716 x,y,pixel,meta = r.read_flat() |
| 2717 d = hashlib.md5(seqtobytes(pixel)).digest() |
| 2718 self.assertEqual(_enhex(d), '255cd971ab8cd9e7275ff906e5041aa0') |
| 2719 def testfromarray(self): |
| 2720 img = from_array([[0, 0x33, 0x66], [0xff, 0xcc, 0x99]], 'L') |
| 2721 img.save('testfromarray.png') |
| 2722 def testfromarrayL16(self): |
| 2723 img = from_array(group(range(2**16), 256), 'L;16') |
| 2724 img.save('testL16.png') |
| 2725 def testfromarrayRGB(self): |
| 2726 img = from_array([[0,0,0, 0,0,1, 0,1,0, 0,1,1], |
| 2727 [1,0,0, 1,0,1, 1,1,0, 1,1,1]], 'RGB;1') |
| 2728 o = BytesIO() |
| 2729 img.save(o) |
| 2730 def testfromarrayIter(self): |
| 2731 import itertools |
| 2732 |
| 2733 i = itertools.islice(itertools.count(10), 20) |
| 2734 i = itertools.imap(lambda x: [x, x, x], i) |
| 2735 img = from_array(i, 'RGB;5', dict(height=20)) |
| 2736 f = open('testiter.png', 'wb') |
| 2737 img.save(f) |
| 2738 f.close() |
| 2739 |
| 2740 # numpy dependent tests. These are skipped (with a message to |
| 2741 # sys.stderr) if numpy cannot be imported. |
| 2742 def testNumpyuint16(self): |
| 2743 """numpy uint16.""" |
| 2744 |
| 2745 try: |
| 2746 import numpy |
| 2747 except ImportError: |
| 2748 print >>sys.stderr, "skipping numpy test" |
| 2749 return |
| 2750 |
| 2751 rows = [map(numpy.uint16, range(0,0x10000,0x5555))] |
| 2752 b = topngbytes('numpyuint16.png', rows, 4, 1, |
| 2753 greyscale=True, alpha=False, bitdepth=16) |
| 2754 def testNumpyuint8(self): |
| 2755 """numpy uint8.""" |
| 2756 |
| 2757 try: |
| 2758 import numpy |
| 2759 except ImportError: |
| 2760 print >>sys.stderr, "skipping numpy test" |
| 2761 return |
| 2762 |
| 2763 rows = [map(numpy.uint8, range(0,0x100,0x55))] |
| 2764 b = topngbytes('numpyuint8.png', rows, 4, 1, |
| 2765 greyscale=True, alpha=False, bitdepth=8) |
| 2766 def testNumpybool(self): |
| 2767 """numpy bool.""" |
| 2768 |
| 2769 try: |
| 2770 import numpy |
| 2771 except ImportError: |
| 2772 print >>sys.stderr, "skipping numpy test" |
| 2773 return |
| 2774 |
| 2775 rows = [map(numpy.bool, [0,1])] |
| 2776 b = topngbytes('numpybool.png', rows, 2, 1, |
| 2777 greyscale=True, alpha=False, bitdepth=1) |
| 2778 def testNumpyarray(self): |
| 2779 """numpy array.""" |
| 2780 try: |
| 2781 import numpy |
| 2782 except ImportError: |
| 2783 print >>sys.stderr, "skipping numpy test" |
| 2784 return |
| 2785 |
| 2786 pixels = numpy.array([[0,0x5555],[0x5555,0xaaaa]], numpy.uint16) |
| 2787 img = from_array(pixels, 'L') |
| 2788 img.save('testnumpyL16.png') |
| 2789 |
| 2790 # === Command Line Support === |
| 2791 |
| 2792 def _dehex(s): |
| 2793 """Liberally convert from hex string to binary string.""" |
| 2794 import re |
| 2795 import binascii |
| 2796 |
| 2797 # Remove all non-hexadecimal digits |
| 2798 s = re.sub(r'[^a-fA-F\d]', '', s) |
| 2799 # binscii.unhexlify works in Python 2 and Python 3 (unlike |
| 2800 # thing.decode('hex')). |
| 2801 return binascii.unhexlify(strtobytes(s)) |
| 2802 def _enhex(s): |
| 2803 """Convert from binary string (bytes) to hex string (str).""" |
| 2804 |
| 2805 import binascii |
| 2806 |
| 2807 return bytestostr(binascii.hexlify(s)) |
| 2808 |
| 2809 # Copies of PngSuite test files taken |
| 2810 # from http://www.schaik.com/pngsuite/pngsuite_bas_png.html |
| 2811 # on 2009-02-19 by drj and converted to hex. |
| 2812 # Some of these are not actually in PngSuite (but maybe they should |
| 2813 # be?), they use the same naming scheme, but start with a capital |
| 2814 # letter. |
| 2815 _pngsuite = { |
| 2816 'basi0g01': _dehex(""" |
| 2817 89504e470d0a1a0a0000000d49484452000000200000002001000000012c0677 |
| 2818 cf0000000467414d41000186a031e8965f0000009049444154789c2d8d310ec2 |
| 2819 300c45dfc682c415187a00a42e197ab81e83b127e00c5639001363a580d8582c |
| 2820 65c910357c4b78b0bfbfdf4f70168c19e7acb970a3f2d1ded9695ce5bf5963df |
| 2821 d92aaf4c9fd927ea449e6487df5b9c36e799b91bdf082b4d4bd4014fe4014b01 |
| 2822 ab7a17aee694d28d328a2d63837a70451e1648702d9a9ff4a11d2f7a51aa21e5 |
| 2823 a18c7ffd0094e3511d661822f20000000049454e44ae426082 |
| 2824 """), |
| 2825 'basi0g02': _dehex(""" |
| 2826 89504e470d0a1a0a0000000d49484452000000200000002002000000016ba60d |
| 2827 1f0000000467414d41000186a031e8965f0000005149444154789c635062e860 |
| 2828 00e17286bb609c93c370ec189494960631366e4467b3ae675dcf10f521ea0303 |
| 2829 90c1ca006444e11643482064114a4852c710baea3f18c31918020c30410403a6 |
| 2830 0ac1a09239009c52804d85b6d97d0000000049454e44ae426082 |
| 2831 """), |
| 2832 'basi0g04': _dehex(""" |
| 2833 89504e470d0a1a0a0000000d4948445200000020000000200400000001e4e6f8 |
| 2834 bf0000000467414d41000186a031e8965f000000ae49444154789c658e5111c2 |
| 2835 301044171c141c141c041c843a287510ea20d441c041c141c141c04191102454 |
| 2836 03994998cecd7edcecedbb9bdbc3b2c2b6457545fbc4bac1be437347f7c66a77 |
| 2837 3c23d60db15e88f5c5627338a5416c2e691a9b475a89cd27eda12895ae8dfdab |
| 2838 43d61e590764f5c83a226b40d669bec307f93247701687723abf31ff83a2284b |
| 2839 a5b4ae6b63ac6520ad730ca4ed7b06d20e030369bd6720ed383290360406d24e |
| 2840 13811f2781eba9d34d07160000000049454e44ae426082 |
| 2841 """), |
| 2842 'basi0g08': _dehex(""" |
| 2843 89504e470d0a1a0a0000000d4948445200000020000000200800000001211615 |
| 2844 be0000000467414d41000186a031e8965f000000b549444154789cb5905d0ac2 |
| 2845 3010849dbac81c42c47bf843cf253e8878b0aa17110f214bdca6be240f5d21a5 |
| 2846 94ced3e49bcd322c1624115515154998aa424822a82a5624a1aa8a8b24c58f99 |
| 2847 999908130989a04a00d76c2c09e76cf21adcb209393a6553577da17140a2c59e |
| 2848 70ecbfa388dff1f03b82fb82bd07f05f7cb13f80bb07ad2fd60c011c3c588eef |
| 2849 f1f4e03bbec7ce832dca927aea005e431b625796345307b019c845e6bfc3bb98 |
| 2850 769d84f9efb02ea6c00f9bb9ff45e81f9f280000000049454e44ae426082 |
| 2851 """), |
| 2852 'basi0g16': _dehex(""" |
| 2853 89504e470d0a1a0a0000000d49484452000000200000002010000000017186c9 |
| 2854 fd0000000467414d41000186a031e8965f000000e249444154789cb5913b0ec2 |
| 2855 301044c7490aa8f85d81c3e4301c8f53a4ca0da8902c8144b3920b4043111282 |
| 2856 23bc4956681a6bf5fc3c5a3ba0448912d91a4de2c38dd8e380231eede4c4f7a1 |
| 2857 4677700bec7bd9b1d344689315a3418d1a6efbe5b8305ba01f8ff4808c063e26 |
| 2858 c60d5c81edcf6c58c535e252839e93801b15c0a70d810ae0d306b205dc32b187 |
| 2859 272b64057e4720ff0502154034831520154034c3df81400510cdf0015c86e5cc |
| 2860 5c79c639fddba9dcb5456b51d7980eb52d8e7d7fa620a75120d6064641a05120 |
| 2861 b606771a05626b401a05f1f589827cf0fe44c1f0bae0055698ee8914fffffe00 |
| 2862 00000049454e44ae426082 |
| 2863 """), |
| 2864 'basi2c08': _dehex(""" |
| 2865 89504e470d0a1a0a0000000d49484452000000200000002008020000018b1fdd |
| 2866 350000000467414d41000186a031e8965f000000f249444154789cd59341aa04 |
| 2867 210c44abc07b78133d59d37333bd89d76868b566d10cf4675af8596431a11662 |
| 2868 7c5688919280e312257dd6a0a4cf1a01008ee312a5f3c69c37e6fcc3f47e6776 |
| 2869 a07f8bdaf5b40feed2d33e025e2ff4fe2d4a63e1a16d91180b736d8bc45854c5 |
| 2870 6d951863f4a7e0b66dcf09a900f3ffa2948d4091e53ca86c048a64390f662b50 |
| 2871 4a999660ced906182b9a01a8be00a56404a6ede182b1223b4025e32c4de34304 |
| 2872 63457680c93aada6c99b73865aab2fc094920d901a203f5ddfe1970d28456783 |
| 2873 26cffbafeffcd30654f46d119be4793f827387fc0d189d5bc4d69a3c23d45a7f |
| 2874 db803146578337df4d0a3121fc3d330000000049454e44ae426082 |
| 2875 """), |
| 2876 'basi2c16': _dehex(""" |
| 2877 89504e470d0a1a0a0000000d4948445200000020000000201002000001db8f01 |
| 2878 760000000467414d41000186a031e8965f0000020a49444154789cd5962173e3 |
| 2879 3010853fcf1838cc61a1818185a53e56787fa13fa130852e3b5878b4b0b03081 |
| 2880 b97f7030070b53e6b057a0a8912bbb9163b9f109ececbc59bd7dcf2b45492409 |
| 2881 d66f00eb1dd83cb5497d65456aeb8e1040913b3b2c04504c936dd5a9c7e2c6eb |
| 2882 b1b8f17a58e8d043da56f06f0f9f62e5217b6ba3a1b76f6c9e99e8696a2a72e2 |
| 2883 c4fb1e4d452e92ec9652b807486d12b6669be00db38d9114b0c1961e375461a5 |
| 2884 5f76682a85c367ad6f682ff53a9c2a353191764b78bb07d8ddc3c97c1950f391 |
| 2885 6745c7b9852c73c2f212605a466a502705c8338069c8b9e84efab941eb393a97 |
| 2886 d4c9fd63148314209f1c1d3434e847ead6380de291d6f26a25c1ebb5047f5f24 |
| 2887 d85c49f0f22cc1d34282c72709cab90477bf25b89d49f0f351822297e0ea9704 |
| 2888 f34c82bc94002448ede51866e5656aef5d7c6a385cb4d80e6a538ceba04e6df2 |
| 2889 480e9aa84ddedb413bb5c97b3838456df2d4fec2c7a706983e7474d085fae820 |
| 2890 a841776a83073838973ac0413fea2f1dc4a06e71108fda73109bdae48954ad60 |
| 2891 bf867aac3ce44c7c1589a711cf8a81df9b219679d96d1cec3d8bbbeaa2012626 |
| 2892 df8c7802eda201b2d2e0239b409868171fc104ba8b76f10b4da09f6817ffc609 |
| 2893 c413ede267fd1fbab46880c90f80eccf0013185eb48b47ba03df2bdaadef3181 |
| 2894 cb8976f18e13188768170f98c0f844bb78cb04c62ddac59d09fc3fa25dfc1da4 |
| 2895 14deb3df1344f70000000049454e44ae426082 |
| 2896 """), |
| 2897 'basi3p08': _dehex(""" |
| 2898 89504e470d0a1a0a0000000d494844520000002000000020080300000133a3ba |
| 2899 500000000467414d41000186a031e8965f00000300504c5445224400f5ffed77 |
| 2900 ff77cbffff110a003a77002222ffff11ff110000222200ffac5566ff66ff6666 |
| 2901 ff01ff221200dcffffccff994444ff005555220000cbcbff44440055ff55cbcb |
| 2902 00331a00ffecdcedffffe4ffcbffdcdc44ff446666ff330000442200ededff66 |
| 2903 6600ffa444ffffaaeded0000cbcbfefffffdfffeffff0133ff33552a000101ff |
| 2904 8888ff00aaaa010100440000888800ffe4cbba5b0022ff22663200ffff99aaaa |
| 2905 ff550000aaaa00cb630011ff11d4ffaa773a00ff4444dc6b0066000001ff0188 |
| 2906 4200ecffdc6bdc00ffdcba00333300ed00ed7300ffff88994a0011ffff770000 |
| 2907 ff8301ffbabafe7b00fffeff00cb00ff999922ffff880000ffff77008888ffdc |
| 2908 ff1a33000000aa33ffff009900990000000001326600ffbaff44ffffffaaff00 |
| 2909 770000fefeaa00004a9900ffff66ff22220000998bff1155ffffff0101ff88ff |
| 2910 005500001111fffffefffdfea4ff4466ffffff66ff003300ffff55ff77770000 |
| 2911 88ff44ff00110077ffff006666ffffed000100fff5ed1111ffffff44ff22ffff |
| 2912 eded11110088ffff00007793ff2200dcdc3333fffe00febabaff99ffff333300 |
| 2913 63cb00baba00acff55ffffdcffff337bfe00ed00ed5555ffaaffffdcdcff5555 |
| 2914 00000066dcdc00dc00dc83ff017777fffefeffffffcbff5555777700fefe00cb |
| 2915 00cb0000fe010200010000122200ffff220044449bff33ffd4aa0000559999ff |
| 2916 999900ba00ba2a5500ffcbcbb4ff66ff9b33ffffbaaa00aa42880053aa00ffaa |
| 2917 aa0000ed00babaffff1100fe00000044009999990099ffcc99ba000088008800 |
| 2918 dc00ff93220000dcfefffeaa5300770077020100cb0000000033ffedff00ba00 |
| 2919 ff3333edffedffc488bcff7700aa00660066002222dc0000ffcbffdcffdcff8b |
| 2920 110000cb00010155005500880000002201ffffcbffcbed0000ff88884400445b |
| 2921 ba00ffbc77ff99ff006600baffba00777773ed00fe00003300330000baff77ff |
| 2922 004400aaffaafffefe000011220022c4ff8800eded99ff99ff55ff002200ffb4 |
| 2923 661100110a1100ff1111dcffbabaffff88ff88010001ff33ffb98ed362000002 |
| 2924 a249444154789c65d0695c0b001806f03711a9904a94d24dac63292949e5a810 |
| 2925 d244588a14ca5161d1a1323973252242d62157d12ae498c8124d25ca3a11398a |
| 2926 16e55a3cdffab0ffe7f77d7fcff3528645349b584c3187824d9d19d4ec2e3523 |
| 2927 9eb0ae975cf8de02f2486d502191841b42967a1ad49e5ddc4265f69a899e26b5 |
| 2928 e9e468181baae3a71a41b95669da8df2ea3594c1b31046d7b17bfb86592e4cbe |
| 2929 d89b23e8db0af6304d756e60a8f4ad378bdc2552ae5948df1d35b52143141533 |
| 2930 33bbbbababebeb3b3bc9c9c9c6c6c0c0d7b7b535323225a5aa8a02024a4bedec |
| 2931 0a0a2a2bcdcd7d7cf2f3a9a9c9cdcdd8b8adcdd5b5ababa828298982824a4ab2 |
| 2932 b21212acadbdbc1414e2e24859b9a72730302f4f49292c4c57373c9c0a0b7372 |
| 2933 8c8c1c1c3a3a92936d6dfdfd293e3e26262a4a4eaea2424b4b5fbfbc9c323278 |
| 2934 3c0b0ba1303abaae8ecdeeed950d6669a9a7a7a141d4de9e9d5d5cdcd2229b94 |
| 2935 c572716132f97cb1d8db9bc3110864a39795d9db6b6a26267a7a9a98d4d6a6a7 |
| 2936 cb76090ef6f030354d4d75766e686030545464cb393a1a1ac6c68686eae8f8f9 |
| 2937 a9aa4644c8b66d6e1689dcdd2512a994cb35330b0991ad9f9b6b659596a6addd |
| 2938 d8282fafae5e5323fb8f41d01f76c22fd8061be01bfc041a0323e1002c81cd30 |
| 2939 0b9ec027a0c930014ec035580fc3e112bc069a0b53e11c0c8095f00176c163a0 |
| 2940 e5301baec06a580677600ddc05ba0f13e120bc81a770133ec355a017300d4ec2 |
| 2941 0c7800bbe1219c02fa08f3e13c1c85dbb00a2ec05ea0dff00a6ec15a98027360 |
| 2942 070c047a06d7e1085c84f1b014f6c03fa0b33018b6c0211801ebe018fc00da0a |
| 2943 6f61113c877eb01d4ec317a085700f26c130f80efbe132bc039a0733e106fc81 |
| 2944 f7f017f6c10aa0d1300a0ec374780943e1382c06fa0a9b60238c83473016cec0 |
| 2945 02f80f73fefe1072afc1e50000000049454e44ae426082 |
| 2946 """), |
| 2947 'basi6a08': _dehex(""" |
| 2948 89504e470d0a1a0a0000000d4948445200000020000000200806000001047d4a |
| 2949 620000000467414d41000186a031e8965f0000012049444154789cc595414ec3 |
| 2950 3010459fa541b8bbb26641b8069b861e8b4d12c1c112c1452a710a2a65d840d5 |
| 2951 949041fc481ec98ae27c7f3f8d27e3e4648047600fec0d1f390fbbe2633a31e2 |
| 2952 9389e4e4ea7bfdbf3d9a6b800ab89f1bd6b553cfcbb0679e960563d72e0a9293 |
| 2953 b7337b9f988cc67f5f0e186d20e808042f1c97054e1309da40d02d7e27f92e03 |
| 2954 6cbfc64df0fc3117a6210a1b6ad1a00df21c1abcf2a01944c7101b0cb568a001 |
| 2955 909c9cf9e399cf3d8d9d4660a875405d9a60d000b05e2de55e25780b7a5268e0 |
| 2956 622118e2399aab063a815808462f1ab86890fc2e03e48bb109ded7d26ce4bf59 |
| 2957 0db91bac0050747fec5015ce80da0e5700281be533f0ce6d5900b59bcb00ea6d |
| 2958 200314cf801faab200ea752803a8d7a90c503a039f824a53f4694e7342000000 |
| 2959 0049454e44ae426082 |
| 2960 """), |
| 2961 'basn0g01': _dehex(""" |
| 2962 89504e470d0a1a0a0000000d49484452000000200000002001000000005b0147 |
| 2963 590000000467414d41000186a031e8965f0000005b49444154789c2dccb10903 |
| 2964 300c05d1ebd204b24a200b7a346f90153c82c18d0a61450751f1e08a2faaead2 |
| 2965 a4846ccea9255306e753345712e211b221bf4b263d1b427325255e8bdab29e6f |
| 2966 6aca30692e9d29616ee96f3065f0bf1f1087492fd02f14c90000000049454e44 |
| 2967 ae426082 |
| 2968 """), |
| 2969 'basn0g02': _dehex(""" |
| 2970 89504e470d0a1a0a0000000d49484452000000200000002002000000001ca13d |
| 2971 890000000467414d41000186a031e8965f0000001f49444154789c6360085df5 |
| 2972 1f8cf1308850c20053868f0133091f6390b90700bd497f818b0989a900000000 |
| 2973 49454e44ae426082 |
| 2974 """), |
| 2975 # A version of basn0g04 dithered down to 3 bits. |
| 2976 'Basn0g03': _dehex(""" |
| 2977 89504e470d0a1a0a0000000d494844520000002000000020040000000093e1c8 |
| 2978 2900000001734249540371d88211000000fd49444154789c6d90d18906210c84 |
| 2979 c356f22356b2889588604301b112112b11d94a96bb495cf7fe87f32d996f2689 |
| 2980 44741cc658e39c0b118f883e1f63cc89dafbc04c0f619d7d898396c54b875517 |
| 2981 83f3a2e7ac09a2074430e7f497f00f1138a5444f82839c5206b1f51053cca968 |
| 2982 63258821e7f2b5438aac16fbecc052b646e709de45cf18996b29648508728612 |
| 2983 952ca606a73566d44612b876845e9a347084ea4868d2907ff06be4436c4b41a3 |
| 2984 a3e1774285614c5affb40dbd931a526619d9fa18e4c2be420858de1df0e69893 |
| 2985 a0e3e5523461be448561001042b7d4a15309ce2c57aef2ba89d1c13794a109d7 |
| 2986 b5880aa27744fc5c4aecb5e7bcef5fe528ec6293a930690000000049454e44ae |
| 2987 426082 |
| 2988 """), |
| 2989 'basn0g04': _dehex(""" |
| 2990 89504e470d0a1a0a0000000d494844520000002000000020040000000093e1c8 |
| 2991 290000000467414d41000186a031e8965f0000004849444154789c6360601014 |
| 2992 545232367671090d4d4b2b2f6720430095dbd1418e002a77e64c720450b9ab56 |
| 2993 912380caddbd9b1c0154ee9933e408a072efde25470095fbee1d1902001f14ee |
| 2994 01eaff41fa0000000049454e44ae426082 |
| 2995 """), |
| 2996 'basn0g08': _dehex(""" |
| 2997 89504e470d0a1a0a0000000d4948445200000020000000200800000000561125 |
| 2998 280000000467414d41000186a031e8965f0000004149444154789c6364602400 |
| 2999 1408c8b30c05058c0f0829f8f71f3f6079301c1430ca11906764a2795c0c0605 |
| 3000 8c8ff0cafeffcff887e67131181430cae0956564040050e5fe7135e2d8590000 |
| 3001 000049454e44ae426082 |
| 3002 """), |
| 3003 'basn0g16': _dehex(""" |
| 3004 89504e470d0a1a0a0000000d49484452000000200000002010000000000681f9 |
| 3005 6b0000000467414d41000186a031e8965f0000005e49444154789cd5d2310ac0 |
| 3006 300c4351395bef7fc6dca093c0287b32d52a04a3d98f3f3880a7b857131363a0 |
| 3007 3a82601d089900dd82f640ca04e816dc06422640b7a03d903201ba05b7819009 |
| 3008 d02d680fa44c603f6f07ec4ff41938cf7f0016d84bd85fae2b9fd70000000049 |
| 3009 454e44ae426082 |
| 3010 """), |
| 3011 'basn2c08': _dehex(""" |
| 3012 89504e470d0a1a0a0000000d4948445200000020000000200802000000fc18ed |
| 3013 a30000000467414d41000186a031e8965f0000004849444154789cedd5c10900 |
| 3014 300c024085ec91fdb772133b442bf4a1f8cee12bb40d043b800a14f81ca0ede4 |
| 3015 7d4c784081020f4a871fc284071428f0a0743823a94081bb7077a3c00182b1f9 |
| 3016 5e0f40cf4b0000000049454e44ae426082 |
| 3017 """), |
| 3018 'basn2c16': _dehex(""" |
| 3019 89504e470d0a1a0a0000000d4948445200000020000000201002000000ac8831 |
| 3020 e00000000467414d41000186a031e8965f000000e549444154789cd596c10a83 |
| 3021 301044a7e0417fcb7eb7fdadf6961e06039286266693cc7a188645e43dd6a08f |
| 3022 1042003e2fe09aef6472737e183d27335fcee2f35a77b702ebce742870a23397 |
| 3023 f3edf2705dd10160f3b2815fe8ecf2027974a6b0c03f74a6e4192843e75c6c03 |
| 3024 35e8ec3202f5e84c0181bbe8cca967a00d9df3491bb040671f2e6087ce1c2860 |
| 3025 8d1e05f8c7ee0f1d00b667e70df44467ef26d01fbd9bc028f42860f71d188bce |
| 3026 fb8d3630039dbd59601e7ab3c06cf428507f0634d039afdc80123a7bb1801e7a |
| 3027 b1802a7a14c89f016d74ce331bf080ce9e08f8414f04bca133bfe642fe5e07bb |
| 3028 c4ec0000000049454e44ae426082 |
| 3029 """), |
| 3030 'basn6a08': _dehex(""" |
| 3031 89504e470d0a1a0a0000000d4948445200000020000000200806000000737a7a |
| 3032 f40000000467414d41000186a031e8965f0000006f49444154789cedd6310a80 |
| 3033 300c46e12764684fa1f73f55048f21c4ddc545781d52e85028fc1f4d28d98a01 |
| 3034 305e7b7e9cffba33831d75054703ca06a8f90d58a0074e351e227d805c8254e3 |
| 3035 1bb0420f5cdc2e0079208892ffe2a00136a07b4007943c1004d900195036407f |
| 3036 011bf00052201a9c160fb84c0000000049454e44ae426082 |
| 3037 """), |
| 3038 'cs3n3p08': _dehex(""" |
| 3039 89504e470d0a1a0a0000000d494844520000002000000020080300000044a48a |
| 3040 c60000000467414d41000186a031e8965f0000000373424954030303a392a042 |
| 3041 00000054504c544592ff0000ff9200ffff00ff0000dbff00ff6dffb600006dff |
| 3042 b6ff00ff9200dbff000049ffff2400ff000024ff0049ff0000ffdb00ff4900ff |
| 3043 b6ffff0000ff2400b6ffffdb000092ffff6d000024ffff49006dff00df702b17 |
| 3044 0000004b49444154789c85cac70182000000b1b3625754b0edbfa72324ef7486 |
| 3045 184ed0177a437b680bcdd0031c0ed00ea21f74852ed00a1c9ed0086da0057487 |
| 3046 6ed0121cd6d004bda0013a421ff803224033e177f4ae260000000049454e44ae |
| 3047 426082 |
| 3048 """), |
| 3049 's09n3p02': _dehex(""" |
| 3050 89504e470d0a1a0a0000000d49484452000000090000000902030000009dffee |
| 3051 830000000467414d41000186a031e8965f000000037342495404040477f8b5a3 |
| 3052 0000000c504c544500ff000077ffff00ffff7700ff5600640000001f49444154 |
| 3053 789c63600002fbff0c0c56ab19182ca381581a4283f82071200000696505c36a |
| 3054 437f230000000049454e44ae426082 |
| 3055 """), |
| 3056 'tbgn3p08': _dehex(""" |
| 3057 89504e470d0a1a0a0000000d494844520000002000000020080300000044a48a |
| 3058 c60000000467414d41000186a031e8965f00000207504c54457f7f7fafafafab |
| 3059 abab110000222200737300999999510d00444400959500959595e6e600919191 |
| 3060 8d8d8d620d00898989666600b7b700911600000000730d007373736f6f6faaaa |
| 3061 006b6b6b676767c41a00cccc0000f30000ef00d51e0055555567670000dd0051 |
| 3062 515100d1004d4d4de61e0038380000b700160d0d00ab00560d00090900009500 |
| 3063 009100008d003333332f2f2f2f2b2f2b2b000077007c7c001a05002b27000073 |
| 3064 002b2b2b006f00bb1600272727780d002323230055004d4d00cc1e00004d00cc |
| 3065 1a000d00003c09006f6f00002f003811271111110d0d0d55554d090909001100 |
| 3066 4d0900050505000d00e2e200000900000500626200a6a6a6a2a2a29e9e9e8484 |
| 3067 00fb00fbd5d500801100800d00ea00ea555500a6a600e600e6f7f700e200e233 |
| 3068 0500888888d900d9848484c01a007777003c3c05c8c8008080804409007c7c7c |
| 3069 bb00bbaa00aaa600a61e09056262629e009e9a009af322005e5e5e05050000ee |
| 3070 005a5a5adddd00a616008d008d00e20016050027270088110078780000c40078 |
| 3071 00787300736f006f44444400aa00c81e004040406600663c3c3c090000550055 |
| 3072 1a1a00343434d91e000084004d004d007c004500453c3c00ea1e00222222113c |
| 3073 113300331e1e1efb22001a1a1a004400afaf00270027003c001616161e001e0d |
| 3074 160d2f2f00808000001e00d1d1001100110d000db7b7b7090009050005b3b3b3 |
| 3075 6d34c4230000000174524e530040e6d86600000001624b474402660b7c640000 |
| 3076 01f249444154789c6360c0048c8c58049100575f215ee92e6161ef109cd2a15e |
| 3077 4b9645ce5d2c8f433aa4c24f3cbd4c98833b2314ab74a186f094b9c2c27571d2 |
| 3078 6a2a58e4253c5cda8559057a392363854db4d9d0641973660b0b0bb76bb16656 |
| 3079 06970997256877a07a95c75a1804b2fbcd128c80b482a0b0300f8a824276a9a8 |
| 3080 ec6e61612b3e57ee06fbf0009619d5fac846ac5c60ed20e754921625a2daadc6 |
| 3081 1967e29e97d2239c8aec7e61fdeca9cecebef54eb36c848517164514af16169e |
| 3082 866444b2b0b7b55534c815cc2ec22d89cd1353800a8473100a4485852d924a6a |
| 3083 412adc74e7ad1016ceed043267238c901716f633a812022998a4072267c4af02 |
| 3084 92127005c0f811b62830054935ce017b38bf0948cc5c09955f030a24617d9d46 |
| 3085 63371fd940b0827931cbfdf4956076ac018b592f72d45594a9b1f307f3261b1a |
| 3086 084bc2ad50018b1900719ba6ba4ca325d0427d3f6161449486f981144cf3100e |
| 3087 2a5f2a1ce8683e4ddf1b64275240c8438d98af0c729bbe07982b8a1c94201dc2 |
| 3088 b3174c9820bcc06201585ad81b25b64a2146384e3798290c05ad280a18c0a62e |
| 3089 e898260c07fca80a24c076cc864b777131a00190cdfa3069035eccbc038c30e1 |
| 3090 3e88b46d16b6acc5380d6ac202511c392f4b789aa7b0b08718765990111606c2 |
| 3091 9e854c38e5191878fbe471e749b0112bb18902008dc473b2b2e8e72700000000 |
| 3092 49454e44ae426082 |
| 3093 """), |
| 3094 'Tp2n3p08': _dehex(""" |
| 3095 89504e470d0a1a0a0000000d494844520000002000000020080300000044a48a |
| 3096 c60000000467414d41000186a031e8965f00000300504c544502ffff80ff05ff |
| 3097 7f0703ff7f0180ff04ff00ffff06ff000880ff05ff7f07ffff06ff000804ff00 |
| 3098 0180ff02ffff03ff7f02ffff80ff0503ff7f0180ffff0008ff7f0704ff00ffff |
| 3099 06ff000802ffffff7f0704ff0003ff7fffff0680ff050180ff04ff000180ffff |
| 3100 0008ffff0603ff7f80ff05ff7f0702ffffff000880ff05ffff0603ff7f02ffff |
| 3101 ff7f070180ff04ff00ffff06ff000880ff050180ffff7f0702ffff04ff0003ff |
| 3102 7fff7f0704ff0003ff7f0180ffffff06ff000880ff0502ffffffff0603ff7fff |
| 3103 7f0702ffff04ff000180ff80ff05ff0008ff7f07ffff0680ff0504ff00ff0008 |
| 3104 0180ff03ff7f02ffff02ffffffff0604ff0003ff7f0180ffff000880ff05ff7f |
| 3105 0780ff05ff00080180ff02ffffff7f0703ff7fffff0604ff00ff7f07ff0008ff |
| 3106 ff0680ff0504ff0002ffff0180ff03ff7fff0008ffff0680ff0504ff000180ff |
| 3107 02ffff03ff7fff7f070180ff02ffff04ff00ffff06ff0008ff7f0780ff0503ff |
| 3108 7fffff06ff0008ff7f0780ff0502ffff03ff7f0180ff04ff0002ffffff7f07ff |
| 3109 ff0604ff0003ff7fff00080180ff80ff05ffff0603ff7f0180ffff000804ff00 |
| 3110 80ff0502ffffff7f0780ff05ffff0604ff000180ffff000802ffffff7f0703ff |
| 3111 7fff0008ff7f070180ff03ff7f02ffff80ff05ffff0604ff00ff0008ffff0602 |
| 3112 ffff0180ff04ff0003ff7f80ff05ff7f070180ff04ff00ff7f0780ff0502ffff |
| 3113 ff000803ff7fffff0602ffffff7f07ffff0680ff05ff000804ff0003ff7f0180 |
| 3114 ff02ffff0180ffff7f0703ff7fff000804ff0080ff05ffff0602ffff04ff00ff |
| 3115 ff0603ff7fff7f070180ff80ff05ff000803ff7f0180ffff7f0702ffffff0008 |
| 3116 04ff00ffff0680ff0503ff7f0180ff04ff0080ff05ffff06ff000802ffffff7f |
| 3117 0780ff05ff0008ff7f070180ff03ff7f04ff0002ffffffff0604ff00ff7f07ff |
| 3118 000880ff05ffff060180ff02ffff03ff7f80ff05ffff0602ffff0180ff03ff7f |
| 3119 04ff00ff7f07ff00080180ffff000880ff0502ffff04ff00ff7f0703ff7fffff |
| 3120 06ff0008ffff0604ff00ff7f0780ff0502ffff03ff7f0180ffdeb83387000000 |
| 3121 f874524e53000000000000000008080808080808081010101010101010181818 |
| 3122 1818181818202020202020202029292929292929293131313131313131393939 |
| 3123 393939393941414141414141414a4a4a4a4a4a4a4a52525252525252525a5a5a |
| 3124 5a5a5a5a5a62626262626262626a6a6a6a6a6a6a6a73737373737373737b7b7b |
| 3125 7b7b7b7b7b83838383838383838b8b8b8b8b8b8b8b94949494949494949c9c9c |
| 3126 9c9c9c9c9ca4a4a4a4a4a4a4a4acacacacacacacacb4b4b4b4b4b4b4b4bdbdbd |
| 3127 bdbdbdbdbdc5c5c5c5c5c5c5c5cdcdcdcdcdcdcdcdd5d5d5d5d5d5d5d5dedede |
| 3128 dededededee6e6e6e6e6e6e6e6eeeeeeeeeeeeeeeef6f6f6f6f6f6f6f6b98ac5 |
| 3129 ca0000012c49444154789c6360e7169150d230b475f7098d4ccc28a96ced9e32 |
| 3130 63c1da2d7b8e9fb97af3d1fb8f3f18e8a0808953544a4dd7c4c2c9233c2621bf |
| 3131 b4aab17fdacce5ab36ee3a72eafaad87efbefea68702362e7159652d031b07cf |
| 3132 c0b8a4cce28aa68e89f316aedfb4ffd0b92bf79fbcfcfe931e0a183904e55435 |
| 3133 8decdcbcc22292b3caaadb7b27cc5db67af3be63e72fdf78fce2d31f7a2860e5 |
| 3134 119356d037b374f10e8a4fc92eaa6fee99347fc9caad7b0f9ebd74f7c1db2fbf |
| 3135 e8a180995f484645dbdccad12f38363dafbcb6a573faeca5ebb6ed3e7ce2c29d |
| 3136 e76fbefda38702063e0149751d537b67ff80e8d4dcc29a86bea97316add9b0e3 |
| 3137 c0e96bf79ebdfafc971e0a587885e515f58cad5d7d43a2d2720aeadaba26cf5a |
| 3138 bc62fbcea3272fde7efafac37f3a28000087c0fe101bc2f85f0000000049454e |
| 3139 44ae426082 |
| 3140 """), |
| 3141 'tbbn1g04': _dehex(""" |
| 3142 89504e470d0a1a0a0000000d494844520000002000000020040000000093e1c8 |
| 3143 290000000467414d41000186a031e8965f0000000274524e530007e8f7589b00 |
| 3144 000002624b47440000aa8d23320000013e49444154789c55d1cd4b024118c7f1 |
| 3145 efbe6419045b6a48a72d352808b435284f9187ae9b098627a1573a19945beba5 |
| 3146 e8129e8222af11d81e3a4545742de8ef6af6d5762e0fbf0fc33c33f36085cb76 |
| 3147 bc4204778771b867260683ee57e13f0c922df5c719c2b3b6c6c25b2382cea4b9 |
| 3148 9f7d4f244370746ac71f4ca88e0f173a6496749af47de8e44ba8f3bf9bdfa98a |
| 3149 0faf857a7dd95c7dc8d7c67c782c99727997f41eb2e3c1e554152465bb00fe8e |
| 3150 b692d190b718d159f4c0a45c4435915a243c58a7a4312a7a57913f05747594c6 |
| 3151 46169866c57101e4d4ce4d511423119c419183a3530cc63db88559ae28e7342a |
| 3152 1e9c8122b71139b8872d6e913153224bc1f35b60e4445bd4004e20ed6682c759 |
| 3153 1d9873b3da0fbf50137dc5c9bde84fdb2ec8bde1189e0448b63584735993c209 |
| 3154 7a601bd2710caceba6158797285b7f2084a2f82c57c01a0000000049454e44ae |
| 3155 426082 |
| 3156 """), |
| 3157 'tbrn2c08': _dehex(""" |
| 3158 89504e470d0a1a0a0000000d4948445200000020000000200802000000fc18ed |
| 3159 a30000000467414d41000186a031e8965f0000000674524e53007f007f007f8a |
| 3160 33334f00000006624b474400ff0000000033277cf3000004d649444154789cad |
| 3161 965f68537714c73fd912d640235e692f34d0406fa0c1663481045ab060065514 |
| 3162 56660a295831607df0a1488715167060840a1614e6431e9cb34fd2c00a762c85 |
| 3163 f6a10f816650c13b0cf40612e1822ddc4863bd628a8924d23d6464f9d3665dd9 |
| 3164 f7e977ce3dbff3cd3939bfdfef6bb87dfb364782dbed065ebe7cd93acc78b4ec |
| 3165 a228debd7bb7bfbfbfbbbbfb7f261045311a8d261209405194274f9ea4d3e916 |
| 3166 f15f1c3eb5dd6e4fa5fecce526239184a2b0b8486f6f617171b1f5ae4311381c |
| 3167 8e57af5e5dbd7a351088150a78bd389d44222c2f93cdfe66b7db8f4ee07038b6 |
| 3168 b6b6bebf766d7e7e7e60a06432313b4ba984c3c1c4049a46b95c5a58583822c1 |
| 3169 dbb76f27272733d1b9df853c3030c0f232562b9108cf9eb1b888d7cbf030abab |
| 3170 31abd5fa1f08dc6ef7e7cf9f1f3f7e1c8944745d4f1400c62c001313acad21cb |
| 3171 b8dd2c2c603271eb1640341aad4c6d331aa7e8c48913a150a861307ecc11e964 |
| 3172 74899919bc5e14e56fffc404f1388502f178dceff7ef4bf0a5cfe7abb533998c |
| 3173 e5f9ea2f1dd88c180d64cb94412df3dd57e83a6b3b3c7a84c98420100c72fd3a |
| 3174 636348bae726379fe69e8e8d8dbd79f3a6558b0607079796965256479b918085 |
| 3175 7b02db12712b6181950233023f3f647494ee6e2e5ea45864cce5b8a7fe3acffc |
| 3176 3aebb22c2bd5d20e22d0757d7b7bbbbdbd3d94a313bed1b0aa3cd069838b163a |
| 3177 8d4c59585f677292d0b84d9a995bd337def3fe6bbe5e6001989b9b6bfe27ea08 |
| 3178 36373781542ab56573248b4c5bc843ac4048c7ab21aa24ca00534c25482828a3 |
| 3179 8c9ee67475bbaaaab22cb722c8e57240a150301a8d219de94e44534d7d90e885 |
| 3180 87acb0e2c4f9800731629b6c5ee14a35a6b9887d2a0032994cb9cf15dbe59650 |
| 3181 ff7b46a04c9a749e7cc5112214266cc65c31354d5b5d5d3d90209bcd5616a552 |
| 3182 a95c2e87f2a659bd9ee01c2cd73964e438f129a6aa9e582c363838b80f81d7eb |
| 3183 5555b56a2a8ad2d9d7affd0409f8015c208013fea00177b873831b0282c964f2 |
| 3184 783c1e8fa7582cee5f81a669b5e6eeeeaee58e8559b0c233d8843c7c0b963a82 |
| 3185 34e94b5cb2396d7d7d7db22c8ba258fb0afd43f0e2c58b919191ba9de9b4d425 |
| 3186 118329b0c3323c8709d02041b52b4ea7f39de75d2a934a2693c0a953a76a93d4 |
| 3187 5d157ebf7f6565a5542a553df97c5e10045dd731c130b86113cc300cbd489224 |
| 3188 08422a952a140a95788fc763b1d41558d7a2d7af5f5fb870a1d6a3aaaacd6603 |
| 3189 18802da84c59015bd2e6897b745d9765b99a1df0f97c0daf74e36deaf7fbcd66 |
| 3190 73ad2797cb89a2c839880188a2e8743a8bc5a22ccbba5e376466b3b9bdbdbd21 |
| 3191 6123413a9d0e0402b51e4dd3bababa788eb022b85caeb6b6364551b6b7b76942 |
| 3192 43f7f727007a7a7a04a1ee8065b3595fde2768423299ac1ec6669c3973e65004 |
| 3193 c0f8f878ad69341a33994ced2969c0d0d0502412f9f8f163f3a7fd654b474787 |
| 3194 288ad53e74757535df6215b85cae60302849d2410aecc037f9f2e5cbd5b5c160 |
| 3195 680eb0dbede170381c0e7ff8f0a185be3b906068684892a4ca7a6f6faff69328 |
| 3196 8ad3d3d3f7efdfdfdbdbfb57e96868a14d0d0643381c96242997cbe5f3794010 |
| 3197 84603078fcf8f1d6496bd14a3aba5c2ea7d369341a5555b5582c8140e0fcf9f3 |
| 3198 1b1b1b87cf4eeb0a8063c78e45a3d19e9e1ebfdfdf5a831e844655d18093274f |
| 3199 9e3d7bf6d3a74f3b3b3b47c80efc05ff7af28fefb70d9b0000000049454e44ae |
| 3200 426082 |
| 3201 """), |
| 3202 'basn6a16': _dehex(""" |
| 3203 89504e470d0a1a0a0000000d494844520000002000000020100600000023eaa6 |
| 3204 b70000000467414d41000186a031e8965f00000d2249444154789cdd995f6c1c |
| 3205 d775c67ff38fb34b724d2ee55a8e4b04a0ac87049100cab4dbd8c6528902cb4d |
| 3206 10881620592e52d4325ac0905bc98a94025e71fd622cb5065ac98a0c283050c0 |
| 3207 728a00b6e542a1d126885cd3298928891d9a0444037e904434951d4b90b84b2f |
| 3208 c9dde1fcebc33977a95555348f411e16dfce9d3b77ee77eebde77ce78c95a669 |
| 3209 0ad07c17009a13edd898b87dfb1fcb7d2b4d1bff217f33df80deb1e6267df0ff |
| 3210 c1e6e6dfafdf1f5a7fd30f9aef66b6d546dd355bf02c40662e3307f9725a96c6 |
| 3211 744c3031f83782f171c148dbc3bf1774f5dad1e79d6f095a3f54d4fbec5234ef |
| 3212 d9a2f8d73afe4f14f57ef4f42def7b44f19060f06b45bddf1c5534d77fd922be |
| 3213 2973a15a82e648661c6e3240aa3612ead952b604bde57458894f29deaf133bac |
| 3214 13d2766f5227a4a3b8cf08da7adfd6fbd6bd8a4fe9dbb43d35e3dfa3f844fbf8 |
| 3215 9119bf4f7144094fb56333abf8a86063ca106f94b3a3b512343765e60082097f |
| 3216 1bb86ba72439a653519b09f5cee1ce61c897d37eedf5553580ae60f4af8af33a |
| 3217 b14fd400b6a0f34535c0434afc0b3a9f07147527a5fa7ca218ff56c74d74dc3f |
| 3218 155cfd3325fc278acf2ae1cb4a539f5f9937c457263b0bd51234c732a300cdd1 |
| 3219 cc1840f0aaff54db0e4874ed5a9b5d6d27d4bb36746d80de72baa877ff4b275a |
| 3220 d7895ed1897ea4139b5143fcbb1a62560da1ed9662aaed895ec78a91c18795b8 |
| 3221 5e07ab4af8ba128e95e682e0728bf8f2e5ae815a091a53d902ac1920d8e05f06 |
| 3222 589de8d8d66680789f4e454fb9d9ec66cd857af796ee2d902fa73fd5bba775a2 |
| 3223 153580ae44705ed0d37647d15697cb8f14bfa3e3e8fdf8031d47af571503357c |
| 3224 f30d25acedcbbf135c9a35c49766ba07ab255859e8ec03684e66860182dff8f7 |
| 3225 0304bff6ff1c20fc81b7afdd00a71475539a536e36bb5973a19e3b923b02bde5 |
| 3226 e4efd4003ac170eb2d13fe274157afedbd82d6fb3a9a1e85e4551d47cf7078f8 |
| 3227 9671fe4289ebf5f2bf08d63f37c4eb4773c55a0996efeefa0ca011671d8060ca |
| 3228 2f0004c7fcc300e166ef0240f825efe3361f106d57d423d0723f7acacd66376b |
| 3229 2ed47b7a7a7a205f4ef4ac4691e0aad9aa0d41cf13741c3580a506487574ddca |
| 3230 61a8c403c1863ebfbcac3475168b2de28b8b3d77544bb05ce92a02aceced3c0d |
| 3231 d0cc65ea371b201cf1c601c24dde1c4078cedbdeb60322f50126a019bf6edc9b |
| 3232 39e566b39b3517eaf97c3e0fbde5e4491d45bd74537145d155b476aa0176e868 |
| 3233 c6abebf30dbd5e525c54ac8e18e2d56abeb756827a3d970358a97416019a6f64 |
| 3234 f60004fdfe1580d5c98e618070cc1b05887eee7e0d209a70db7d8063029889b4 |
| 3235 c620ead78d7b33a7dc6c76b3e6427ddddbebde867c393aa7845e5403e8ca794a |
| 3236 d0d6fb897af5f03525fe5782f5e7046bdaef468bf88d1debc6ab25583cd17310 |
| 3237 6079b9ab0ba059c914018245bf076075b5a303200c3c1f209a733701444fbbaf |
| 3238 00c4134ebb016c5d0b23614c243701cdf875e3decce9349bddacb9505fbf7dfd |
| 3239 76e82d87736a00f5d2b5ffd4b7dce2719a4d25ae717ee153c1abef18e257cfad |
| 3240 7fa45682da48ef38c052b53b0fd06864b300c151ff08c0ea431de701a287dd5f |
| 3241 004497dc7b01a253ee3e80b8c7f91c20f967fb6fdb7c80ada7d8683723614c24 |
| 3242 3701cdf875e3decc29379bddacb950ef3fd47f08f2e5a61ea4aa2a3eb757cd55 |
| 3243 13345efcfa59c12b2f19e2578ef77fb75a82854ffbee01a83f977b11a031931d |
| 3244 040802df07082b5e11207cc17b1e209a770700e2df0a83e409fb7580f827c230 |
| 3245 99b06fd901fb058d6835dacd481813c94d40337eddb83773cacd66376b2ed437 |
| 3246 bebcf165e82d2f4e4beb7f3fa6e652c2d7ee10bc78c010bfb87fe3c95a09ae9f |
| 3247 bd732740bd2fb700d0f865f64180e059ff044018ca0ca28a5b04883f701e0088 |
| 3248 bfec7c0c909cb71f0448c6ec518074b375012079d9dedf66004bcfbc51eb2dd1 |
| 3249 aadacd481813c94d40337eddb83773cacd66376b2ed487868686205fbe7c49ef |
| 3250 5605a73f34c4a7a787eeab96e0da81bb4e022c15ba27019a5b339300e16bf286 |
| 3251 a8eae601e25866907cdf3e0890acb36f00245fb57f05904e59c300e92561946e |
| 3252 b2e600d209ab7d07f04d458dfb46ad1bd16ab49b913026929b8066fcba716fe6 |
| 3253 949bcd6ed65ca8ef7e7cf7e3d05b7e7c8f217ee6cdddbb6a25a856f37980e0c7 |
| 3254 fe4e80a82623c48193014846ec7180f4acf518409aca0cd28a5504e03b32c374 |
| 3255 de1a00608a0240faaa327a4b19fe946fb6f90054dbb5f2333d022db56eb4966a |
| 3256 3723614c243701cdf8f556bea8a7dc6c76b3e66bd46584ddbbcebc0990cf4b0f |
| 3257 ff4070520c282338a7e26700ec725202b01e4bcf0258963c6f1d4d8f0030cb20 |
| 3258 805549c520930c03584fa522b676f11600ffc03fde3e1b3489a9c9054c9aa23b |
| 3259 c08856a3dd8c843191dc0434e3d78d7b33a75c36fb993761f7ae5a69f72ef97f |
| 3260 e6ad336fed7e1c60e8bee96980bbdebbb60da07b7069062033d9dc0ae03d296f |
| 3261 70ab511ec071640676252902d833c916007b3e1900b0a6d2028035968e025861 |
| 3262 ea01581369fb11488c34d18cbc95989afccca42baad65ba2d5683723614c24d7 |
| 3263 8066fcbab8b7e96918baaf5aaa56219f975fb50a43f7c9bde90fa73f1c1a02d8 |
| 3264 78f2e27e803b77ca08b90519315b6fe400fc1392097a9eccc0ad444500e70199 |
| 3265 a1331f0f00d8934901c07e5d526ceb87c2d07e2579badd005a2b31a5089391b7 |
| 3266 1253358049535a6add8856dd0146c298482e01ede27ed878b256ba7600ee3a09 |
| 3267 c18fc1df09fe01084ec25defc1b56db0f1a4f4bd78e0e2818d2f0334e7330300 |
| 3268 7df7c888b917e50dd9c1c60c80efcb0cbc63e1f700bce7c31700dccbd1060027 |
| 3269 8add9b0de06c8e2f00d84962b7d7030e2a61538331b98051f92631bd253f336a |
| 3270 dd8856a3dd44c25c390efddfad96ae9f853b77c25201ba27c533b8bdf28b6ad0 |
| 3271 3d084b33d2e7fa59099e9901b8f2d29597fa0f01848f78e70082117f1ca07b76 |
| 3272 6910209b9519f895a008d031bbba05c09d8f06005c5b18b8fba25300cea6780e |
| 3273 c03e911c6ccf06d507b48a4fa606634a114609de929f9934c5a87511ad57cfc1 |
| 3274 fa476aa5854fa1ef1e3910b905686e85cc24c40138198915f133d2d6dc2a7dea |
| 3275 7df2ccc2a752faf2cec1d577aebeb37e3b4034eeee0008dff3be0e6b923773b4 |
| 3276 7904c0ef9119767cb4fa1500ef1361e08e452500f71561e84cc4ed3e20fab6a2 |
| 3277 c905f40cb76a3026bf3319b91ac2e46792a6dcd801ebc6aba5da08f48ecb81c8 |
| 3278 bd088d5f42f6417191de93908c803d0e76199292b485af41b60e8d9c3c537f0e |
| 3279 8211f0c7211a077707dc18b931b2ee6d80a4d7ae024491ebc24d4a708ff70680 |
| 3280 7f25e807e8785f1878e322d6ddaf453f0770ff2dfa769b01423dbbad72a391b6 |
| 3281 5a7c3235985629423372494cab55c8f7d64a8b27a0e7202c55a13b0f8d19c80e |
| 3282 4ae9ca3f015115dc3ca467c17a4c7ee95970ab10e5a54ff0ac3cd39881ee5958 |
| 3283 1a84f03df0be0e492fd855a8d6aa35d10b4962dbb0a604a3d3ee5e80a8eee600 |
| 3284 a24977f8660378bf0bbf00e01d0a8fb7f980f04b8aa6ce6aca8d5a7533c52753 |
| 3285 839152c4e222f4dc512dd5eb90cbc981e8ea12cf90cd8a8bf47d89159e2741d3 |
| 3286 7124f65b96fcd254dae258fa84a13c13043246a32129574787e49eae2b49b86d |
| 3287 c3e2e78b9ff7f4002415bb08907c66df0d103b4e0c104db90500ff70700c203a |
| 3288 ee1e82dba4c3e16e256c0acca6ceaae9afd1f612d7eb472157ac95962bd05594 |
| 3289 7dd1598466053245088e827f44628657942a825b84e4fb601f84b4025611aca3 |
| 3290 901e01bb024911dc0a4445f08e41f83df02b10142173149ab71baf027611ea95 |
| 3291 7a257704201d14cd9af4d90b00f194530088cb4e09c0df1c5c0088f7393f6833 |
| 3292 c0aa3ac156655de3bca9b34ab9716906ba07aba5e5bba1eb3358d90b9da7c533 |
| 3293 64f6888bf47b60f521e8380fe10be03d2feac17900927560df40f4e48f805960 |
| 3294 50328d648bf4893f9067c217a0631656b7c898c122847bc07b03a2d3e0ee85e4 |
| 3295 33b0ef867450c4fad2ecd26cf7168074c0ba0c904cdac300c9cfec4701924df6 |
| 3296 1cdca61e10685c6f7d52d0caba1498972f43d740adb4b2009d7d7220b20e3473 |
| 3297 90a943d00ffe959bb6eac3e0fe42ea49ee00c45f06e76329b1dabf127d690d80 |
| 3298 5581b408f63c2403e0cc433c00ee658836803b0fd100747c04ab5f917704fd10 |
| 3299 d5c1cd41ec801343d207f602a403605d86e5f9e5f9ae0d00e994556833806685 |
| 3300 c931fb709b0f08b4e869bea5c827859549e82c544b8d29c816a0390999613920 |
| 3301 7e610d5727a16318c2003c1fa24be0de2b32caf92224e7c17e5004b6350c4c01 |
| 3302 05601218066b0ad28224e149019c086257ca315102de2712903bde97b8144d82 |
| 3303 3b2c6ac52d403c054e019249b087f53d0558995a99ea946c70cc927458b3c1ff |
| 3304 550f30050df988d4284376b4566a8e416654cc921985e037e0df0fc131f00f4b |
| 3305 acf0c6211c036f14a239703741740adc7da227edd7e56b833d0ae92549b4d357 |
| 3306 25dfb49ed2ff63908e6adf27d6d0dda7638d4154d2778daca17f58e61297c129 |
| 3307 41f233b01f5dc3740cac51688c35c6b22580f48224fee9b83502569a66b629f1 |
| 3308 09f3713473413e2666e7fe6f6c6efefdfafda1f56f6e06f93496d9d67cb7366a |
| 3309 9964b6f92e64b689196ec6c604646fd3fe4771ff1bf03f65d8ecc3addbb5f300 |
| 3310 00000049454e44ae426082 |
| 3311 """), |
| 3312 } |
| 3313 |
| 3314 def read_pam_header(infile): |
| 3315 """ |
| 3316 Read (the rest of a) PAM header. `infile` should be positioned |
| 3317 immediately after the initial 'P7' line (at the beginning of the |
| 3318 second line). Returns are as for `read_pnm_header`. |
| 3319 """ |
| 3320 |
| 3321 # Unlike PBM, PGM, and PPM, we can read the header a line at a time. |
| 3322 header = dict() |
| 3323 while True: |
| 3324 l = infile.readline().strip() |
| 3325 if l == strtobytes('ENDHDR'): |
| 3326 break |
| 3327 if not l: |
| 3328 raise EOFError('PAM ended prematurely') |
| 3329 if l[0] == strtobytes('#'): |
| 3330 continue |
| 3331 l = l.split(None, 1) |
| 3332 if l[0] not in header: |
| 3333 header[l[0]] = l[1] |
| 3334 else: |
| 3335 header[l[0]] += strtobytes(' ') + l[1] |
| 3336 |
| 3337 required = ['WIDTH', 'HEIGHT', 'DEPTH', 'MAXVAL'] |
| 3338 required = [strtobytes(x) for x in required] |
| 3339 WIDTH,HEIGHT,DEPTH,MAXVAL = required |
| 3340 present = [x for x in required if x in header] |
| 3341 if len(present) != len(required): |
| 3342 raise Error('PAM file must specify WIDTH, HEIGHT, DEPTH, and MAXVAL') |
| 3343 width = int(header[WIDTH]) |
| 3344 height = int(header[HEIGHT]) |
| 3345 depth = int(header[DEPTH]) |
| 3346 maxval = int(header[MAXVAL]) |
| 3347 if (width <= 0 or |
| 3348 height <= 0 or |
| 3349 depth <= 0 or |
| 3350 maxval <= 0): |
| 3351 raise Error( |
| 3352 'WIDTH, HEIGHT, DEPTH, MAXVAL must all be positive integers') |
| 3353 return 'P7', width, height, depth, maxval |
| 3354 |
| 3355 def read_pnm_header(infile, supported=('P5','P6')): |
| 3356 """ |
| 3357 Read a PNM header, returning (format,width,height,depth,maxval). |
| 3358 `width` and `height` are in pixels. `depth` is the number of |
| 3359 channels in the image; for PBM and PGM it is synthesized as 1, for |
| 3360 PPM as 3; for PAM images it is read from the header. `maxval` is |
| 3361 synthesized (as 1) for PBM images. |
| 3362 """ |
| 3363 |
| 3364 # Generally, see http://netpbm.sourceforge.net/doc/ppm.html |
| 3365 # and http://netpbm.sourceforge.net/doc/pam.html |
| 3366 |
| 3367 supported = [strtobytes(x) for x in supported] |
| 3368 |
| 3369 # Technically 'P7' must be followed by a newline, so by using |
| 3370 # rstrip() we are being liberal in what we accept. I think this |
| 3371 # is acceptable. |
| 3372 type = infile.read(3).rstrip() |
| 3373 if type not in supported: |
| 3374 raise NotImplementedError('file format %s not supported' % type) |
| 3375 if type == strtobytes('P7'): |
| 3376 # PAM header parsing is completely different. |
| 3377 return read_pam_header(infile) |
| 3378 # Expected number of tokens in header (3 for P4, 4 for P6) |
| 3379 expected = 4 |
| 3380 pbm = ('P1', 'P4') |
| 3381 if type in pbm: |
| 3382 expected = 3 |
| 3383 header = [type] |
| 3384 |
| 3385 # We have to read the rest of the header byte by byte because the |
| 3386 # final whitespace character (immediately following the MAXVAL in |
| 3387 # the case of P6) may not be a newline. Of course all PNM files in |
| 3388 # the wild use a newline at this point, so it's tempting to use |
| 3389 # readline; but it would be wrong. |
| 3390 def getc(): |
| 3391 c = infile.read(1) |
| 3392 if not c: |
| 3393 raise Error('premature EOF reading PNM header') |
| 3394 return c |
| 3395 |
| 3396 c = getc() |
| 3397 while True: |
| 3398 # Skip whitespace that precedes a token. |
| 3399 while c.isspace(): |
| 3400 c = getc() |
| 3401 # Skip comments. |
| 3402 while c == '#': |
| 3403 while c not in '\n\r': |
| 3404 c = getc() |
| 3405 if not c.isdigit(): |
| 3406 raise Error('unexpected character %s found in header' % c) |
| 3407 # According to the specification it is legal to have comments |
| 3408 # that appear in the middle of a token. |
| 3409 # This is bonkers; I've never seen it; and it's a bit awkward to |
| 3410 # code good lexers in Python (no goto). So we break on such |
| 3411 # cases. |
| 3412 token = strtobytes('') |
| 3413 while c.isdigit(): |
| 3414 token += c |
| 3415 c = getc() |
| 3416 # Slight hack. All "tokens" are decimal integers, so convert |
| 3417 # them here. |
| 3418 header.append(int(token)) |
| 3419 if len(header) == expected: |
| 3420 break |
| 3421 # Skip comments (again) |
| 3422 while c == '#': |
| 3423 while c not in '\n\r': |
| 3424 c = getc() |
| 3425 if not c.isspace(): |
| 3426 raise Error('expected header to end with whitespace, not %s' % c) |
| 3427 |
| 3428 if type in pbm: |
| 3429 # synthesize a MAXVAL |
| 3430 header.append(1) |
| 3431 depth = (1,3)[type == strtobytes('P6')] |
| 3432 return header[0], header[1], header[2], depth, header[3] |
| 3433 |
| 3434 def write_pnm(file, width, height, pixels, meta): |
| 3435 """Write a Netpbm PNM/PAM file.""" |
| 3436 |
| 3437 bitdepth = meta['bitdepth'] |
| 3438 maxval = 2**bitdepth - 1 |
| 3439 # Rudely, the number of image planes can be used to determine |
| 3440 # whether we are L (PGM), LA (PAM), RGB (PPM), or RGBA (PAM). |
| 3441 planes = meta['planes'] |
| 3442 # Can be an assert as long as we assume that pixels and meta came |
| 3443 # from a PNG file. |
| 3444 assert planes in (1,2,3,4) |
| 3445 if planes in (1,3): |
| 3446 if 1 == planes: |
| 3447 # PGM |
| 3448 # Could generate PBM if maxval is 1, but we don't (for one |
| 3449 # thing, we'd have to convert the data, not just blat it |
| 3450 # out). |
| 3451 fmt = 'P5' |
| 3452 else: |
| 3453 # PPM |
| 3454 fmt = 'P6' |
| 3455 file.write('%s %d %d %d\n' % (fmt, width, height, maxval)) |
| 3456 if planes in (2,4): |
| 3457 # PAM |
| 3458 # See http://netpbm.sourceforge.net/doc/pam.html |
| 3459 if 2 == planes: |
| 3460 tupltype = 'GRAYSCALE_ALPHA' |
| 3461 else: |
| 3462 tupltype = 'RGB_ALPHA' |
| 3463 file.write('P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\n' |
| 3464 'TUPLTYPE %s\nENDHDR\n' % |
| 3465 (width, height, planes, maxval, tupltype)) |
| 3466 # Values per row |
| 3467 vpr = planes * width |
| 3468 # struct format |
| 3469 fmt = '>%d' % vpr |
| 3470 if maxval > 0xff: |
| 3471 fmt = fmt + 'H' |
| 3472 else: |
| 3473 fmt = fmt + 'B' |
| 3474 for row in pixels: |
| 3475 file.write(struct.pack(fmt, *row)) |
| 3476 file.flush() |
| 3477 |
| 3478 def color_triple(color): |
| 3479 """ |
| 3480 Convert a command line colour value to a RGB triple of integers. |
| 3481 FIXME: Somewhere we need support for greyscale backgrounds etc. |
| 3482 """ |
| 3483 if color.startswith('#') and len(color) == 4: |
| 3484 return (int(color[1], 16), |
| 3485 int(color[2], 16), |
| 3486 int(color[3], 16)) |
| 3487 if color.startswith('#') and len(color) == 7: |
| 3488 return (int(color[1:3], 16), |
| 3489 int(color[3:5], 16), |
| 3490 int(color[5:7], 16)) |
| 3491 elif color.startswith('#') and len(color) == 13: |
| 3492 return (int(color[1:5], 16), |
| 3493 int(color[5:9], 16), |
| 3494 int(color[9:13], 16)) |
| 3495 |
| 3496 def _add_common_options(parser): |
| 3497 """Call *parser.add_option* for each of the options that are |
| 3498 common between this PNG--PNM conversion tool and the gen |
| 3499 tool. |
| 3500 """ |
| 3501 parser.add_option("-i", "--interlace", |
| 3502 default=False, action="store_true", |
| 3503 help="create an interlaced PNG file (Adam7)") |
| 3504 parser.add_option("-t", "--transparent", |
| 3505 action="store", type="string", metavar="#RRGGBB", |
| 3506 help="mark the specified colour as transparent") |
| 3507 parser.add_option("-b", "--background", |
| 3508 action="store", type="string", metavar="#RRGGBB", |
| 3509 help="save the specified background colour") |
| 3510 parser.add_option("-g", "--gamma", |
| 3511 action="store", type="float", metavar="value", |
| 3512 help="save the specified gamma value") |
| 3513 parser.add_option("-c", "--compression", |
| 3514 action="store", type="int", metavar="level", |
| 3515 help="zlib compression level (0-9)") |
| 3516 return parser |
| 3517 |
| 3518 def _main(argv): |
| 3519 """ |
| 3520 Run the PNG encoder with options from the command line. |
| 3521 """ |
| 3522 |
| 3523 # Parse command line arguments |
| 3524 from optparse import OptionParser |
| 3525 import re |
| 3526 version = '%prog ' + re.sub(r'( ?\$|URL: |Rev:)', '', __version__) |
| 3527 parser = OptionParser(version=version) |
| 3528 parser.set_usage("%prog [options] [imagefile]") |
| 3529 parser.add_option('-r', '--read-png', default=False, |
| 3530 action='store_true', |
| 3531 help='Read PNG, write PNM') |
| 3532 parser.add_option("-a", "--alpha", |
| 3533 action="store", type="string", metavar="pgmfile", |
| 3534 help="alpha channel transparency (RGBA)") |
| 3535 _add_common_options(parser) |
| 3536 |
| 3537 (options, args) = parser.parse_args(args=argv[1:]) |
| 3538 |
| 3539 # Convert options |
| 3540 if options.transparent is not None: |
| 3541 options.transparent = color_triple(options.transparent) |
| 3542 if options.background is not None: |
| 3543 options.background = color_triple(options.background) |
| 3544 |
| 3545 # Prepare input and output files |
| 3546 if len(args) == 0: |
| 3547 infilename = '-' |
| 3548 infile = sys.stdin |
| 3549 elif len(args) == 1: |
| 3550 infilename = args[0] |
| 3551 infile = open(infilename, 'rb') |
| 3552 else: |
| 3553 parser.error("more than one input file") |
| 3554 outfile = sys.stdout |
| 3555 if sys.platform == "win32": |
| 3556 import msvcrt, os |
| 3557 msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY) |
| 3558 |
| 3559 if options.read_png: |
| 3560 # Encode PNG to PPM |
| 3561 png = Reader(file=infile) |
| 3562 width,height,pixels,meta = png.asDirect() |
| 3563 write_pnm(outfile, width, height, pixels, meta) |
| 3564 else: |
| 3565 # Encode PNM to PNG |
| 3566 format, width, height, depth, maxval = \ |
| 3567 read_pnm_header(infile, ('P5','P6','P7')) |
| 3568 # When it comes to the variety of input formats, we do something |
| 3569 # rather rude. Observe that L, LA, RGB, RGBA are the 4 colour |
| 3570 # types supported by PNG and that they correspond to 1, 2, 3, 4 |
| 3571 # channels respectively. So we use the number of channels in |
| 3572 # the source image to determine which one we have. We do not |
| 3573 # care about TUPLTYPE. |
| 3574 greyscale = depth <= 2 |
| 3575 pamalpha = depth in (2,4) |
| 3576 supported = map(lambda x: 2**x-1, range(1,17)) |
| 3577 try: |
| 3578 mi = supported.index(maxval) |
| 3579 except ValueError: |
| 3580 raise NotImplementedError( |
| 3581 'your maxval (%s) not in supported list %s' % |
| 3582 (maxval, str(supported))) |
| 3583 bitdepth = mi+1 |
| 3584 writer = Writer(width, height, |
| 3585 greyscale=greyscale, |
| 3586 bitdepth=bitdepth, |
| 3587 interlace=options.interlace, |
| 3588 transparent=options.transparent, |
| 3589 background=options.background, |
| 3590 alpha=bool(pamalpha or options.alpha), |
| 3591 gamma=options.gamma, |
| 3592 compression=options.compression) |
| 3593 if options.alpha: |
| 3594 pgmfile = open(options.alpha, 'rb') |
| 3595 format, awidth, aheight, adepth, amaxval = \ |
| 3596 read_pnm_header(pgmfile, 'P5') |
| 3597 if amaxval != '255': |
| 3598 raise NotImplementedError( |
| 3599 'maxval %s not supported for alpha channel' % amaxval) |
| 3600 if (awidth, aheight) != (width, height): |
| 3601 raise ValueError("alpha channel image size mismatch" |
| 3602 " (%s has %sx%s but %s has %sx%s)" |
| 3603 % (infilename, width, height, |
| 3604 options.alpha, awidth, aheight)) |
| 3605 writer.convert_ppm_and_pgm(infile, pgmfile, outfile) |
| 3606 else: |
| 3607 writer.convert_pnm(infile, outfile) |
| 3608 |
| 3609 |
| 3610 if __name__ == '__main__': |
| 3611 try: |
| 3612 _main(sys.argv) |
| 3613 except Error, e: |
| 3614 print >>sys.stderr, e |
OLD | NEW |