This repository has been archived by the owner on Jan 12, 2022. It is now read-only.
forked from mozilla/mozilla-central
/
nsPNGDecoder.cpp
893 lines (753 loc) · 28 KB
/
nsPNGDecoder.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 2001
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Stuart Parmenter <stuart@mozilla.com>
* Andrew Smith
* Federico Mena-Quintero <federico@novell.com>
* Bobby Holley <bobbyholley@gmail.com>
* Glenn Randers-Pehrson <glennrp@gmail.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "nsPNGDecoder.h"
#include "ImageLogging.h"
#include "nsMemory.h"
#include "nsRect.h"
#include "nsIInputStream.h"
#include "RasterImage.h"
#include "imgIContainerObserver.h"
#include "gfxColor.h"
#include "nsColor.h"
#include "nspr.h"
#include "png.h"
#include "gfxPlatform.h"
namespace mozilla {
namespace image {
#ifdef PR_LOGGING
static PRLogModuleInfo *gPNGLog = PR_NewLogModule("PNGDecoder");
static PRLogModuleInfo *gPNGDecoderAccountingLog =
PR_NewLogModule("PNGDecoderAccounting");
#endif
/* limit image dimensions (bug #251381) */
#define MOZ_PNG_MAX_DIMENSION 1000000L
// For size decodes
#define WIDTH_OFFSET 16
#define HEIGHT_OFFSET (WIDTH_OFFSET + 4)
#define BYTES_NEEDED_FOR_DIMENSIONS (HEIGHT_OFFSET + 4)
// First 8 bytes of a PNG file
const PRUint8
nsPNGDecoder::pngSignatureBytes[] = { 137, 80, 78, 71, 13, 10, 26, 10 };
nsPNGDecoder::nsPNGDecoder(RasterImage &aImage, imgIDecoderObserver* aObserver)
: Decoder(aImage, aObserver),
mPNG(nsnull), mInfo(nsnull),
mCMSLine(nsnull), interlacebuf(nsnull),
mInProfile(nsnull), mTransform(nsnull),
mHeaderBuf(nsnull), mHeaderBytesRead(0),
mChannels(0), mFrameIsHidden(false),
mCMSMode(0), mDisablePremultipliedAlpha(false)
{
}
nsPNGDecoder::~nsPNGDecoder()
{
if (mPNG)
png_destroy_read_struct(&mPNG, mInfo ? &mInfo : NULL, NULL);
if (mCMSLine)
nsMemory::Free(mCMSLine);
if (interlacebuf)
nsMemory::Free(interlacebuf);
if (mInProfile) {
qcms_profile_release(mInProfile);
/* mTransform belongs to us only if mInProfile is non-null */
if (mTransform)
qcms_transform_release(mTransform);
}
if (mHeaderBuf)
nsMemory::Free(mHeaderBuf);
}
// CreateFrame() is used for both simple and animated images
void nsPNGDecoder::CreateFrame(png_uint_32 x_offset, png_uint_32 y_offset,
PRInt32 width, PRInt32 height,
gfxASurface::gfxImageFormat format)
{
PRUint32 imageDataLength;
nsresult rv = mImage.EnsureFrame(GetFrameCount(), x_offset, y_offset,
width, height, format,
&mImageData, &imageDataLength);
if (NS_FAILED(rv))
longjmp(png_jmpbuf(mPNG), 5); // NS_ERROR_OUT_OF_MEMORY
mFrameRect.x = x_offset;
mFrameRect.y = y_offset;
mFrameRect.width = width;
mFrameRect.height = height;
#ifdef PNG_APNG_SUPPORTED
if (png_get_valid(mPNG, mInfo, PNG_INFO_acTL))
SetAnimFrameInfo();
#endif
// Tell the superclass we're starting a frame
PostFrameStart();
PR_LOG(gPNGDecoderAccountingLog, PR_LOG_DEBUG,
("PNGDecoderAccounting: nsPNGDecoder::CreateFrame -- created "
"image frame with %dx%d pixels in container %p",
width, height,
&mImage));
mFrameHasNoAlpha = true;
}
#ifdef PNG_APNG_SUPPORTED
// set timeout and frame disposal method for the current frame
void nsPNGDecoder::SetAnimFrameInfo()
{
png_uint_16 delay_num, delay_den;
/* delay, in seconds is delay_num/delay_den */
png_byte dispose_op;
png_byte blend_op;
PRInt32 timeout; /* in milliseconds */
delay_num = png_get_next_frame_delay_num(mPNG, mInfo);
delay_den = png_get_next_frame_delay_den(mPNG, mInfo);
dispose_op = png_get_next_frame_dispose_op(mPNG, mInfo);
blend_op = png_get_next_frame_blend_op(mPNG, mInfo);
if (delay_num == 0) {
timeout = 0; // SetFrameTimeout() will set to a minimum
} else {
if (delay_den == 0)
delay_den = 100; // so says the APNG spec
// Need to cast delay_num to float to have a proper division and
// the result to int to avoid compiler warning
timeout = static_cast<PRInt32>
(static_cast<PRFloat64>(delay_num) * 1000 / delay_den);
}
PRUint32 numFrames = mImage.GetNumFrames();
mImage.SetFrameTimeout(numFrames - 1, timeout);
if (dispose_op == PNG_DISPOSE_OP_PREVIOUS)
mImage.SetFrameDisposalMethod(numFrames - 1,
RasterImage::kDisposeRestorePrevious);
else if (dispose_op == PNG_DISPOSE_OP_BACKGROUND)
mImage.SetFrameDisposalMethod(numFrames - 1,
RasterImage::kDisposeClear);
else
mImage.SetFrameDisposalMethod(numFrames - 1,
RasterImage::kDisposeKeep);
if (blend_op == PNG_BLEND_OP_SOURCE)
mImage.SetFrameBlendMethod(numFrames - 1, RasterImage::kBlendSource);
/*else // 'over' is the default
mImage.SetFrameBlendMethod(numFrames - 1, RasterImage::kBlendOver); */
}
#endif
// set timeout and frame disposal method for the current frame
void nsPNGDecoder::EndImageFrame()
{
if (mFrameIsHidden)
return;
PRUint32 numFrames = 1;
#ifdef PNG_APNG_SUPPORTED
numFrames = mImage.GetNumFrames();
// We can't use mPNG->num_frames_read as it may be one ahead.
if (numFrames > 1) {
// Tell the image renderer that the frame is complete
if (mFrameHasNoAlpha)
mImage.SetFrameHasNoAlpha(numFrames - 1);
// PNG is always non-premult
mImage.SetFrameAsNonPremult(numFrames - 1, true);
PostInvalidation(mFrameRect);
}
#endif
PostFrameStop();
}
void
nsPNGDecoder::InitInternal()
{
mCMSMode = gfxPlatform::GetCMSMode();
if ((mDecodeFlags & DECODER_NO_COLORSPACE_CONVERSION) != 0)
mCMSMode = eCMSMode_Off;
mDisablePremultipliedAlpha = (mDecodeFlags & DECODER_NO_PREMULTIPLY_ALPHA) != 0;
#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
static png_byte color_chunks[]=
{ 99, 72, 82, 77, '\0', /* cHRM */
105, 67, 67, 80, '\0'}; /* iCCP */
static png_byte unused_chunks[]=
{ 98, 75, 71, 68, '\0', /* bKGD */
104, 73, 83, 84, '\0', /* hIST */
105, 84, 88, 116, '\0', /* iTXt */
111, 70, 70, 115, '\0', /* oFFs */
112, 67, 65, 76, '\0', /* pCAL */
115, 67, 65, 76, '\0', /* sCAL */
112, 72, 89, 115, '\0', /* pHYs */
115, 66, 73, 84, '\0', /* sBIT */
115, 80, 76, 84, '\0', /* sPLT */
116, 69, 88, 116, '\0', /* tEXt */
116, 73, 77, 69, '\0', /* tIME */
122, 84, 88, 116, '\0'}; /* zTXt */
#endif
// For size decodes, we only need a small buffer
if (IsSizeDecode()) {
mHeaderBuf = (PRUint8 *)moz_xmalloc(BYTES_NEEDED_FOR_DIMENSIONS);
return;
}
/* For full decodes, do png init stuff */
/* Initialize the container's source image header. */
/* Always decode to 24 bit pixdepth */
mPNG = png_create_read_struct(PNG_LIBPNG_VER_STRING,
NULL, nsPNGDecoder::error_callback,
nsPNGDecoder::warning_callback);
if (!mPNG) {
PostDecoderError(NS_ERROR_OUT_OF_MEMORY);
return;
}
mInfo = png_create_info_struct(mPNG);
if (!mInfo) {
PostDecoderError(NS_ERROR_OUT_OF_MEMORY);
png_destroy_read_struct(&mPNG, NULL, NULL);
return;
}
#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
/* Ignore unused chunks */
if (mCMSMode == eCMSMode_Off)
png_set_keep_unknown_chunks(mPNG, 1, color_chunks, 2);
png_set_keep_unknown_chunks(mPNG, 1, unused_chunks,
(int)sizeof(unused_chunks)/5);
#endif
#ifdef PNG_SET_CHUNK_MALLOC_LIMIT_SUPPORTED
if (mCMSMode != eCMSMode_Off)
png_set_chunk_malloc_max(mPNG, 4000000L);
#endif
/* use this as libpng "progressive pointer" (retrieve in callbacks) */
png_set_progressive_read_fn(mPNG, static_cast<png_voidp>(this),
nsPNGDecoder::info_callback,
nsPNGDecoder::row_callback,
nsPNGDecoder::end_callback);
}
void
nsPNGDecoder::WriteInternal(const char *aBuffer, PRUint32 aCount)
{
// We use gotos, so we need to declare variables here
PRUint32 width = 0;
PRUint32 height = 0;
NS_ABORT_IF_FALSE(!HasError(), "Shouldn't call WriteInternal after error!");
// If we only want width/height, we don't need to go through libpng
if (IsSizeDecode()) {
// Are we done?
if (mHeaderBytesRead == BYTES_NEEDED_FOR_DIMENSIONS)
return;
// Read data into our header buffer
PRUint32 bytesToRead = NS_MIN(aCount, BYTES_NEEDED_FOR_DIMENSIONS -
mHeaderBytesRead);
memcpy(mHeaderBuf + mHeaderBytesRead, aBuffer, bytesToRead);
mHeaderBytesRead += bytesToRead;
// If we're done now, verify the data and set up the container
if (mHeaderBytesRead == BYTES_NEEDED_FOR_DIMENSIONS) {
// Check that the signature bytes are right
if (memcmp(mHeaderBuf, nsPNGDecoder::pngSignatureBytes,
sizeof(pngSignatureBytes))) {
PostDataError();
return;
}
// Grab the width and height, accounting for endianness (thanks libpng!)
width = png_get_uint_32(mHeaderBuf + WIDTH_OFFSET);
height = png_get_uint_32(mHeaderBuf + HEIGHT_OFFSET);
// Too big?
if ((width > MOZ_PNG_MAX_DIMENSION) || (height > MOZ_PNG_MAX_DIMENSION)) {
PostDataError();
return;
}
// Post our size to the superclass
PostSize(width, height);
}
}
// Otherwise, we're doing a standard decode
else {
// libpng uses setjmp/longjmp for error handling - set the buffer
if (setjmp(png_jmpbuf(mPNG))) {
// We might not really know what caused the error, but it makes more
// sense to blame the data.
if (!HasError())
PostDataError();
png_destroy_read_struct(&mPNG, &mInfo, NULL);
return;
}
// Pass the data off to libpng
png_process_data(mPNG, mInfo, (unsigned char *)aBuffer, aCount);
}
}
// Sets up gamma pre-correction in libpng before our callback gets called.
// We need to do this if we don't end up with a CMS profile.
static void
PNGDoGammaCorrection(png_structp png_ptr, png_infop info_ptr)
{
double aGamma;
if (png_get_gAMA(png_ptr, info_ptr, &aGamma)) {
if ((aGamma <= 0.0) || (aGamma > 21474.83)) {
aGamma = 0.45455;
png_set_gAMA(png_ptr, info_ptr, aGamma);
}
png_set_gamma(png_ptr, 2.2, aGamma);
}
else
png_set_gamma(png_ptr, 2.2, 0.45455);
}
// Adapted from http://www.littlecms.com/pngchrm.c example code
static qcms_profile *
PNGGetColorProfile(png_structp png_ptr, png_infop info_ptr,
int color_type, qcms_data_type *inType, PRUint32 *intent)
{
qcms_profile *profile = nsnull;
*intent = QCMS_INTENT_PERCEPTUAL; // Our default
// First try to see if iCCP chunk is present
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_iCCP)) {
png_uint_32 profileLen;
#if (PNG_LIBPNG_VER < 10500)
char *profileData, *profileName;
#else
png_bytep profileData;
png_charp profileName;
#endif
int compression;
png_get_iCCP(png_ptr, info_ptr, &profileName, &compression,
&profileData, &profileLen);
profile = qcms_profile_from_memory(
#if (PNG_LIBPNG_VER < 10500)
profileData,
#else
(char *)profileData,
#endif
profileLen);
if (profile) {
PRUint32 profileSpace = qcms_profile_get_color_space(profile);
bool mismatch = false;
if (color_type & PNG_COLOR_MASK_COLOR) {
if (profileSpace != icSigRgbData)
mismatch = true;
} else {
if (profileSpace == icSigRgbData)
png_set_gray_to_rgb(png_ptr);
else if (profileSpace != icSigGrayData)
mismatch = true;
}
if (mismatch) {
qcms_profile_release(profile);
profile = nsnull;
} else {
*intent = qcms_profile_get_rendering_intent(profile);
}
}
}
// Check sRGB chunk
if (!profile && png_get_valid(png_ptr, info_ptr, PNG_INFO_sRGB)) {
profile = qcms_profile_sRGB();
if (profile) {
int fileIntent;
png_set_gray_to_rgb(png_ptr);
png_get_sRGB(png_ptr, info_ptr, &fileIntent);
PRUint32 map[] = { QCMS_INTENT_PERCEPTUAL,
QCMS_INTENT_RELATIVE_COLORIMETRIC,
QCMS_INTENT_SATURATION,
QCMS_INTENT_ABSOLUTE_COLORIMETRIC };
*intent = map[fileIntent];
}
}
// Check gAMA/cHRM chunks
if (!profile &&
png_get_valid(png_ptr, info_ptr, PNG_INFO_gAMA) &&
png_get_valid(png_ptr, info_ptr, PNG_INFO_cHRM)) {
qcms_CIE_xyYTRIPLE primaries;
qcms_CIE_xyY whitePoint;
png_get_cHRM(png_ptr, info_ptr,
&whitePoint.x, &whitePoint.y,
&primaries.red.x, &primaries.red.y,
&primaries.green.x, &primaries.green.y,
&primaries.blue.x, &primaries.blue.y);
whitePoint.Y =
primaries.red.Y = primaries.green.Y = primaries.blue.Y = 1.0;
double gammaOfFile;
png_get_gAMA(png_ptr, info_ptr, &gammaOfFile);
profile = qcms_profile_create_rgb_with_gamma(whitePoint, primaries,
1.0/gammaOfFile);
if (profile)
png_set_gray_to_rgb(png_ptr);
}
if (profile) {
PRUint32 profileSpace = qcms_profile_get_color_space(profile);
if (profileSpace == icSigGrayData) {
if (color_type & PNG_COLOR_MASK_ALPHA)
*inType = QCMS_DATA_GRAYA_8;
else
*inType = QCMS_DATA_GRAY_8;
} else {
if (color_type & PNG_COLOR_MASK_ALPHA ||
png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
*inType = QCMS_DATA_RGBA_8;
else
*inType = QCMS_DATA_RGB_8;
}
}
return profile;
}
void
nsPNGDecoder::info_callback(png_structp png_ptr, png_infop info_ptr)
{
/* int number_passes; NOT USED */
png_uint_32 width, height;
int bit_depth, color_type, interlace_type, compression_type, filter_type;
unsigned int channels;
png_bytep trans = NULL;
int num_trans = 0;
nsPNGDecoder *decoder =
static_cast<nsPNGDecoder*>(png_get_progressive_ptr(png_ptr));
/* always decode to 24-bit RGB or 32-bit RGBA */
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
&interlace_type, &compression_type, &filter_type);
/* Are we too big? */
if (width > MOZ_PNG_MAX_DIMENSION || height > MOZ_PNG_MAX_DIMENSION)
longjmp(png_jmpbuf(decoder->mPNG), 1);
// Post our size to the superclass
decoder->PostSize(width, height);
if (decoder->HasError()) {
// Setting the size led to an error.
longjmp(png_jmpbuf(decoder->mPNG), 1);
}
if (color_type == PNG_COLOR_TYPE_PALETTE)
png_set_expand(png_ptr);
if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
png_set_expand(png_ptr);
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
int sample_max = (1 << bit_depth);
png_color_16p trans_values;
png_get_tRNS(png_ptr, info_ptr, &trans, &num_trans, &trans_values);
/* libpng doesn't reject a tRNS chunk with out-of-range samples
so we check it here to avoid setting up a useless opacity
channel or producing unexpected transparent pixels when using
libpng-1.2.19 through 1.2.26 (bug #428045) */
if ((color_type == PNG_COLOR_TYPE_GRAY &&
(int)trans_values->gray > sample_max) ||
(color_type == PNG_COLOR_TYPE_RGB &&
((int)trans_values->red > sample_max ||
(int)trans_values->green > sample_max ||
(int)trans_values->blue > sample_max)))
{
/* clear the tRNS valid flag and release tRNS memory */
png_free_data(png_ptr, info_ptr, PNG_FREE_TRNS, 0);
}
else
png_set_expand(png_ptr);
}
if (bit_depth == 16)
png_set_scale_16(png_ptr);
qcms_data_type inType;
PRUint32 intent = -1;
PRUint32 pIntent;
if (decoder->mCMSMode != eCMSMode_Off) {
intent = gfxPlatform::GetRenderingIntent();
decoder->mInProfile = PNGGetColorProfile(png_ptr, info_ptr,
color_type, &inType, &pIntent);
/* If we're not mandating an intent, use the one from the image. */
if (intent == PRUint32(-1))
intent = pIntent;
}
if (decoder->mInProfile && gfxPlatform::GetCMSOutputProfile()) {
qcms_data_type outType;
if (color_type & PNG_COLOR_MASK_ALPHA || num_trans)
outType = QCMS_DATA_RGBA_8;
else
outType = QCMS_DATA_RGB_8;
decoder->mTransform = qcms_transform_create(decoder->mInProfile,
inType,
gfxPlatform::GetCMSOutputProfile(),
outType,
(qcms_intent)intent);
} else {
png_set_gray_to_rgb(png_ptr);
// only do gamma correction if CMS isn't entirely disabled
if (decoder->mCMSMode != eCMSMode_Off)
PNGDoGammaCorrection(png_ptr, info_ptr);
if (decoder->mCMSMode == eCMSMode_All) {
if (color_type & PNG_COLOR_MASK_ALPHA || num_trans)
decoder->mTransform = gfxPlatform::GetCMSRGBATransform();
else
decoder->mTransform = gfxPlatform::GetCMSRGBTransform();
}
}
/* let libpng expand interlaced images */
if (interlace_type == PNG_INTERLACE_ADAM7) {
/* number_passes = */
png_set_interlace_handling(png_ptr);
}
/* now all of those things we set above are used to update various struct
* members and whatnot, after which we can get channels, rowbytes, etc. */
png_read_update_info(png_ptr, info_ptr);
decoder->mChannels = channels = png_get_channels(png_ptr, info_ptr);
/*---------------------------------------------------------------*/
/* copy PNG info into imagelib structs (formerly png_set_dims()) */
/*---------------------------------------------------------------*/
PRInt32 alpha_bits = 1;
if (channels == 2 || channels == 4) {
/* check if alpha is coming from a tRNS chunk and is binary */
if (num_trans) {
/* if it's not an indexed color image, tRNS means binary */
if (color_type == PNG_COLOR_TYPE_PALETTE) {
for (int i=0; i<num_trans; i++) {
if ((trans[i] != 0) && (trans[i] != 255)) {
alpha_bits = 8;
break;
}
}
}
} else {
alpha_bits = 8;
}
}
if (channels == 1 || channels == 3)
decoder->format = gfxASurface::ImageFormatRGB24;
else if (channels == 2 || channels == 4)
decoder->format = gfxASurface::ImageFormatARGB32;
#ifdef PNG_APNG_SUPPORTED
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_acTL))
png_set_progressive_frame_fn(png_ptr, nsPNGDecoder::frame_info_callback, NULL);
if (png_get_first_frame_is_hidden(png_ptr, info_ptr)) {
decoder->mFrameIsHidden = true;
} else {
#endif
decoder->CreateFrame(0, 0, width, height, decoder->format);
#ifdef PNG_APNG_SUPPORTED
}
#endif
if (decoder->mTransform &&
(channels <= 2 || interlace_type == PNG_INTERLACE_ADAM7)) {
PRUint32 bpp[] = { 0, 3, 4, 3, 4 };
decoder->mCMSLine =
(PRUint8 *)moz_malloc(bpp[channels] * width);
if (!decoder->mCMSLine) {
longjmp(png_jmpbuf(decoder->mPNG), 5); // NS_ERROR_OUT_OF_MEMORY
}
}
if (interlace_type == PNG_INTERLACE_ADAM7) {
if (height < PR_INT32_MAX / (width * channels))
decoder->interlacebuf = (PRUint8 *)moz_malloc(channels * width * height);
if (!decoder->interlacebuf) {
longjmp(png_jmpbuf(decoder->mPNG), 5); // NS_ERROR_OUT_OF_MEMORY
}
}
/* Reject any ancillary chunk after IDAT with a bad CRC (bug #397593).
* It would be better to show the default frame (if one has already been
* successfully decoded) before bailing, but it's simpler to just bail
* out with an error message.
*/
png_set_crc_action(png_ptr, PNG_CRC_NO_CHANGE, PNG_CRC_ERROR_QUIT);
return;
}
void
nsPNGDecoder::row_callback(png_structp png_ptr, png_bytep new_row,
png_uint_32 row_num, int pass)
{
/* libpng comments:
*
* this function is called for every row in the image. If the
* image is interlacing, and you turned on the interlace handler,
* this function will be called for every row in every pass.
* Some of these rows will not be changed from the previous pass.
* When the row is not changed, the new_row variable will be NULL.
* The rows and passes are called in order, so you don't really
* need the row_num and pass, but I'm supplying them because it
* may make your life easier.
*
* For the non-NULL rows of interlaced images, you must call
* png_progressive_combine_row() passing in the row and the
* old row. You can call this function for NULL rows (it will
* just return) and for non-interlaced images (it just does the
* memcpy for you) if it will make the code easier. Thus, you
* can just do this for all cases:
*
* png_progressive_combine_row(png_ptr, old_row, new_row);
*
* where old_row is what was displayed for previous rows. Note
* that the first pass (pass == 0 really) will completely cover
* the old row, so the rows do not have to be initialized. After
* the first pass (and only for interlaced images), you will have
* to pass the current row, and the function will combine the
* old row and the new row.
*/
nsPNGDecoder *decoder =
static_cast<nsPNGDecoder*>(png_get_progressive_ptr(png_ptr));
// skip this frame
if (decoder->mFrameIsHidden)
return;
if (row_num >= (png_uint_32) decoder->mFrameRect.height)
return;
if (new_row) {
PRInt32 width = decoder->mFrameRect.width;
PRUint32 iwidth = decoder->mFrameRect.width;
png_bytep line = new_row;
if (decoder->interlacebuf) {
line = decoder->interlacebuf + (row_num * decoder->mChannels * width);
png_progressive_combine_row(png_ptr, line, new_row);
}
PRUint32 bpr = width * sizeof(PRUint32);
PRUint32 *cptr32 = (PRUint32*)(decoder->mImageData + (row_num*bpr));
bool rowHasNoAlpha = true;
if (decoder->mTransform) {
if (decoder->mCMSLine) {
qcms_transform_data(decoder->mTransform, line, decoder->mCMSLine,
iwidth);
/* copy alpha over */
PRUint32 channels = decoder->mChannels;
if (channels == 2 || channels == 4) {
for (PRUint32 i = 0; i < iwidth; i++)
decoder->mCMSLine[4 * i + 3] = line[channels * i + channels - 1];
}
line = decoder->mCMSLine;
} else {
qcms_transform_data(decoder->mTransform, line, line, iwidth);
}
}
switch (decoder->format) {
case gfxASurface::ImageFormatRGB24:
{
// counter for while() loops below
PRUint32 idx = iwidth;
// copy as bytes until source pointer is 32-bit-aligned
for (; (NS_PTR_TO_UINT32(line) & 0x3) && idx; --idx) {
*cptr32++ = GFX_PACKED_PIXEL(0xFF, line[0], line[1], line[2]);
line += 3;
}
// copy pixels in blocks of 4
while (idx >= 4) {
GFX_BLOCK_RGB_TO_FRGB(line, cptr32);
idx -= 4;
line += 12;
cptr32 += 4;
}
// copy remaining pixel(s)
while (idx--) {
// 32-bit read of final pixel will exceed buffer, so read bytes
*cptr32++ = GFX_PACKED_PIXEL(0xFF, line[0], line[1], line[2]);
line += 3;
}
}
break;
case gfxASurface::ImageFormatARGB32:
{
if (!decoder->mDisablePremultipliedAlpha) {
for (PRUint32 x=width; x>0; --x) {
*cptr32++ = GFX_PACKED_PIXEL(line[3], line[0], line[1], line[2]);
if (line[3] != 0xff)
rowHasNoAlpha = false;
line += 4;
}
} else {
for (PRUint32 x=width; x>0; --x) {
*cptr32++ = GFX_PACKED_PIXEL_NO_PREMULTIPLY(line[3], line[0], line[1], line[2]);
if (line[3] != 0xff)
rowHasNoAlpha = false;
line += 4;
}
}
}
break;
default:
longjmp(png_jmpbuf(decoder->mPNG), 1);
}
if (!rowHasNoAlpha)
decoder->mFrameHasNoAlpha = false;
PRUint32 numFrames = decoder->mImage.GetNumFrames();
if (numFrames <= 1) {
// Only do incremental image display for the first frame
// XXXbholley - this check should be handled in the superclass
nsIntRect r(0, row_num, width, 1);
decoder->PostInvalidation(r);
}
}
}
// got the header of a new frame that's coming
void
nsPNGDecoder::frame_info_callback(png_structp png_ptr, png_uint_32 frame_num)
{
#ifdef PNG_APNG_SUPPORTED
png_uint_32 x_offset, y_offset;
PRInt32 width, height;
nsPNGDecoder *decoder =
static_cast<nsPNGDecoder*>(png_get_progressive_ptr(png_ptr));
// old frame is done
decoder->EndImageFrame();
// Only the first frame can be hidden, so unhide unconditionally here.
decoder->mFrameIsHidden = false;
x_offset = png_get_next_frame_x_offset(png_ptr, decoder->mInfo);
y_offset = png_get_next_frame_y_offset(png_ptr, decoder->mInfo);
width = png_get_next_frame_width(png_ptr, decoder->mInfo);
height = png_get_next_frame_height(png_ptr, decoder->mInfo);
decoder->CreateFrame(x_offset, y_offset, width, height, decoder->format);
#endif
}
void
nsPNGDecoder::end_callback(png_structp png_ptr, png_infop info_ptr)
{
/* libpng comments:
*
* this function is called when the whole image has been read,
* including any chunks after the image (up to and including
* the IEND). You will usually have the same info chunk as you
* had in the header, although some data may have been added
* to the comments and time fields.
*
* Most people won't do much here, perhaps setting a flag that
* marks the image as finished.
*/
nsPNGDecoder *decoder =
static_cast<nsPNGDecoder*>(png_get_progressive_ptr(png_ptr));
// We shouldn't get here if we've hit an error
NS_ABORT_IF_FALSE(!decoder->HasError(), "Finishing up PNG but hit error!");
#ifdef PNG_APNG_SUPPORTED
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_acTL)) {
PRInt32 num_plays = png_get_num_plays(png_ptr, info_ptr);
decoder->mImage.SetLoopCount(num_plays - 1);
}
#endif
// Send final notifications
decoder->EndImageFrame();
decoder->PostDecodeDone();
}
void
nsPNGDecoder::error_callback(png_structp png_ptr, png_const_charp error_msg)
{
PR_LOG(gPNGLog, PR_LOG_ERROR, ("libpng error: %s\n", error_msg));
longjmp(png_jmpbuf(png_ptr), 1);
}
void
nsPNGDecoder::warning_callback(png_structp png_ptr, png_const_charp warning_msg)
{
PR_LOG(gPNGLog, PR_LOG_WARNING, ("libpng warning: %s\n", warning_msg));
}
Telemetry::ID
nsPNGDecoder::SpeedHistogram()
{
return Telemetry::IMAGE_DECODE_SPEED_PNG;
}
} // namespace image
} // namespace mozilla