You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  1. /*
  2. * Copyright (C) 2014 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <stdint.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <errno.h>
  28. #include <byteswap.h>
  29. #include <ipxe/umalloc.h>
  30. #include <ipxe/pixbuf.h>
  31. #include <ipxe/deflate.h>
  32. #include <ipxe/png.h>
  33. /** @file
  34. *
  35. * Portable Network Graphics (PNG) format
  36. *
  37. * The PNG format is defined in RFC 2083.
  38. */
  39. /** PNG context */
  40. struct png_context {
  41. /** Offset within image */
  42. size_t offset;
  43. /** Pixel buffer */
  44. struct pixel_buffer *pixbuf;
  45. /** Bit depth */
  46. unsigned int depth;
  47. /** Colour type */
  48. unsigned int colour_type;
  49. /** Number of channels */
  50. unsigned int channels;
  51. /** Number of interlace passes */
  52. unsigned int passes;
  53. /** Palette, in iPXE's pixel buffer format */
  54. uint32_t palette[PNG_PALETTE_COUNT];
  55. /** Decompression buffer for raw PNG data */
  56. struct deflate_chunk raw;
  57. /** Decompressor */
  58. struct deflate deflate;
  59. };
  60. /** A PNG interlace pass */
  61. struct png_interlace {
  62. /** Pass number */
  63. unsigned int pass;
  64. /** X starting indent */
  65. unsigned int x_indent;
  66. /** Y starting indent */
  67. unsigned int y_indent;
  68. /** X stride */
  69. unsigned int x_stride;
  70. /** Y stride */
  71. unsigned int y_stride;
  72. /** Width */
  73. unsigned int width;
  74. /** Height */
  75. unsigned int height;
  76. };
  77. /** PNG file signature */
  78. static struct png_signature png_signature = PNG_SIGNATURE;
  79. /** Number of interlacing passes */
  80. static uint8_t png_interlace_passes[] = {
  81. [PNG_INTERLACE_NONE] = 1,
  82. [PNG_INTERLACE_ADAM7] = 7,
  83. };
  84. /**
  85. * Transcribe PNG chunk type name (for debugging)
  86. *
  87. * @v type Chunk type
  88. * @ret name Chunk type name
  89. */
  90. static const char * png_type_name ( uint32_t type ) {
  91. static union {
  92. uint32_t type;
  93. char name[ sizeof ( uint32_t ) + 1 /* NUL */ ];
  94. } u;
  95. u.type = type;
  96. return u.name;
  97. }
  98. /**
  99. * Calculate PNG interlace pass parameters
  100. *
  101. * @v png PNG context
  102. * @v pass Pass number (0=first pass)
  103. * @v interlace Interlace pass to fill in
  104. */
  105. static void png_interlace ( struct png_context *png, unsigned int pass,
  106. struct png_interlace *interlace ) {
  107. unsigned int grid_width_log2;
  108. unsigned int grid_height_log2;
  109. unsigned int x_indent;
  110. unsigned int y_indent;
  111. unsigned int x_stride_log2;
  112. unsigned int y_stride_log2;
  113. unsigned int x_stride;
  114. unsigned int y_stride;
  115. unsigned int width;
  116. unsigned int height;
  117. /* Sanity check */
  118. assert ( png->passes > 0 );
  119. /* Store pass number */
  120. interlace->pass = pass;
  121. /* Calculate interlace grid dimensions */
  122. grid_width_log2 = ( png->passes / 2 );
  123. grid_height_log2 = ( ( png->passes - 1 ) / 2 );
  124. /* Calculate starting indents */
  125. interlace->x_indent = x_indent =
  126. ( ( pass & 1 ) ?
  127. ( 1 << ( grid_width_log2 - ( pass / 2 ) - 1 ) ) : 0 );
  128. interlace->y_indent = y_indent =
  129. ( ( pass && ! ( pass & 1 ) ) ?
  130. ( 1 << ( grid_height_log2 - ( ( pass - 1 ) / 2 ) - 1 ) ) : 0);
  131. /* Calculate strides */
  132. x_stride_log2 = ( grid_width_log2 - ( pass / 2 ) );
  133. y_stride_log2 =
  134. ( grid_height_log2 - ( pass ? ( ( pass - 1 ) / 2 ) : 0 ) );
  135. interlace->x_stride = x_stride = ( 1 << x_stride_log2 );
  136. interlace->y_stride = y_stride = ( 1 << y_stride_log2 );
  137. /* Calculate pass dimensions */
  138. width = png->pixbuf->width;
  139. height = png->pixbuf->height;
  140. interlace->width =
  141. ( ( width - x_indent + x_stride - 1 ) >> x_stride_log2 );
  142. interlace->height =
  143. ( ( height - y_indent + y_stride - 1 ) >> y_stride_log2 );
  144. }
  145. /**
  146. * Calculate PNG pixel length
  147. *
  148. * @v png PNG context
  149. * @ret pixel_len Pixel length
  150. */
  151. static unsigned int png_pixel_len ( struct png_context *png ) {
  152. return ( ( ( png->channels * png->depth ) + 7 ) / 8 );
  153. }
  154. /**
  155. * Calculate PNG scanline length
  156. *
  157. * @v png PNG context
  158. * @v interlace Interlace pass
  159. * @ret scanline_len Scanline length (including filter byte)
  160. */
  161. static size_t png_scanline_len ( struct png_context *png,
  162. struct png_interlace *interlace ) {
  163. return ( 1 /* Filter byte */ +
  164. ( ( interlace->width * png->channels * png->depth ) + 7 ) / 8);
  165. }
  166. /**
  167. * Handle PNG image header chunk
  168. *
  169. * @v image PNG image
  170. * @v png PNG context
  171. * @v len Chunk length
  172. * @ret rc Return status code
  173. */
  174. static int png_image_header ( struct image *image, struct png_context *png,
  175. size_t len ) {
  176. struct png_image_header ihdr;
  177. struct png_interlace interlace;
  178. unsigned int pass;
  179. /* Sanity check */
  180. if ( len != sizeof ( ihdr ) ) {
  181. DBGC ( image, "PNG %s invalid IHDR length %zd\n",
  182. image->name, len );
  183. return -EINVAL;
  184. }
  185. if ( png->pixbuf ) {
  186. DBGC ( image, "PNG %s duplicate IHDR\n", image->name );
  187. return -EINVAL;
  188. }
  189. /* Extract image header */
  190. copy_from_user ( &ihdr, image->data, png->offset, len );
  191. DBGC ( image, "PNG %s %dx%d depth %d type %d compression %d filter %d "
  192. "interlace %d\n", image->name, ntohl ( ihdr.width ),
  193. ntohl ( ihdr.height ), ihdr.depth, ihdr.colour_type,
  194. ihdr.compression, ihdr.filter, ihdr.interlace );
  195. /* Sanity checks */
  196. if ( ihdr.compression >= PNG_COMPRESSION_UNKNOWN ) {
  197. DBGC ( image, "PNG %s unknown compression method %d\n",
  198. image->name, ihdr.compression );
  199. return -ENOTSUP;
  200. }
  201. if ( ihdr.filter >= PNG_FILTER_UNKNOWN ) {
  202. DBGC ( image, "PNG %s unknown filter method %d\n",
  203. image->name, ihdr.filter );
  204. return -ENOTSUP;
  205. }
  206. if ( ihdr.interlace >= PNG_INTERLACE_UNKNOWN ) {
  207. DBGC ( image, "PNG %s unknown interlace method %d\n",
  208. image->name, ihdr.interlace );
  209. return -ENOTSUP;
  210. }
  211. /* Allocate pixel buffer */
  212. png->pixbuf = alloc_pixbuf ( ntohl ( ihdr.width ),
  213. ntohl ( ihdr.height ) );
  214. if ( ! png->pixbuf ) {
  215. DBGC ( image, "PNG %s could not allocate pixel buffer\n",
  216. image->name );
  217. return -ENOMEM;
  218. }
  219. /* Extract bit depth */
  220. png->depth = ihdr.depth;
  221. if ( ( png->depth == 0 ) ||
  222. ( ( png->depth & ( png->depth - 1 ) ) != 0 ) ) {
  223. DBGC ( image, "PNG %s invalid depth %d\n",
  224. image->name, png->depth );
  225. return -EINVAL;
  226. }
  227. /* Calculate number of channels */
  228. png->colour_type = ihdr.colour_type;
  229. png->channels = 1;
  230. if ( ! ( ihdr.colour_type & PNG_COLOUR_TYPE_PALETTE ) ) {
  231. if ( ihdr.colour_type & PNG_COLOUR_TYPE_RGB )
  232. png->channels += 2;
  233. if ( ihdr.colour_type & PNG_COLOUR_TYPE_ALPHA )
  234. png->channels += 1;
  235. }
  236. /* Calculate number of interlace passes */
  237. png->passes = png_interlace_passes[ihdr.interlace];
  238. /* Calculate length of raw data buffer */
  239. for ( pass = 0 ; pass < png->passes ; pass++ ) {
  240. png_interlace ( png, pass, &interlace );
  241. if ( interlace.width == 0 )
  242. continue;
  243. png->raw.len += ( interlace.height *
  244. png_scanline_len ( png, &interlace ) );
  245. }
  246. /* Allocate raw data buffer */
  247. png->raw.data = umalloc ( png->raw.len );
  248. if ( ! png->raw.data ) {
  249. DBGC ( image, "PNG %s could not allocate data buffer\n",
  250. image->name );
  251. return -ENOMEM;
  252. }
  253. return 0;
  254. }
  255. /**
  256. * Handle PNG palette chunk
  257. *
  258. * @v image PNG image
  259. * @v png PNG context
  260. * @v len Chunk length
  261. * @ret rc Return status code
  262. */
  263. static int png_palette ( struct image *image, struct png_context *png,
  264. size_t len ) {
  265. size_t offset = png->offset;
  266. struct png_palette_entry palette;
  267. unsigned int i;
  268. /* Populate palette */
  269. for ( i = 0 ; i < ( sizeof ( png->palette ) /
  270. sizeof ( png->palette[0] ) ) ; i++ ) {
  271. /* Stop when we run out of palette data */
  272. if ( len < sizeof ( palette ) )
  273. break;
  274. /* Extract palette entry */
  275. copy_from_user ( &palette, image->data, offset,
  276. sizeof ( palette ) );
  277. png->palette[i] = ( ( palette.red << 16 ) |
  278. ( palette.green << 8 ) |
  279. ( palette.blue << 0 ) );
  280. DBGC2 ( image, "PNG %s palette entry %d is %#06x\n",
  281. image->name, i, png->palette[i] );
  282. /* Move to next entry */
  283. offset += sizeof ( palette );
  284. len -= sizeof ( palette );
  285. }
  286. return 0;
  287. }
  288. /**
  289. * Handle PNG image data chunk
  290. *
  291. * @v image PNG image
  292. * @v png PNG context
  293. * @v len Chunk length
  294. * @ret rc Return status code
  295. */
  296. static int png_image_data ( struct image *image, struct png_context *png,
  297. size_t len ) {
  298. struct deflate_chunk in;
  299. int rc;
  300. /* Deflate this chunk */
  301. deflate_chunk_init ( &in, image->data, png->offset,
  302. ( png->offset + len ) );
  303. if ( ( rc = deflate_inflate ( &png->deflate, &in, &png->raw ) ) != 0 ) {
  304. DBGC ( image, "PNG %s could not decompress: %s\n",
  305. image->name, strerror ( rc ) );
  306. return rc;
  307. }
  308. return 0;
  309. }
  310. /**
  311. * Unfilter byte using the "None" filter
  312. *
  313. * @v current Filtered current byte
  314. * @v left Unfiltered left byte
  315. * @v above Unfiltered above byte
  316. * @v above_left Unfiltered above-left byte
  317. * @ret current Unfiltered current byte
  318. */
  319. static unsigned int png_unfilter_none ( unsigned int current,
  320. unsigned int left __unused,
  321. unsigned int above __unused,
  322. unsigned int above_left __unused ) {
  323. return current;
  324. }
  325. /**
  326. * Unfilter byte using the "Sub" filter
  327. *
  328. * @v current Filtered current byte
  329. * @v left Unfiltered left byte
  330. * @v above Unfiltered above byte
  331. * @v above_left Unfiltered above-left byte
  332. * @ret current Unfiltered current byte
  333. */
  334. static unsigned int png_unfilter_sub ( unsigned int current,
  335. unsigned int left,
  336. unsigned int above __unused,
  337. unsigned int above_left __unused ) {
  338. return ( current + left );
  339. }
  340. /**
  341. * Unfilter byte using the "Up" filter
  342. *
  343. * @v current Filtered current byte
  344. * @v left Unfiltered left byte
  345. * @v above Unfiltered above byte
  346. * @v above_left Unfiltered above-left byte
  347. * @ret current Unfiltered current byte
  348. */
  349. static unsigned int png_unfilter_up ( unsigned int current,
  350. unsigned int left __unused,
  351. unsigned int above,
  352. unsigned int above_left __unused ) {
  353. return ( current + above );
  354. }
  355. /**
  356. * Unfilter byte using the "Average" filter
  357. *
  358. * @v current Filtered current byte
  359. * @v left Unfiltered left byte
  360. * @v above Unfiltered above byte
  361. * @v above_left Unfiltered above-left byte
  362. * @ret current Unfiltered current byte
  363. */
  364. static unsigned int png_unfilter_average ( unsigned int current,
  365. unsigned int left,
  366. unsigned int above,
  367. unsigned int above_left __unused ) {
  368. return ( current + ( ( above + left ) >> 1 ) );
  369. }
  370. /**
  371. * Paeth predictor function (defined in RFC 2083)
  372. *
  373. * @v a Pixel A
  374. * @v b Pixel B
  375. * @v c Pixel C
  376. * @ret predictor Predictor pixel
  377. */
  378. static unsigned int png_paeth_predictor ( unsigned int a, unsigned int b,
  379. unsigned int c ) {
  380. unsigned int p;
  381. unsigned int pa;
  382. unsigned int pb;
  383. unsigned int pc;
  384. /* Algorithm as defined in RFC 2083 section 6.6 */
  385. p = ( a + b - c );
  386. pa = abs ( p - a );
  387. pb = abs ( p - b );
  388. pc = abs ( p - c );
  389. if ( ( pa <= pb ) && ( pa <= pc ) ) {
  390. return a;
  391. } else if ( pb <= pc ) {
  392. return b;
  393. } else {
  394. return c;
  395. }
  396. }
  397. /**
  398. * Unfilter byte using the "Paeth" filter
  399. *
  400. * @v current Filtered current byte
  401. * @v above_left Unfiltered above-left byte
  402. * @v above Unfiltered above byte
  403. * @v left Unfiltered left byte
  404. * @ret current Unfiltered current byte
  405. */
  406. static unsigned int png_unfilter_paeth ( unsigned int current,
  407. unsigned int left,
  408. unsigned int above,
  409. unsigned int above_left ) {
  410. return ( current + png_paeth_predictor ( left, above, above_left ) );
  411. }
  412. /** A PNG filter */
  413. struct png_filter {
  414. /**
  415. * Unfilter byte
  416. *
  417. * @v current Filtered current byte
  418. * @v left Unfiltered left byte
  419. * @v above Unfiltered above byte
  420. * @v above_left Unfiltered above-left byte
  421. * @ret current Unfiltered current byte
  422. */
  423. unsigned int ( * unfilter ) ( unsigned int current,
  424. unsigned int left,
  425. unsigned int above,
  426. unsigned int above_left );
  427. };
  428. /** PNG filter types */
  429. static struct png_filter png_filters[] = {
  430. [PNG_FILTER_BASIC_NONE] = { png_unfilter_none },
  431. [PNG_FILTER_BASIC_SUB] = { png_unfilter_sub },
  432. [PNG_FILTER_BASIC_UP] = { png_unfilter_up },
  433. [PNG_FILTER_BASIC_AVERAGE] = { png_unfilter_average },
  434. [PNG_FILTER_BASIC_PAETH] = { png_unfilter_paeth },
  435. };
  436. /**
  437. * Unfilter one interlace pass of PNG raw data
  438. *
  439. * @v image PNG image
  440. * @v png PNG context
  441. * @v interlace Interlace pass
  442. * @ret rc Return status code
  443. *
  444. * This routine may assume that it is impossible to overrun the raw
  445. * data buffer, since the size is determined by the image dimensions.
  446. */
  447. static int png_unfilter_pass ( struct image *image, struct png_context *png,
  448. struct png_interlace *interlace ) {
  449. size_t offset = png->raw.offset;
  450. size_t pixel_len = png_pixel_len ( png );
  451. size_t scanline_len = png_scanline_len ( png, interlace );
  452. struct png_filter *filter;
  453. unsigned int scanline;
  454. unsigned int byte;
  455. uint8_t filter_type;
  456. uint8_t left;
  457. uint8_t above;
  458. uint8_t above_left;
  459. uint8_t current;
  460. /* On the first scanline of a pass, above bytes are assumed to
  461. * be zero.
  462. */
  463. above = 0;
  464. /* Iterate over each scanline in turn */
  465. for ( scanline = 0 ; scanline < interlace->height ; scanline++ ) {
  466. /* Extract filter byte and determine filter type */
  467. copy_from_user ( &filter_type, png->raw.data, offset++,
  468. sizeof ( filter_type ) );
  469. if ( filter_type >= ( sizeof ( png_filters ) /
  470. sizeof ( png_filters[0] ) ) ) {
  471. DBGC ( image, "PNG %s unknown filter type %d\n",
  472. image->name, filter_type );
  473. return -ENOTSUP;
  474. }
  475. filter = &png_filters[filter_type];
  476. assert ( filter->unfilter != NULL );
  477. DBGC2 ( image, "PNG %s pass %d scanline %d filter type %d\n",
  478. image->name, interlace->pass, scanline, filter_type );
  479. /* At the start of a line, both above-left and left
  480. * bytes are taken to be zero.
  481. */
  482. left = 0;
  483. above_left = 0;
  484. /* Iterate over each byte (not pixel) in turn */
  485. for ( byte = 0 ; byte < ( scanline_len - 1 ) ; byte++ ) {
  486. /* Extract predictor bytes, if applicable */
  487. if ( byte >= pixel_len ) {
  488. copy_from_user ( &left, png->raw.data,
  489. ( offset - pixel_len ),
  490. sizeof ( left ) );
  491. }
  492. if ( scanline > 0 ) {
  493. copy_from_user ( &above, png->raw.data,
  494. ( offset - scanline_len ),
  495. sizeof ( above ) );
  496. }
  497. if ( ( scanline > 0 ) && ( byte >= pixel_len ) ) {
  498. copy_from_user ( &above_left, png->raw.data,
  499. ( offset - scanline_len -
  500. pixel_len ),
  501. sizeof ( above_left ) );
  502. }
  503. /* Unfilter current byte */
  504. copy_from_user ( &current, png->raw.data,
  505. offset, sizeof ( current ) );
  506. current = filter->unfilter ( current, left, above,
  507. above_left );
  508. copy_to_user ( png->raw.data, offset++,
  509. &current, sizeof ( current ) );
  510. }
  511. }
  512. /* Update offset */
  513. png->raw.offset = offset;
  514. return 0;
  515. }
  516. /**
  517. * Unfilter PNG raw data
  518. *
  519. * @v image PNG image
  520. * @v png PNG context
  521. * @ret rc Return status code
  522. *
  523. * This routine may assume that it is impossible to overrun the raw
  524. * data buffer, since the size is determined by the image dimensions.
  525. */
  526. static int png_unfilter ( struct image *image, struct png_context *png ) {
  527. struct png_interlace interlace;
  528. unsigned int pass;
  529. int rc;
  530. /* Process each interlace pass */
  531. png->raw.offset = 0;
  532. for ( pass = 0 ; pass < png->passes ; pass++ ) {
  533. /* Calculate interlace pass parameters */
  534. png_interlace ( png, pass, &interlace );
  535. /* Skip zero-width rows (which have no filter bytes) */
  536. if ( interlace.width == 0 )
  537. continue;
  538. /* Unfilter this pass */
  539. if ( ( rc = png_unfilter_pass ( image, png,
  540. &interlace ) ) != 0 )
  541. return rc;
  542. }
  543. assert ( png->raw.offset == png->raw.len );
  544. return 0;
  545. }
  546. /**
  547. * Calculate PNG pixel component value
  548. *
  549. * @v raw Raw component value
  550. * @v alpha Alpha value
  551. * @v max Maximum raw/alpha value
  552. * @ret value Component value in range 0-255
  553. */
  554. static inline unsigned int png_pixel ( unsigned int raw, unsigned int alpha,
  555. unsigned int max ) {
  556. /* The basic calculation is 255*(raw/max)*(value/max). We use
  557. * fixed-point arithmetic (scaling up to the maximum range for
  558. * a 32-bit integer), in order to get the same results for
  559. * alpha blending as the test cases (produced using
  560. * ImageMagick).
  561. */
  562. return ( ( ( ( ( 0xff00 * raw * alpha ) / max ) / max ) + 0x80 ) >> 8 );
  563. }
  564. /**
  565. * Fill one interlace pass of PNG pixels
  566. *
  567. * @v image PNG image
  568. * @v png PNG context
  569. * @v interlace Interlace pass
  570. *
  571. * This routine may assume that it is impossible to overrun either the
  572. * raw data buffer or the pixel buffer, since the sizes of both are
  573. * determined by the image dimensions.
  574. */
  575. static void png_pixels_pass ( struct image *image,
  576. struct png_context *png,
  577. struct png_interlace *interlace ) {
  578. size_t raw_offset = png->raw.offset;
  579. uint8_t channel[png->channels];
  580. int is_indexed = ( png->colour_type & PNG_COLOUR_TYPE_PALETTE );
  581. int is_rgb = ( png->colour_type & PNG_COLOUR_TYPE_RGB );
  582. int has_alpha = ( png->colour_type & PNG_COLOUR_TYPE_ALPHA );
  583. size_t pixbuf_y_offset;
  584. size_t pixbuf_offset;
  585. size_t pixbuf_x_stride;
  586. size_t pixbuf_y_stride;
  587. size_t raw_stride;
  588. unsigned int y;
  589. unsigned int x;
  590. unsigned int c;
  591. unsigned int bits;
  592. unsigned int depth;
  593. unsigned int max;
  594. unsigned int alpha;
  595. unsigned int raw;
  596. unsigned int value;
  597. uint8_t current = 0;
  598. uint32_t pixel;
  599. /* We only ever use the top byte of 16-bit pixels. Model this
  600. * as a bit depth of 8 with a stride of more than one.
  601. */
  602. depth = png->depth;
  603. raw_stride = ( ( depth + 7 ) / 8 );
  604. if ( depth > 8 )
  605. depth = 8;
  606. max = ( ( 1 << depth ) - 1 );
  607. /* Calculate pixel buffer offset and strides */
  608. pixbuf_y_offset = ( ( ( interlace->y_indent * png->pixbuf->width ) +
  609. interlace->x_indent ) * sizeof ( pixel ) );
  610. pixbuf_x_stride = ( interlace->x_stride * sizeof ( pixel ) );
  611. pixbuf_y_stride = ( interlace->y_stride * png->pixbuf->width *
  612. sizeof ( pixel ) );
  613. DBGC2 ( image, "PNG %s pass %d %dx%d at (%d,%d) stride (%d,%d)\n",
  614. image->name, interlace->pass, interlace->width,
  615. interlace->height, interlace->x_indent, interlace->y_indent,
  616. interlace->x_stride, interlace->y_stride );
  617. /* Iterate over each scanline in turn */
  618. for ( y = 0 ; y < interlace->height ; y++ ) {
  619. /* Skip filter byte */
  620. raw_offset++;
  621. /* Iterate over each pixel in turn */
  622. bits = depth;
  623. pixbuf_offset = pixbuf_y_offset;
  624. for ( x = 0 ; x < interlace->width ; x++ ) {
  625. /* Extract sample value */
  626. for ( c = 0 ; c < png->channels ; c++ ) {
  627. /* Get sample value into high bits of current */
  628. current <<= depth;
  629. bits -= depth;
  630. if ( ! bits ) {
  631. copy_from_user ( &current,
  632. png->raw.data,
  633. raw_offset,
  634. sizeof ( current ) );
  635. raw_offset += raw_stride;
  636. bits = 8;
  637. }
  638. /* Extract sample value */
  639. channel[c] = ( current >> ( 8 - depth ) );
  640. }
  641. /* Convert to native pixel format */
  642. if ( is_indexed ) {
  643. /* Indexed */
  644. pixel = png->palette[channel[0]];
  645. } else {
  646. /* Determine alpha value */
  647. alpha = ( has_alpha ?
  648. channel[ png->channels - 1 ] : max );
  649. /* Convert to RGB value */
  650. pixel = 0;
  651. for ( c = 0 ; c < 3 ; c++ ) {
  652. raw = channel[ is_rgb ? c : 0 ];
  653. value = png_pixel ( raw, alpha, max );
  654. assert ( value <= 255 );
  655. pixel = ( ( pixel << 8 ) | value );
  656. }
  657. }
  658. /* Store pixel */
  659. copy_to_user ( png->pixbuf->data, pixbuf_offset,
  660. &pixel, sizeof ( pixel ) );
  661. pixbuf_offset += pixbuf_x_stride;
  662. }
  663. /* Move to next output row */
  664. pixbuf_y_offset += pixbuf_y_stride;
  665. }
  666. /* Update offset */
  667. png->raw.offset = raw_offset;
  668. }
  669. /**
  670. * Fill PNG pixels
  671. *
  672. * @v image PNG image
  673. * @v png PNG context
  674. *
  675. * This routine may assume that it is impossible to overrun either the
  676. * raw data buffer or the pixel buffer, since the sizes of both are
  677. * determined by the image dimensions.
  678. */
  679. static void png_pixels ( struct image *image, struct png_context *png ) {
  680. struct png_interlace interlace;
  681. unsigned int pass;
  682. /* Process each interlace pass */
  683. png->raw.offset = 0;
  684. for ( pass = 0 ; pass < png->passes ; pass++ ) {
  685. /* Calculate interlace pass parameters */
  686. png_interlace ( png, pass, &interlace );
  687. /* Skip zero-width rows (which have no filter bytes) */
  688. if ( interlace.width == 0 )
  689. continue;
  690. /* Unfilter this pass */
  691. png_pixels_pass ( image, png, &interlace );
  692. }
  693. assert ( png->raw.offset == png->raw.len );
  694. }
  695. /**
  696. * Handle PNG image end chunk
  697. *
  698. * @v image PNG image
  699. * @v png PNG context
  700. * @v len Chunk length
  701. * @ret rc Return status code
  702. */
  703. static int png_image_end ( struct image *image, struct png_context *png,
  704. size_t len ) {
  705. int rc;
  706. /* Sanity checks */
  707. if ( len != 0 ) {
  708. DBGC ( image, "PNG %s invalid IEND length %zd\n",
  709. image->name, len );
  710. return -EINVAL;
  711. }
  712. if ( ! png->pixbuf ) {
  713. DBGC ( image, "PNG %s missing pixel buffer (no IHDR?)\n",
  714. image->name );
  715. return -EINVAL;
  716. }
  717. if ( ! deflate_finished ( &png->deflate ) ) {
  718. DBGC ( image, "PNG %s decompression not complete\n",
  719. image->name );
  720. return -EINVAL;
  721. }
  722. if ( png->raw.offset != png->raw.len ) {
  723. DBGC ( image, "PNG %s incorrect decompressed length (expected "
  724. "%zd, got %zd)\n", image->name, png->raw.len,
  725. png->raw.offset );
  726. return -EINVAL;
  727. }
  728. /* Unfilter raw data */
  729. if ( ( rc = png_unfilter ( image, png ) ) != 0 )
  730. return rc;
  731. /* Fill pixel buffer */
  732. png_pixels ( image, png );
  733. return 0;
  734. }
  735. /** A PNG chunk handler */
  736. struct png_chunk_handler {
  737. /** Chunk type */
  738. uint32_t type;
  739. /**
  740. * Handle chunk
  741. *
  742. * @v image PNG image
  743. * @v png PNG context
  744. * @v len Chunk length
  745. * @ret rc Return status code
  746. */
  747. int ( * handle ) ( struct image *image, struct png_context *png,
  748. size_t len );
  749. };
  750. /** PNG chunk handlers */
  751. static struct png_chunk_handler png_chunk_handlers[] = {
  752. { htonl ( PNG_TYPE_IHDR ), png_image_header },
  753. { htonl ( PNG_TYPE_PLTE ), png_palette },
  754. { htonl ( PNG_TYPE_IDAT ), png_image_data },
  755. { htonl ( PNG_TYPE_IEND ), png_image_end },
  756. };
  757. /**
  758. * Handle PNG chunk
  759. *
  760. * @v image PNG image
  761. * @v png PNG context
  762. * @v type Chunk type
  763. * @v len Chunk length
  764. * @ret rc Return status code
  765. */
  766. static int png_chunk ( struct image *image, struct png_context *png,
  767. uint32_t type, size_t len ) {
  768. struct png_chunk_handler *handler;
  769. unsigned int i;
  770. DBGC ( image, "PNG %s chunk type %s offset %zd length %zd\n",
  771. image->name, png_type_name ( type ), png->offset, len );
  772. /* Handle according to chunk type */
  773. for ( i = 0 ; i < ( sizeof ( png_chunk_handlers ) /
  774. sizeof ( png_chunk_handlers[0] ) ) ; i++ ) {
  775. handler = &png_chunk_handlers[i];
  776. if ( handler->type == type )
  777. return handler->handle ( image, png, len );
  778. }
  779. /* Fail if unknown chunk type is critical */
  780. if ( ! ( type & htonl ( PNG_CHUNK_ANCILLARY ) ) ) {
  781. DBGC ( image, "PNG %s unknown critical chunk type %s\n",
  782. image->name, png_type_name ( type ) );
  783. return -ENOTSUP;
  784. }
  785. /* Ignore non-critical unknown chunk types */
  786. return 0;
  787. }
  788. /**
  789. * Convert PNG image to pixel buffer
  790. *
  791. * @v image PNG image
  792. * @v pixbuf Pixel buffer to fill in
  793. * @ret rc Return status code
  794. */
  795. static int png_pixbuf ( struct image *image, struct pixel_buffer **pixbuf ) {
  796. struct png_context *png;
  797. struct png_chunk_header header;
  798. struct png_chunk_footer footer;
  799. size_t remaining;
  800. size_t chunk_len;
  801. int rc;
  802. /* Allocate and initialise context */
  803. png = zalloc ( sizeof ( *png ) );
  804. if ( ! png ) {
  805. rc = -ENOMEM;
  806. goto err_alloc;
  807. }
  808. png->offset = sizeof ( struct png_signature );
  809. deflate_init ( &png->deflate, DEFLATE_ZLIB );
  810. /* Process chunks */
  811. do {
  812. /* Extract chunk header */
  813. remaining = ( image->len - png->offset );
  814. if ( remaining < sizeof ( header ) ) {
  815. DBGC ( image, "PNG %s truncated chunk header at offset "
  816. "%zd\n", image->name, png->offset );
  817. rc = -EINVAL;
  818. goto err_truncated;
  819. }
  820. copy_from_user ( &header, image->data, png->offset,
  821. sizeof ( header ) );
  822. png->offset += sizeof ( header );
  823. /* Validate chunk length */
  824. chunk_len = ntohl ( header.len );
  825. if ( remaining < ( sizeof ( header ) + chunk_len +
  826. sizeof ( footer ) ) ) {
  827. DBGC ( image, "PNG %s truncated chunk data/footer at "
  828. "offset %zd\n", image->name, png->offset );
  829. rc = -EINVAL;
  830. goto err_truncated;
  831. }
  832. /* Handle chunk */
  833. if ( ( rc = png_chunk ( image, png, header.type,
  834. chunk_len ) ) != 0 )
  835. goto err_chunk;
  836. /* Move to next chunk */
  837. png->offset += ( chunk_len + sizeof ( footer ) );
  838. } while ( png->offset < image->len );
  839. /* Check that we finished with an IEND chunk */
  840. if ( header.type != htonl ( PNG_TYPE_IEND ) ) {
  841. DBGC ( image, "PNG %s did not finish with IEND\n",
  842. image->name );
  843. rc = -EINVAL;
  844. goto err_iend;
  845. }
  846. /* Return pixel buffer */
  847. *pixbuf = pixbuf_get ( png->pixbuf );
  848. /* Success */
  849. rc = 0;
  850. err_iend:
  851. err_chunk:
  852. err_truncated:
  853. pixbuf_put ( png->pixbuf );
  854. ufree ( png->raw.data );
  855. free ( png );
  856. err_alloc:
  857. return rc;
  858. }
  859. /**
  860. * Probe PNG image
  861. *
  862. * @v image PNG image
  863. * @ret rc Return status code
  864. */
  865. static int png_probe ( struct image *image ) {
  866. struct png_signature signature;
  867. /* Sanity check */
  868. if ( image->len < sizeof ( signature ) ) {
  869. DBGC ( image, "PNG %s is too short\n", image->name );
  870. return -ENOEXEC;
  871. }
  872. /* Check signature */
  873. copy_from_user ( &signature, image->data, 0, sizeof ( signature ) );
  874. if ( memcmp ( &signature, &png_signature, sizeof ( signature ) ) != 0 ){
  875. DBGC ( image, "PNG %s has invalid signature\n", image->name );
  876. return -ENOEXEC;
  877. }
  878. return 0;
  879. }
  880. /** PNG image type */
  881. struct image_type png_image_type __image_type ( PROBE_NORMAL ) = {
  882. .name = "PNG",
  883. .probe = png_probe,
  884. .pixbuf = png_pixbuf,
  885. };