1 /* 2 * Copyright (c) 2010, Google Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following disclaimer 12 * in the documentation and/or other materials provided with the 13 * distribution. 14 * * Neither the name of Google Inc. nor the names of its 15 * contributors may be used to endorse or promote products derived from 16 * this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 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. 29 */ 30 31 #include "config.h" 32 #include "JPEGImageEncoder.h" 33 34 #include "ImageData.h" 35 #include "IntSize.h" 36 #include "SkBitmap.h" 37 #include "SkColorPriv.h" 38 #include "SkUnPreMultiply.h" 39 extern "C" { 40 #include <stdio.h> // jpeglib.h needs stdio.h FILE 41 #include "jpeglib.h" 42 #include <setjmp.h> 43 } 44 45 namespace WebCore { 46 47 struct JPEGOutputBuffer : public jpeg_destination_mgr { 48 Vector<unsigned char>* output; 49 Vector<unsigned char> buffer; 50 }; 51 52 static void prepareOutput(j_compress_ptr cinfo) 53 { 54 JPEGOutputBuffer* out = static_cast<JPEGOutputBuffer*>(cinfo->dest); 55 const size_t internalBufferSize = 8192; 56 out->buffer.resize(internalBufferSize); 57 out->next_output_byte = out->buffer.data(); 58 out->free_in_buffer = out->buffer.size(); 59 } 60 61 static boolean writeOutput(j_compress_ptr cinfo) 62 { 63 JPEGOutputBuffer* out = static_cast<JPEGOutputBuffer*>(cinfo->dest); 64 out->output->append(out->buffer.data(), out->buffer.size()); 65 out->next_output_byte = out->buffer.data(); 66 out->free_in_buffer = out->buffer.size(); 67 return TRUE; 68 } 69 70 static void finishOutput(j_compress_ptr cinfo) 71 { 72 JPEGOutputBuffer* out = static_cast<JPEGOutputBuffer*>(cinfo->dest); 73 const size_t size = out->buffer.size() - out->free_in_buffer; 74 out->output->append(out->buffer.data(), size); 75 } 76 77 static void handleError(j_common_ptr common) 78 { 79 jmp_buf* jumpBufferPtr = static_cast<jmp_buf*>(common->client_data); 80 longjmp(*jumpBufferPtr, -1); 81 } 82 83 static void preMultipliedBGRAtoRGB(const void* pixels, unsigned int pixelCount, unsigned char* output) 84 { 85 const SkPMColor* input = static_cast<const SkPMColor*>(pixels); 86 for (; pixelCount-- > 0; ++input) { 87 *output++ = SkGetPackedR32(*input); 88 *output++ = SkGetPackedG32(*input); 89 *output++ = SkGetPackedB32(*input); 90 } 91 } 92 93 static void RGBAtoRGB(const unsigned char* input, unsigned int pixels, unsigned char* output) 94 { 95 for (; pixels-- > 0; input += 4) { 96 *output++ = input[0]; 97 *output++ = input[1]; 98 *output++ = input[2]; 99 } 100 } 101 102 static bool encodePixels(const IntSize& inputSize, unsigned char* inputPixels, 103 bool premultiplied, int quality, Vector<unsigned char>* output) 104 { 105 IntSize imageSize(inputSize); 106 imageSize.clampNegativeToZero(); 107 JPEGOutputBuffer destination; 108 destination.output = output; 109 Vector<JSAMPLE> row; 110 111 jpeg_compress_struct cinfo; 112 jpeg_error_mgr error; 113 cinfo.err = jpeg_std_error(&error); 114 error.error_exit = handleError; 115 jmp_buf jumpBuffer; 116 cinfo.client_data = &jumpBuffer; 117 118 if (setjmp(jumpBuffer)) { 119 jpeg_destroy_compress(&cinfo); 120 return false; 121 } 122 123 jpeg_create_compress(&cinfo); 124 cinfo.dest = &destination; 125 cinfo.dest->init_destination = prepareOutput; 126 cinfo.dest->empty_output_buffer = writeOutput; 127 cinfo.dest->term_destination = finishOutput; 128 cinfo.image_height = imageSize.height(); 129 cinfo.image_width = imageSize.width(); 130 cinfo.in_color_space = JCS_RGB; 131 cinfo.input_components = 3; 132 133 jpeg_set_defaults(&cinfo); 134 jpeg_set_quality(&cinfo, quality, TRUE); 135 jpeg_start_compress(&cinfo, TRUE); 136 137 unsigned char* pixels = inputPixels; 138 row.resize(cinfo.image_width * cinfo.input_components); 139 while (cinfo.next_scanline < cinfo.image_height) { 140 if (premultiplied) 141 preMultipliedBGRAtoRGB(pixels, cinfo.image_width, row.data()); 142 else 143 RGBAtoRGB(pixels, cinfo.image_width, row.data()); 144 jpeg_write_scanlines(&cinfo, row.dataSlot(), 1); 145 pixels += cinfo.image_width * 4; 146 } 147 148 jpeg_finish_compress(&cinfo); 149 jpeg_destroy_compress(&cinfo); 150 return true; 151 } 152 153 bool JPEGImageEncoder::encode(const SkBitmap& bitmap, int quality, Vector<unsigned char>* output) 154 { 155 if (bitmap.config() != SkBitmap::kARGB_8888_Config) 156 return false; // Only support ARGB 32 bpp skia bitmaps. 157 158 SkAutoLockPixels bitmapLock(bitmap); 159 IntSize imageSize(bitmap.width(), bitmap.height()); 160 161 return encodePixels(imageSize, static_cast<unsigned char *>(bitmap.getPixels()), 162 true, quality, output); 163 } 164 165 bool JPEGImageEncoder::encode(const ImageData& imageData, int quality, Vector<unsigned char>* output) 166 { 167 return encodePixels(imageData.size(), imageData.data()->data()->data(), 168 false, quality, output); 169 } 170 171 } // namespace WebCore 172