OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2010, Google Inc. All rights reserved. | 2 * Copyright (c) 2010, Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 *output++ = alpha; | 61 *output++ = alpha; |
62 } else { | 62 } else { |
63 *output++ = SkGetPackedR32(*input); | 63 *output++ = SkGetPackedR32(*input); |
64 *output++ = SkGetPackedG32(*input); | 64 *output++ = SkGetPackedG32(*input); |
65 *output++ = SkGetPackedB32(*input); | 65 *output++ = SkGetPackedB32(*input); |
66 *output++ = alpha; | 66 *output++ = alpha; |
67 } | 67 } |
68 } | 68 } |
69 } | 69 } |
70 | 70 |
71 static bool encodePixels(IntSize imageSize, unsigned char* inputPixels, bool pre
multiplied, Vector<unsigned char>* output) | 71 static bool encodePixels(IntSize imageSize, const unsigned char* inputPixels, bo
ol premultiplied, Vector<unsigned char>* output) |
72 { | 72 { |
73 imageSize.clampNegativeToZero(); | 73 imageSize.clampNegativeToZero(); |
74 Vector<unsigned char> row; | 74 Vector<unsigned char> row; |
75 | 75 |
76 png_struct* png = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0); | 76 png_struct* png = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0); |
77 png_info* info = png_create_info_struct(png); | 77 png_info* info = png_create_info_struct(png); |
78 if (!png || !info || setjmp(png_jmpbuf(png))) { | 78 if (!png || !info || setjmp(png_jmpbuf(png))) { |
79 png_destroy_write_struct(png ? &png : 0, info ? &info : 0); | 79 png_destroy_write_struct(png ? &png : 0, info ? &info : 0); |
80 return false; | 80 return false; |
81 } | 81 } |
82 | 82 |
83 // Optimize compression for speed. | 83 // Optimize compression for speed. |
84 // The parameters are the same as what libpng uses by default for RGB and RG
BA images, except: | 84 // The parameters are the same as what libpng uses by default for RGB and RG
BA images, except: |
85 // - the zlib compression level is 3 instead of 6, to avoid the lazy Ziv-Lem
pel match searching; | 85 // - the zlib compression level is 3 instead of 6, to avoid the lazy Ziv-Lem
pel match searching; |
86 // - the delta filter is 1 ("sub") instead of 5 ("all"), to reduce the filte
r computations. | 86 // - the delta filter is 1 ("sub") instead of 5 ("all"), to reduce the filte
r computations. |
87 // The zlib memory level (8) and strategy (Z_FILTERED) will be set inside li
bpng. | 87 // The zlib memory level (8) and strategy (Z_FILTERED) will be set inside li
bpng. |
88 // | 88 // |
89 // Avoid the zlib strategies Z_HUFFMAN_ONLY or Z_RLE. | 89 // Avoid the zlib strategies Z_HUFFMAN_ONLY or Z_RLE. |
90 // Although they are the fastest for poorly-compressible images (e.g. photog
raphs), | 90 // Although they are the fastest for poorly-compressible images (e.g. photog
raphs), |
91 // they are very slow for highly-compressible images (e.g. text, drawings or
business graphics). | 91 // they are very slow for highly-compressible images (e.g. text, drawings or
business graphics). |
92 png_set_compression_level(png, 3); | 92 png_set_compression_level(png, 3); |
93 png_set_filter(png, PNG_FILTER_TYPE_BASE, PNG_FILTER_SUB); | 93 png_set_filter(png, PNG_FILTER_TYPE_BASE, PNG_FILTER_SUB); |
94 | 94 |
95 png_set_write_fn(png, output, writeOutput, 0); | 95 png_set_write_fn(png, output, writeOutput, 0); |
96 png_set_IHDR(png, info, imageSize.width(), imageSize.height(), | 96 png_set_IHDR(png, info, imageSize.width(), imageSize.height(), |
97 8, PNG_COLOR_TYPE_RGB_ALPHA, 0, 0, 0); | 97 8, PNG_COLOR_TYPE_RGB_ALPHA, 0, 0, 0); |
98 png_write_info(png, info); | 98 png_write_info(png, info); |
99 | 99 |
100 unsigned char* pixels = inputPixels; | 100 unsigned char* pixels = const_cast<unsigned char*>(inputPixels); |
101 row.resize(imageSize.width() * sizeof(SkPMColor)); | 101 row.resize(imageSize.width() * sizeof(SkPMColor)); |
102 const size_t pixelRowStride = imageSize.width() * 4; | 102 const size_t pixelRowStride = imageSize.width() * 4; |
103 for (int y = 0; y < imageSize.height(); ++y) { | 103 for (int y = 0; y < imageSize.height(); ++y) { |
104 if (premultiplied) { | 104 if (premultiplied) { |
105 preMultipliedBGRAtoRGBA(pixels, imageSize.width(), row.data()); | 105 preMultipliedBGRAtoRGBA(pixels, imageSize.width(), row.data()); |
106 png_write_row(png, row.data()); | 106 png_write_row(png, row.data()); |
107 } else | 107 } else |
108 png_write_row(png, pixels); | 108 png_write_row(png, pixels); |
109 pixels += pixelRowStride; | 109 pixels += pixelRowStride; |
110 } | 110 } |
111 | 111 |
112 png_write_end(png, info); | 112 png_write_end(png, info); |
113 png_destroy_write_struct(&png, &info); | 113 png_destroy_write_struct(&png, &info); |
114 return true; | 114 return true; |
115 } | 115 } |
116 | 116 |
117 bool PNGImageEncoder::encode(const SkBitmap& bitmap, Vector<unsigned char>* outp
ut) | 117 bool PNGImageEncoder::encode(const SkBitmap& bitmap, Vector<unsigned char>* outp
ut) |
118 { | 118 { |
119 SkAutoLockPixels bitmapLock(bitmap); | 119 SkAutoLockPixels bitmapLock(bitmap); |
120 | 120 |
121 if (bitmap.colorType() != kN32_SkColorType || !bitmap.getPixels()) | 121 if (bitmap.colorType() != kN32_SkColorType || !bitmap.getPixels()) |
122 return false; // Only support 32 bit/pixel skia bitmaps. | 122 return false; // Only support 32 bit/pixel skia bitmaps. |
123 | 123 |
124 return encodePixels(IntSize(bitmap.width(), bitmap.height()), static_cast<un
signed char*>(bitmap.getPixels()), true, output); | 124 return encodePixels(IntSize(bitmap.width(), bitmap.height()), static_cast<un
signed char*>(bitmap.getPixels()), true, output); |
125 } | 125 } |
126 | 126 |
127 bool PNGImageEncoder::encode(const ImageDataBuffer& imageData, Vector<unsigned c
har>* output) | 127 bool PNGImageEncoder::encode(const ImageDataBuffer& imageData, Vector<unsigned c
har>* output) |
128 { | 128 { |
129 return encodePixels(imageData.size(), imageData.data(), false, output); | 129 return encodePixels(IntSize(imageData.width(), imageData.height()), imageDat
a.pixels(), false, output); |
130 } | 130 } |
131 | 131 |
132 } // namespace blink | 132 } // namespace blink |
OLD | NEW |