Chromium Code Reviews| 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 13 matching lines...) Expand all Loading... | |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ | 29 */ |
| 30 | 30 |
| 31 #include "config.h" | 31 #include "config.h" |
| 32 #include "platform/image-encoders/skia/JPEGImageEncoder.h" | 32 #include "platform/image-encoders/skia/JPEGImageEncoder.h" |
| 33 | 33 |
| 34 #include "SkBitmap.h" | 34 // #include "SkBitmap.h" |
|
Noel Gordon
2015/08/27 14:19:22
Will remove this after the trys all pass.
| |
| 35 #include "SkColorPriv.h" | 35 #include "SkColorPriv.h" |
| 36 #include "platform/geometry/IntSize.h" | 36 #include "platform/geometry/IntSize.h" |
| 37 #include "platform/graphics/ImageBuffer.h" | 37 #include "platform/graphics/ImageBuffer.h" |
| 38 | |
| 38 extern "C" { | 39 extern "C" { |
| 39 #include <setjmp.h> | 40 #include <setjmp.h> |
| 40 #include <stdio.h> // jpeglib.h needs stdio.h FILE | 41 #include <stdio.h> // jpeglib.h needs stdio.h FILE |
| 41 #include "jpeglib.h" | 42 #include "jpeglib.h" |
| 42 } | 43 } |
| 43 | 44 |
| 44 namespace blink { | 45 namespace blink { |
| 45 | 46 |
| 46 struct JPEGOutputBuffer : public jpeg_destination_mgr { | 47 struct JPEGOutputBuffer : public jpeg_destination_mgr { |
| 47 Vector<unsigned char>* output; | 48 Vector<unsigned char>* output; |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 72 const size_t size = out->buffer.size() - out->free_in_buffer; | 73 const size_t size = out->buffer.size() - out->free_in_buffer; |
| 73 out->output->append(out->buffer.data(), size); | 74 out->output->append(out->buffer.data(), size); |
| 74 } | 75 } |
| 75 | 76 |
| 76 static void handleError(j_common_ptr common) | 77 static void handleError(j_common_ptr common) |
| 77 { | 78 { |
| 78 jmp_buf* jumpBufferPtr = static_cast<jmp_buf*>(common->client_data); | 79 jmp_buf* jumpBufferPtr = static_cast<jmp_buf*>(common->client_data); |
| 79 longjmp(*jumpBufferPtr, -1); | 80 longjmp(*jumpBufferPtr, -1); |
| 80 } | 81 } |
| 81 | 82 |
| 82 static void preMultipliedBGRAtoRGB(const unsigned char* pixels, unsigned pixelCo unt, unsigned char* output) | |
| 83 { | |
| 84 const SkPMColor* input = reinterpret_cast_ptr<const SkPMColor*>(pixels); | |
| 85 for (; pixelCount-- > 0; ++input) { | |
| 86 *output++ = SkGetPackedR32(*input); | |
| 87 *output++ = SkGetPackedG32(*input); | |
| 88 *output++ = SkGetPackedB32(*input); | |
| 89 } | |
| 90 } | |
| 91 | |
| 92 static void RGBAtoRGB(const unsigned char* pixels, unsigned pixelCount, unsigned char* output) | 83 static void RGBAtoRGB(const unsigned char* pixels, unsigned pixelCount, unsigned char* output) |
| 93 { | 84 { |
| 85 // Per <canvas> spec, composite the input image pixels source-over on black. | |
| 86 | |
| 94 for (; pixelCount-- > 0; pixels += 4) { | 87 for (; pixelCount-- > 0; pixels += 4) { |
| 95 // Do source-over composition on black. | |
| 96 unsigned char alpha = pixels[3]; | 88 unsigned char alpha = pixels[3]; |
| 97 if (alpha != 255) { | 89 if (alpha != 255) { |
| 98 *output++ = SkMulDiv255Round(pixels[0], alpha); | 90 *output++ = SkMulDiv255Round(pixels[0], alpha); |
| 99 *output++ = SkMulDiv255Round(pixels[1], alpha); | 91 *output++ = SkMulDiv255Round(pixels[1], alpha); |
| 100 *output++ = SkMulDiv255Round(pixels[2], alpha); | 92 *output++ = SkMulDiv255Round(pixels[2], alpha); |
| 101 } else { | 93 } else { |
| 102 *output++ = pixels[0]; | 94 *output++ = pixels[0]; |
| 103 *output++ = pixels[1]; | 95 *output++ = pixels[1]; |
| 104 *output++ = pixels[2]; | 96 *output++ = pixels[2]; |
| 105 } | 97 } |
| 106 } | 98 } |
| 107 } | 99 } |
| 108 | 100 |
| 109 static void disableSubsamplingForHighQuality(jpeg_compress_struct* cinfo, int qu ality) | 101 static void disableSubsamplingForHighQuality(jpeg_compress_struct* cinfo, int qu ality) |
| 110 { | 102 { |
| 111 if (quality < 100) | 103 if (quality < 100) |
| 112 return; | 104 return; |
| 113 | 105 |
| 114 for (int i = 0; i < MAX_COMPONENTS; ++i) { | 106 for (int i = 0; i < MAX_COMPONENTS; ++i) { |
| 115 cinfo->comp_info[i].h_samp_factor = 1; | 107 cinfo->comp_info[i].h_samp_factor = 1; |
| 116 cinfo->comp_info[i].v_samp_factor = 1; | 108 cinfo->comp_info[i].v_samp_factor = 1; |
| 117 } | 109 } |
| 118 } | 110 } |
| 119 | 111 |
| 120 static bool encodePixels(IntSize imageSize, const unsigned char* inputPixels, bo ol premultiplied, int quality, Vector<unsigned char>* output) | 112 static bool encodePixels(IntSize imageSize, const unsigned char* inputPixels, in t quality, Vector<unsigned char>* output) |
| 121 { | 113 { |
| 122 if (imageSize.width() <= 0 || imageSize.height() <= 0) | 114 if (imageSize.width() <= 0 || imageSize.height() <= 0) |
| 123 return false; | 115 return false; |
| 124 | 116 |
| 125 JPEGOutputBuffer destination; | 117 JPEGOutputBuffer destination; |
| 126 destination.output = output; | 118 destination.output = output; |
| 127 Vector<JSAMPLE> row; | 119 Vector<JSAMPLE> row; |
| 128 | 120 |
| 129 jpeg_compress_struct cinfo; | 121 jpeg_compress_struct cinfo; |
| 130 jpeg_error_mgr error; | 122 jpeg_error_mgr error; |
| 131 cinfo.err = jpeg_std_error(&error); | 123 cinfo.err = jpeg_std_error(&error); |
| 132 error.error_exit = handleError; | 124 error.error_exit = handleError; |
| 133 jmp_buf jumpBuffer; | 125 jmp_buf jumpBuffer; |
| 134 cinfo.client_data = &jumpBuffer; | 126 cinfo.client_data = &jumpBuffer; |
| 135 | 127 |
| 136 if (setjmp(jumpBuffer)) { | 128 if (setjmp(jumpBuffer)) { |
| 137 jpeg_destroy_compress(&cinfo); | 129 jpeg_destroy_compress(&cinfo); |
| 138 return false; | 130 return false; |
| 139 } | 131 } |
| 140 | 132 |
| 141 jpeg_create_compress(&cinfo); | 133 jpeg_create_compress(&cinfo); |
| 142 cinfo.dest = &destination; | 134 cinfo.dest = &destination; |
| 143 cinfo.dest->init_destination = prepareOutput; | 135 cinfo.dest->init_destination = prepareOutput; |
| 144 cinfo.dest->empty_output_buffer = writeOutput; | 136 cinfo.dest->empty_output_buffer = writeOutput; |
| 145 cinfo.dest->term_destination = finishOutput; | 137 cinfo.dest->term_destination = finishOutput; |
| 146 | 138 |
| 147 cinfo.image_height = imageSize.height(); | 139 cinfo.image_height = imageSize.height(); |
| 148 cinfo.image_width = imageSize.width(); | 140 cinfo.image_width = imageSize.width(); |
| 149 | |
| 150 #if defined(JCS_EXTENSIONS) | |
| 151 if (premultiplied) { | |
| 152 cinfo.in_color_space = SK_B32_SHIFT ? JCS_EXT_RGBX : JCS_EXT_BGRX; | |
| 153 | |
| 154 cinfo.input_components = 4; | |
| 155 | |
| 156 jpeg_set_defaults(&cinfo); | |
| 157 jpeg_set_quality(&cinfo, quality, TRUE); | |
| 158 disableSubsamplingForHighQuality(&cinfo, quality); | |
| 159 jpeg_start_compress(&cinfo, TRUE); | |
| 160 | |
| 161 unsigned char* pixels = const_cast<unsigned char*>(inputPixels); | |
| 162 const size_t pixelRowStride = cinfo.image_width * 4; | |
| 163 while (cinfo.next_scanline < cinfo.image_height) { | |
| 164 jpeg_write_scanlines(&cinfo, &pixels, 1); | |
| 165 pixels += pixelRowStride; | |
| 166 } | |
| 167 | |
| 168 jpeg_finish_compress(&cinfo); | |
| 169 jpeg_destroy_compress(&cinfo); | |
| 170 return true; | |
| 171 } | |
| 172 #endif | |
| 173 | |
| 174 cinfo.in_color_space = JCS_RGB; | 141 cinfo.in_color_space = JCS_RGB; |
| 175 cinfo.input_components = 3; | 142 cinfo.input_components = 3; |
| 176 | 143 |
| 177 void (*extractRowRGB)(const unsigned char*, unsigned, unsigned char* output) ; | |
| 178 extractRowRGB = &RGBAtoRGB; | |
| 179 if (premultiplied) | |
| 180 extractRowRGB = &preMultipliedBGRAtoRGB; | |
| 181 | |
| 182 jpeg_set_defaults(&cinfo); | 144 jpeg_set_defaults(&cinfo); |
| 183 jpeg_set_quality(&cinfo, quality, TRUE); | 145 jpeg_set_quality(&cinfo, quality, TRUE); |
| 184 disableSubsamplingForHighQuality(&cinfo, quality); | 146 disableSubsamplingForHighQuality(&cinfo, quality); |
| 185 jpeg_start_compress(&cinfo, TRUE); | 147 jpeg_start_compress(&cinfo, TRUE); |
| 186 | 148 |
| 187 unsigned char* pixels = const_cast<unsigned char*>(inputPixels); | 149 unsigned char* pixels = const_cast<unsigned char*>(inputPixels); |
| 188 row.resize(cinfo.image_width * cinfo.input_components); | 150 row.resize(cinfo.image_width * cinfo.input_components); |
| 189 const size_t pixelRowStride = cinfo.image_width * 4; | 151 const size_t pixelRowStride = cinfo.image_width * 4; |
| 190 while (cinfo.next_scanline < cinfo.image_height) { | 152 while (cinfo.next_scanline < cinfo.image_height) { |
| 191 JSAMPLE* rowData = row.data(); | 153 JSAMPLE* rowData = row.data(); |
| 192 extractRowRGB(pixels, cinfo.image_width, rowData); | 154 RGBAtoRGB(pixels, cinfo.image_width, rowData); |
| 193 jpeg_write_scanlines(&cinfo, &rowData, 1); | 155 jpeg_write_scanlines(&cinfo, &rowData, 1); |
| 194 pixels += pixelRowStride; | 156 pixels += pixelRowStride; |
| 195 } | 157 } |
| 196 | 158 |
| 197 jpeg_finish_compress(&cinfo); | 159 jpeg_finish_compress(&cinfo); |
| 198 jpeg_destroy_compress(&cinfo); | 160 jpeg_destroy_compress(&cinfo); |
| 199 return true; | 161 return true; |
| 200 } | 162 } |
| 201 | 163 |
| 202 bool JPEGImageEncoder::encode(const SkBitmap& bitmap, int quality, Vector<unsign ed char>* output) | |
| 203 { | |
| 204 SkAutoLockPixels bitmapLock(bitmap); | |
| 205 | |
| 206 if (bitmap.colorType() != kN32_SkColorType || !bitmap.getPixels()) | |
| 207 return false; // Only support 32 bit/pixel skia bitmaps. | |
| 208 | |
| 209 return encodePixels(IntSize(bitmap.width(), bitmap.height()), static_cast<un signed char *>(bitmap.getPixels()), true, quality, output); | |
| 210 } | |
| 211 | |
| 212 bool JPEGImageEncoder::encode(const ImageDataBuffer& imageData, int quality, Vec tor<unsigned char>* output) | 164 bool JPEGImageEncoder::encode(const ImageDataBuffer& imageData, int quality, Vec tor<unsigned char>* output) |
| 213 { | 165 { |
| 214 if (!imageData.pixels()) | 166 if (!imageData.pixels()) |
| 215 return false; | 167 return false; |
| 216 | 168 |
| 217 return encodePixels(IntSize(imageData.width(), imageData.height()), imageDat a.pixels(), false, quality, output); | 169 return encodePixels(IntSize(imageData.width(), imageData.height()), imageDat a.pixels(), quality, output); |
| 218 } | 170 } |
| 219 | 171 |
| 220 } // namespace blink | 172 } // namespace blink |
| OLD | NEW |