KHtml

pngloader.cpp
1 /*
2  Large image load library -- PNG decoder
3 
4  Copyright (C) 2004 Maksim Orlovich <[email protected]>
5 
6  Permission is hereby granted, free of charge, to any person obtaining a copy
7  of this software and associated documentation files (the "Software"), to deal
8  in the Software without restriction, including without limitation the rights
9  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  copies of the Software, and to permit persons to whom the Software is
11  furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
20  AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 
23 */
24 
25 #include "pngloader.h"
26 #include "imageloader.h"
27 #include "imagemanager.h"
28 
29 #include <png.h>
30 
31 namespace khtmlImLoad
32 {
33 
34 class PNGLoader;
35 /**
36 Paranoia: since g++ may very well store "this" in a register, we
37 use and external volatile to refer to our instance in case libPNG longjumps
38 on us. We also use this variable to find where to map libPNG's callbacks.
39 */
40 static volatile PNGLoader *curLoader;
41 
42 class PNGLoader: public ImageLoader
43 {
44 private:
45  png_structp pngReadStruct;
46  png_infop pngInfoStruct;
47 
48  bool libPngError;
49  bool done;
50 
51  bool interlaced;
52 
53  png_uint_32 width;
54  png_uint_32 height;
55  unsigned int depth;
56 
57  unsigned char *scanlineBuf; //Used only for interlaced images
58 
59  /**
60  Dispatch hooks for libPNG callbacks -- call instance methods,
61  and make parameters slightly more our-style
62  */
63  static void dispHaveInfo(png_structp, png_infop)
64  {
65  const_cast<PNGLoader *>(curLoader)->haveInfo();
66  }
67 
68  static void dispHaveRow(png_structp, png_bytep row, png_uint_32 rowNum, int pass)
69  {
70  const_cast<PNGLoader *>(curLoader)->haveRow(rowNum, pass, row);
71  }
72 
73  static void dispHaveEnd(png_structp, png_infop)
74  {
75  const_cast<PNGLoader *>(curLoader)->haveEnd();
76  }
77 
78  /**
79  Our implementation of libPNG callbacks
80  */
81  void haveInfo()
82  {
83  int bitDepth, colorType, interlaceType;
84 
85  png_get_IHDR(pngReadStruct, pngInfoStruct, &width, &height, &bitDepth,
86  &colorType, &interlaceType, nullptr, nullptr);
87 
88  if (!ImageManager::isAcceptableSize(width, height)) {
89  libPngError = true;
90  return;
91  }
92 
93  //Ask libPNG to change bit depths we don't support
94  if (bitDepth < 8)
95 #if PNG_LIBPNG_VER < 10400
96  png_set_gray_1_2_4_to_8(pngReadStruct);
97 #else
98  png_set_expand_gray_1_2_4_to_8(pngReadStruct);
99 #endif
100 
101  if (bitDepth > 8) {
102  png_set_strip_16(pngReadStruct);
103  }
104 
105  //Some images (basically, only paletted ones) may have alpha
106  //included as part of a tRNS chunk. We want to convert that to regular alpha
107  //channel..
108  bool haveTRNS = false;
109  if (png_get_valid(pngReadStruct, pngInfoStruct, PNG_INFO_tRNS)) {
110  png_set_tRNS_to_alpha(pngReadStruct);
111  haveTRNS = true;
112 
113  if (colorType == PNG_COLOR_TYPE_RGB) {
114  colorType = PNG_COLOR_TYPE_RGB_ALPHA; //Paranoia..
115  } else if (colorType == PNG_COLOR_TYPE_GRAY) {
116  colorType = PNG_COLOR_TYPE_GRAY_ALPHA;
117  }
118  }
119 
120  ImageFormat imFrm;
121 
122  //Prepare for mapping from colorType to our format descriptors.
123  switch (colorType) {
124  case PNG_COLOR_TYPE_GRAY:
125  imFrm.greyscaleSetup();
126  break;
127  case PNG_COLOR_TYPE_GRAY_ALPHA:
128  //We don't natively support 8-bit plus alpha, so ask libPNG to expand it out to RGB
129  png_set_gray_to_rgb(pngReadStruct);
130  imFrm.type = ImageFormat::Image_ARGB_32;
131  break;
132  case PNG_COLOR_TYPE_PALETTE:
133  //For now, we handle paletted images as RGB or ARGB
134  //### TODO: handle non-alpha paletted images with a sufficiently small palette as
135  //paletted
136  imFrm.type = haveTRNS ? ImageFormat::Image_ARGB_32 : ImageFormat::Image_RGB_32;
137  png_set_palette_to_rgb(pngReadStruct);
138  break;
139  case PNG_COLOR_TYPE_RGB:
140  imFrm.type = ImageFormat::Image_RGB_32;
141  break;
142  case PNG_COLOR_TYPE_RGB_ALPHA:
143  imFrm.type = ImageFormat::Image_ARGB_32;
144  break;
145  default:
146  //Huh?
147  libPngError = true;
148  return;
149  }
150 
151  //Configure padding/byte swapping if need be (32-bit images)
152  //We want a 32-bit value with ARGB.
153  //This means that for little-endian, in memory we should have BGRA,
154  //and for big-endian, well, ARGB
155  if (imFrm.type == ImageFormat::Image_RGB_32) {
156  //Need fillers, plus perhaps BGR swapping for non-alpha
157 #if Q_BYTE_ORDER == Q_BIG_ENDIAN
158  png_set_filler(pngReadStruct, 0xff, PNG_FILLER_BEFORE);
159 #else
160  png_set_filler(pngReadStruct, 0xff, PNG_FILLER_AFTER);
161  png_set_bgr(pngReadStruct);
162 #endif
163  } else if (imFrm.type == ImageFormat::Image_ARGB_32) {
164 #if Q_BYTE_ORDER == Q_BIG_ENDIAN
165  png_set_swap_alpha(pngReadStruct); //ARGB, not RGBA
166 #else
167  png_set_bgr(pngReadStruct); //BGRA
168 #endif
169  }
170 
171  //Remember depth, for our own use
172  depth = imFrm.depth();
173 
174  //handle interlacing
175  if (interlaceType != PNG_INTERLACE_NONE) {
176  interlaced = true;
177  scanlineBuf = new unsigned char[depth * width];
178  png_set_interlace_handling(pngReadStruct);
179 
180  // Give up on premultiply in this case..
181  if (imFrm.type == ImageFormat::Image_ARGB_32) {
182  imFrm.type = ImageFormat::Image_ARGB_32_DontPremult;
183  }
184  }
185 
186  notifySingleFrameImage(width, height, imFrm);
187 
188  //OK, time to start input
189  png_read_update_info(pngReadStruct, pngInfoStruct);
190  }
191 
192  void haveRow(unsigned int rowNum, int pass, unsigned char *data)
193  {
194  if (interlaced) {
195  Q_ASSERT(png_get_bit_depth(pngReadStruct, pngInfoStruct) <= depth * 8);
196  requestScanline(rowNum, scanlineBuf);
197  png_progressive_combine_row(pngReadStruct, scanlineBuf, data);
198  notifyScanline(pass + 1, scanlineBuf);
199  } else {
200  notifyScanline(pass + 1, data);
201  }
202  }
203 
204  void haveEnd()
205  {
206  done = true;
207  }
208 
209 public:
210  PNGLoader()
211  {
212  done = false;
213  libPngError = false;
214 
215  interlaced = false;
216  scanlineBuf = nullptr;
217 
218  //Setup the basic structures.
219  //### I think I want to pass the handlers to shut it up?
220  pngReadStruct = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
221 
222  pngInfoStruct = png_create_info_struct(pngReadStruct);
223 
224  //Prepare for progressive loading
225  png_set_progressive_read_fn(pngReadStruct, nullptr, dispHaveInfo, dispHaveRow, dispHaveEnd);
226  }
227 
228  ~PNGLoader()
229  {
230  delete[] scanlineBuf;
231 
232  png_destroy_read_struct(&pngReadStruct, &pngInfoStruct, nullptr);
233  //### CHECKME!
234  }
235 
236  int processData(uchar *data, int length) override
237  {
238  if (done) {
239  return Done;
240  }
241 
242  //This bit takes care of dealing with the libPNG error handling
243  if (libPngError) {
244  return Error;
245  }
246 
247  curLoader = this;
248  if (setjmp(png_jmpbuf(pngReadStruct))) {
249  curLoader->libPngError = true;
250  return Error;
251  }
252 
253  //OK, now we can actually do work.... Push data to libPNG,
254  //it will use callbacks
255  //### time limiting?
256  png_process_data(pngReadStruct, pngInfoStruct, data, length);
257 
258  return length;
259  }
260 };
261 
262 ImageLoaderProvider::Type PNGLoaderProvider::type()
263 {
264  return Efficient;
265 }
266 
267 ImageLoader *PNGLoaderProvider::loaderFor(const QByteArray &prefix)
268 {
269  uchar *data = (uchar *)prefix.data();
270  if (prefix.size() < 8) {
271  return nullptr;
272  }
273 
274  if (data[0] == 0x89 &&
275  data[1] == 'P' &&
276  data[2] == 'N' &&
277  data[3] == 'G' &&
278  data[4] == 0x0D &&
279  data[5] == 0x0A &&
280  data[6] == 0x1A &&
281  data[7] == 0x0A) {
282  return new PNGLoader;
283  }
284 
285  return nullptr;
286 }
287 
288 }
289 
void notifyScanline(unsigned char version, unsigned char *line)
Call to provide a scanline, for active frame, and given "version".
Definition: imageloader.h:81
void notifySingleFrameImage(int width, int height, const ImageFormat &format)
wrapper for the above method for single-frame images
Definition: imageloader.h:69
char * data()
int size() const const
void requestScanline(unsigned int lineNum, unsigned char *lineBuf)
Call this to exract the current state of a scanline to the provided bufer.
Definition: imageloader.h:94
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Oct 25 2021 22:48:19 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.