Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(226)

Side by Side Diff: src/codec/SkPngCodec.cpp

Issue 1726823002: Set SkColorSpace object for PNGs and parse ICC profiles (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Rebase Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/codec/SkPngCodec.h ('k') | src/core/SkColorSpace.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2015 Google Inc. 2 * Copyright 2015 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SkCodecPriv.h" 8 #include "SkCodecPriv.h"
9 #include "SkColorPriv.h" 9 #include "SkColorPriv.h"
10 #include "SkColorTable.h" 10 #include "SkColorTable.h"
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
158 } 158 }
159 159
160 /////////////////////////////////////////////////////////////////////////////// 160 ///////////////////////////////////////////////////////////////////////////////
161 // Creation 161 // Creation
162 /////////////////////////////////////////////////////////////////////////////// 162 ///////////////////////////////////////////////////////////////////////////////
163 163
164 bool SkPngCodec::IsPng(const char* buf, size_t bytesRead) { 164 bool SkPngCodec::IsPng(const char* buf, size_t bytesRead) {
165 return !png_sig_cmp((png_bytep) buf, (png_size_t)0, bytesRead); 165 return !png_sig_cmp((png_bytep) buf, (png_size_t)0, bytesRead);
166 } 166 }
167 167
168 static float png_fixed_point_to_float(png_fixed_point x) {
169 // We multiply by the same factor that libpng used to convert
170 // fixed point -> double. Since we want floats, we choose to
171 // do the conversion ourselves rather than convert
172 // fixed point -> double -> float.
173 return ((float) x) * 0.00001f;
174 }
175
176 // Returns a colorSpace object that represents any color space information in
177 // the encoded data. If the encoded data contains no color space, this will
178 // return NULL.
179 SkColorSpace* read_color_space(png_structp png_ptr, png_infop info_ptr) {
180
181 // First check for an ICC profile
182 png_bytep profile;
183 png_uint_32 length;
184 // The below variables are unused, however, we need to pass them in anyway o r
185 // png_get_iCCP() will return nothing.
186 // Could knowing the |name| of the profile ever be interesting? Maybe for d ebugging?
187 png_charp name;
188 // The |compression| is uninteresting since:
189 // (1) libpng has already decompressed the profile for us.
190 // (2) "deflate" is the only mode of decompression that libpng supports.
191 int compression;
192 if (PNG_INFO_iCCP == png_get_iCCP(png_ptr, info_ptr, &name, &compression, &p rofile,
193 &length)) {
194 return SkColorSpace::NewICC(profile, length);
195 }
196
197 // Second, check for sRGB.
198 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_sRGB)) {
199
200 // sRGB chunks also store a rendering intent: Absolute, Relative,
201 // Perceptual, and Saturation.
202 // FIXME (msarett): Extract this information from the sRGB chunk once
203 // we are able to handle this information in
204 // SkColorSpace.
205 return SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named);
206 }
207
208 // Next, check for chromaticities.
209 png_fixed_point XYZ[9];
210 SkFloat3x3 toXYZD50;
211 png_fixed_point gamma;
212 SkFloat3 gammas;
213 if (png_get_cHRM_XYZ_fixed(png_ptr, info_ptr, &XYZ[0], &XYZ[1], &XYZ[2], &XY Z[3], &XYZ[4],
214 &XYZ[5], &XYZ[6], &XYZ[7], &XYZ[8])) {
215
216 // FIXME (msarett): Here we are treating XYZ values as D50 even though t he color
217 // temperature is unspecified. I suspect that this ass umption
218 // is most often ok, but we could also calculate the co lor
219 // temperature (D value) and then convert the XYZ to D5 0. Maybe
220 // we should add a new constructor to SkColorSpace that accepts
221 // XYZ with D-Unkown?
222 for (int i = 0; i < 9; i++) {
223 toXYZD50.fMat[i] = png_fixed_point_to_float(XYZ[i]);
224 }
225
226 if (PNG_INFO_gAMA != png_get_gAMA_fixed(png_ptr, info_ptr, &gamma)) {
227
228 // If the image does not specify gamma, let's choose linear. Should we default
229 // to sRGB? Most images are intended to be sRGB (gamma = 2.2f).
230 gamma = PNG_GAMMA_LINEAR;
231 }
232 gammas.fVec[0] = gammas.fVec[1] = gammas.fVec[2] = png_fixed_point_to_fl oat(gamma);
233
234 return SkColorSpace::NewRGB(toXYZD50, gammas);
235 }
236
237 // Last, check for gamma.
238 if (PNG_INFO_gAMA == png_get_gAMA_fixed(png_ptr, info_ptr, &gamma)) {
239
240 // Guess a default value for cHRM? Or should we just give up?
241 // Here we use the identity matrix as a default.
242 // FIXME (msarett): Should SkFloat3x3 have a method to set the identity matrix?
243 memset(toXYZD50.fMat, 0, 9 * sizeof(float));
244 toXYZD50.fMat[0] = toXYZD50.fMat[4] = toXYZD50.fMat[8] = 1.0f;
245
246 // Set the gammas.
247 gammas.fVec[0] = gammas.fVec[1] = gammas.fVec[2] = png_fixed_point_to_fl oat(gamma);
248
249 return SkColorSpace::NewRGB(toXYZD50, gammas);
250 }
251
252 // Finally, what should we do if there is no color space information in the PNG?
253 // The specification says that this indicates "gamma is unknown" and that th e
254 // "color is device dependent". I'm assuming we can represent this with NUL L.
255 // But should we guess sRGB? Most images are sRGB, even if they don't speci fy.
256 return nullptr;
257 }
258
168 // Reads the header and initializes the output fields, if not NULL. 259 // Reads the header and initializes the output fields, if not NULL.
169 // 260 //
170 // @param stream Input data. Will be read to get enough information to properly 261 // @param stream Input data. Will be read to get enough information to properly
171 // setup the codec. 262 // setup the codec.
172 // @param chunkReader SkPngChunkReader, for reading unknown chunks. May be NULL. 263 // @param chunkReader SkPngChunkReader, for reading unknown chunks. May be NULL.
173 // If not NULL, png_ptr will hold an *unowned* pointer to it. The caller is 264 // If not NULL, png_ptr will hold an *unowned* pointer to it. The caller is
174 // expected to continue to own it for the lifetime of the png_ptr. 265 // expected to continue to own it for the lifetime of the png_ptr.
175 // @param png_ptrp Optional output variable. If non-NULL, will be set to a new 266 // @param png_ptrp Optional output variable. If non-NULL, will be set to a new
176 // png_structp on success. 267 // png_structp on success.
177 // @param info_ptrp Optional output variable. If non-NULL, will be set to a new 268 // @param info_ptrp Optional output variable. If non-NULL, will be set to a new
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
323 *png_ptrp = png_ptr; 414 *png_ptrp = png_ptr;
324 } 415 }
325 if (info_ptrp) { 416 if (info_ptrp) {
326 *info_ptrp = info_ptr; 417 *info_ptrp = info_ptr;
327 } 418 }
328 419
329 return true; 420 return true;
330 } 421 }
331 422
332 SkPngCodec::SkPngCodec(const SkImageInfo& info, SkStream* stream, SkPngChunkRead er* chunkReader, 423 SkPngCodec::SkPngCodec(const SkImageInfo& info, SkStream* stream, SkPngChunkRead er* chunkReader,
333 png_structp png_ptr, png_infop info_ptr, int bitDepth, in t numberPasses) 424 png_structp png_ptr, png_infop info_ptr, int bitDepth, in t numberPasses,
334 : INHERITED(info, stream) 425 SkColorSpace* colorSpace)
426 : INHERITED(info, stream, colorSpace)
335 , fPngChunkReader(SkSafeRef(chunkReader)) 427 , fPngChunkReader(SkSafeRef(chunkReader))
336 , fPng_ptr(png_ptr) 428 , fPng_ptr(png_ptr)
337 , fInfo_ptr(info_ptr) 429 , fInfo_ptr(info_ptr)
338 , fSrcConfig(SkSwizzler::kUnknown) 430 , fSrcConfig(SkSwizzler::kUnknown)
339 , fNumberPasses(numberPasses) 431 , fNumberPasses(numberPasses)
340 , fBitDepth(bitDepth) 432 , fBitDepth(bitDepth)
341 {} 433 {}
342 434
343 SkPngCodec::~SkPngCodec() { 435 SkPngCodec::~SkPngCodec() {
344 this->destroyReadStruct(); 436 this->destroyReadStruct();
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after
534 if (colorPtr) { 626 if (colorPtr) {
535 return get_color_table_fill_value(colorType, colorPtr, 0); 627 return get_color_table_fill_value(colorType, colorPtr, 0);
536 } 628 }
537 return INHERITED::onGetFillValue(colorType); 629 return INHERITED::onGetFillValue(colorType);
538 } 630 }
539 631
540 // Subclass of SkPngCodec which supports scanline decoding 632 // Subclass of SkPngCodec which supports scanline decoding
541 class SkPngScanlineDecoder : public SkPngCodec { 633 class SkPngScanlineDecoder : public SkPngCodec {
542 public: 634 public:
543 SkPngScanlineDecoder(const SkImageInfo& srcInfo, SkStream* stream, 635 SkPngScanlineDecoder(const SkImageInfo& srcInfo, SkStream* stream,
544 SkPngChunkReader* chunkReader, png_structp png_ptr, png_infop info_p tr, int bitDepth) 636 SkPngChunkReader* chunkReader, png_structp png_ptr, png_infop info_p tr, int bitDepth,
545 : INHERITED(srcInfo, stream, chunkReader, png_ptr, info_ptr, bitDepth, 1 ) 637 SkColorSpace* colorSpace)
638 : INHERITED(srcInfo, stream, chunkReader, png_ptr, info_ptr, bitDepth, 1 , colorSpace)
546 , fSrcRow(nullptr) 639 , fSrcRow(nullptr)
547 {} 640 {}
548 641
549 Result onStartScanlineDecode(const SkImageInfo& dstInfo, const Options& opti ons, 642 Result onStartScanlineDecode(const SkImageInfo& dstInfo, const Options& opti ons,
550 SkPMColor ctable[], int* ctableCount) override { 643 SkPMColor ctable[], int* ctableCount) override {
551 if (!conversion_possible(dstInfo, this->getInfo())) { 644 if (!conversion_possible(dstInfo, this->getInfo())) {
552 return kInvalidConversion; 645 return kInvalidConversion;
553 } 646 }
554 647
555 const Result result = this->initializeSwizzler(dstInfo, options, ctable, 648 const Result result = this->initializeSwizzler(dstInfo, options, ctable,
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
600 uint8_t* fSrcRow; 693 uint8_t* fSrcRow;
601 694
602 typedef SkPngCodec INHERITED; 695 typedef SkPngCodec INHERITED;
603 }; 696 };
604 697
605 698
606 class SkPngInterlacedScanlineDecoder : public SkPngCodec { 699 class SkPngInterlacedScanlineDecoder : public SkPngCodec {
607 public: 700 public:
608 SkPngInterlacedScanlineDecoder(const SkImageInfo& srcInfo, SkStream* stream, 701 SkPngInterlacedScanlineDecoder(const SkImageInfo& srcInfo, SkStream* stream,
609 SkPngChunkReader* chunkReader, png_structp png_ptr, png_infop info_p tr, 702 SkPngChunkReader* chunkReader, png_structp png_ptr, png_infop info_p tr,
610 int bitDepth, int numberPasses) 703 int bitDepth, int numberPasses, SkColorSpace* colorSpace)
611 : INHERITED(srcInfo, stream, chunkReader, png_ptr, info_ptr, bitDepth, n umberPasses) 704 : INHERITED(srcInfo, stream, chunkReader, png_ptr, info_ptr, bitDepth, n umberPasses,
705 colorSpace)
612 , fHeight(-1) 706 , fHeight(-1)
613 , fCanSkipRewind(false) 707 , fCanSkipRewind(false)
614 { 708 {
615 SkASSERT(numberPasses != 1); 709 SkASSERT(numberPasses != 1);
616 } 710 }
617 711
618 Result onStartScanlineDecode(const SkImageInfo& dstInfo, const Options& opti ons, 712 Result onStartScanlineDecode(const SkImageInfo& dstInfo, const Options& opti ons,
619 SkPMColor ctable[], int* ctableCount) override { 713 SkPMColor ctable[], int* ctableCount) override {
620 if (!conversion_possible(dstInfo, this->getInfo())) { 714 if (!conversion_possible(dstInfo, this->getInfo())) {
621 return kInvalidConversion; 715 return kInvalidConversion;
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
732 png_infop info_ptr; 826 png_infop info_ptr;
733 SkImageInfo imageInfo; 827 SkImageInfo imageInfo;
734 int bitDepth; 828 int bitDepth;
735 int numberPasses; 829 int numberPasses;
736 830
737 if (!read_header(stream, chunkReader, &png_ptr, &info_ptr, &imageInfo, &bitD epth, 831 if (!read_header(stream, chunkReader, &png_ptr, &info_ptr, &imageInfo, &bitD epth,
738 &numberPasses)) { 832 &numberPasses)) {
739 return nullptr; 833 return nullptr;
740 } 834 }
741 835
836 SkAutoTUnref<SkColorSpace> colorSpace(read_color_space(png_ptr, info_ptr));
837
742 if (1 == numberPasses) { 838 if (1 == numberPasses) {
743 return new SkPngScanlineDecoder(imageInfo, streamDeleter.detach(), chunk Reader, 839 return new SkPngScanlineDecoder(imageInfo, streamDeleter.detach(), chunk Reader,
744 png_ptr, info_ptr, bitDepth); 840 png_ptr, info_ptr, bitDepth, colorSpace) ;
745 } 841 }
746 842
747 return new SkPngInterlacedScanlineDecoder(imageInfo, streamDeleter.detach(), chunkReader, 843 return new SkPngInterlacedScanlineDecoder(imageInfo, streamDeleter.detach(), chunkReader,
748 png_ptr, info_ptr, bitDepth, numbe rPasses); 844 png_ptr, info_ptr, bitDepth, numbe rPasses,
845 colorSpace);
749 } 846 }
OLDNEW
« no previous file with comments | « src/codec/SkPngCodec.h ('k') | src/core/SkColorSpace.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698