tomhudson@google.com | d33b26e | 2012-03-02 16:12:14 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2007 The Android Open Source Project |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame^] | 8 | #include "SkImageEncoderPriv.h" |
tomhudson@google.com | d33b26e | 2012-03-02 16:12:14 +0000 | [diff] [blame] | 9 | |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame^] | 10 | #include "SkCanvas.h" |
tomhudson@google.com | d33b26e | 2012-03-02 16:12:14 +0000 | [diff] [blame] | 11 | #include "SkColorPriv.h" |
| 12 | #include "SkDither.h" |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame^] | 13 | #include "SkJPEGWriteUtility.h" |
| 14 | #include "SkRect.h" |
tomhudson@google.com | d33b26e | 2012-03-02 16:12:14 +0000 | [diff] [blame] | 15 | #include "SkStream.h" |
| 16 | #include "SkTemplates.h" |
djsollen@google.com | 1139940 | 2013-03-20 17:45:27 +0000 | [diff] [blame] | 17 | #include "SkTime.h" |
tomhudson@google.com | d33b26e | 2012-03-02 16:12:14 +0000 | [diff] [blame] | 18 | #include "SkUtils.h" |
halcanary@google.com | fed3037 | 2013-10-04 12:46:45 +0000 | [diff] [blame] | 19 | |
tomhudson@google.com | d33b26e | 2012-03-02 16:12:14 +0000 | [diff] [blame] | 20 | #include <stdio.h> |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame^] | 21 | |
tomhudson@google.com | d33b26e | 2012-03-02 16:12:14 +0000 | [diff] [blame] | 22 | extern "C" { |
| 23 | #include "jpeglib.h" |
| 24 | #include "jerror.h" |
| 25 | } |
| 26 | |
msarett | e8597a4 | 2016-03-24 10:41:47 -0700 | [diff] [blame] | 27 | // These enable timing code that report milliseconds for an encoding |
tomhudson@google.com | d33b26e | 2012-03-02 16:12:14 +0000 | [diff] [blame] | 28 | //#define TIME_ENCODE |
tomhudson@google.com | d33b26e | 2012-03-02 16:12:14 +0000 | [diff] [blame] | 29 | |
tomhudson@google.com | d33b26e | 2012-03-02 16:12:14 +0000 | [diff] [blame] | 30 | typedef void (*WriteScanline)(uint8_t* SK_RESTRICT dst, |
| 31 | const void* SK_RESTRICT src, int width, |
| 32 | const SkPMColor* SK_RESTRICT ctable); |
| 33 | |
msarett | c7d01d3 | 2016-04-18 14:21:55 -0700 | [diff] [blame] | 34 | static void Write_32_RGB(uint8_t* SK_RESTRICT dst, |
tomhudson@google.com | d33b26e | 2012-03-02 16:12:14 +0000 | [diff] [blame] | 35 | const void* SK_RESTRICT srcRow, int width, |
| 36 | const SkPMColor*) { |
| 37 | const uint32_t* SK_RESTRICT src = (const uint32_t*)srcRow; |
| 38 | while (--width >= 0) { |
tomhudson@google.com | d33b26e | 2012-03-02 16:12:14 +0000 | [diff] [blame] | 39 | uint32_t c = *src++; |
| 40 | dst[0] = SkGetPackedR32(c); |
| 41 | dst[1] = SkGetPackedG32(c); |
| 42 | dst[2] = SkGetPackedB32(c); |
tomhudson@google.com | d33b26e | 2012-03-02 16:12:14 +0000 | [diff] [blame] | 43 | dst += 3; |
| 44 | } |
| 45 | } |
| 46 | |
msarett | c7d01d3 | 2016-04-18 14:21:55 -0700 | [diff] [blame] | 47 | static void Write_4444_RGB(uint8_t* SK_RESTRICT dst, |
tomhudson@google.com | d33b26e | 2012-03-02 16:12:14 +0000 | [diff] [blame] | 48 | const void* SK_RESTRICT srcRow, int width, |
| 49 | const SkPMColor*) { |
| 50 | const SkPMColor16* SK_RESTRICT src = (const SkPMColor16*)srcRow; |
| 51 | while (--width >= 0) { |
tomhudson@google.com | d33b26e | 2012-03-02 16:12:14 +0000 | [diff] [blame] | 52 | SkPMColor16 c = *src++; |
| 53 | dst[0] = SkPacked4444ToR32(c); |
| 54 | dst[1] = SkPacked4444ToG32(c); |
| 55 | dst[2] = SkPacked4444ToB32(c); |
tomhudson@google.com | d33b26e | 2012-03-02 16:12:14 +0000 | [diff] [blame] | 56 | dst += 3; |
| 57 | } |
| 58 | } |
| 59 | |
msarett | c7d01d3 | 2016-04-18 14:21:55 -0700 | [diff] [blame] | 60 | static void Write_16_RGB(uint8_t* SK_RESTRICT dst, |
tomhudson@google.com | d33b26e | 2012-03-02 16:12:14 +0000 | [diff] [blame] | 61 | const void* SK_RESTRICT srcRow, int width, |
| 62 | const SkPMColor*) { |
| 63 | const uint16_t* SK_RESTRICT src = (const uint16_t*)srcRow; |
| 64 | while (--width >= 0) { |
tomhudson@google.com | d33b26e | 2012-03-02 16:12:14 +0000 | [diff] [blame] | 65 | uint16_t c = *src++; |
| 66 | dst[0] = SkPacked16ToR32(c); |
| 67 | dst[1] = SkPacked16ToG32(c); |
| 68 | dst[2] = SkPacked16ToB32(c); |
tomhudson@google.com | d33b26e | 2012-03-02 16:12:14 +0000 | [diff] [blame] | 69 | dst += 3; |
| 70 | } |
| 71 | } |
| 72 | |
msarett | c7d01d3 | 2016-04-18 14:21:55 -0700 | [diff] [blame] | 73 | static void Write_Index_RGB(uint8_t* SK_RESTRICT dst, |
tomhudson@google.com | d33b26e | 2012-03-02 16:12:14 +0000 | [diff] [blame] | 74 | const void* SK_RESTRICT srcRow, int width, |
| 75 | const SkPMColor* SK_RESTRICT ctable) { |
| 76 | const uint8_t* SK_RESTRICT src = (const uint8_t*)srcRow; |
| 77 | while (--width >= 0) { |
tomhudson@google.com | d33b26e | 2012-03-02 16:12:14 +0000 | [diff] [blame] | 78 | uint32_t c = ctable[*src++]; |
| 79 | dst[0] = SkGetPackedR32(c); |
| 80 | dst[1] = SkGetPackedG32(c); |
| 81 | dst[2] = SkGetPackedB32(c); |
tomhudson@google.com | d33b26e | 2012-03-02 16:12:14 +0000 | [diff] [blame] | 82 | dst += 3; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | static WriteScanline ChooseWriter(const SkBitmap& bm) { |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 87 | switch (bm.colorType()) { |
| 88 | case kN32_SkColorType: |
msarett | c7d01d3 | 2016-04-18 14:21:55 -0700 | [diff] [blame] | 89 | return Write_32_RGB; |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 90 | case kRGB_565_SkColorType: |
msarett | c7d01d3 | 2016-04-18 14:21:55 -0700 | [diff] [blame] | 91 | return Write_16_RGB; |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 92 | case kARGB_4444_SkColorType: |
msarett | c7d01d3 | 2016-04-18 14:21:55 -0700 | [diff] [blame] | 93 | return Write_4444_RGB; |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 94 | case kIndex_8_SkColorType: |
msarett | c7d01d3 | 2016-04-18 14:21:55 -0700 | [diff] [blame] | 95 | return Write_Index_RGB; |
tomhudson@google.com | d33b26e | 2012-03-02 16:12:14 +0000 | [diff] [blame] | 96 | default: |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 97 | return nullptr; |
tomhudson@google.com | d33b26e | 2012-03-02 16:12:14 +0000 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
| 101 | class SkJPEGImageEncoder : public SkImageEncoder { |
| 102 | protected: |
| 103 | virtual bool onEncode(SkWStream* stream, const SkBitmap& bm, int quality) { |
| 104 | #ifdef TIME_ENCODE |
djsollen@google.com | 1139940 | 2013-03-20 17:45:27 +0000 | [diff] [blame] | 105 | SkAutoTime atm("JPEG Encode"); |
tomhudson@google.com | d33b26e | 2012-03-02 16:12:14 +0000 | [diff] [blame] | 106 | #endif |
| 107 | |
tomhudson@google.com | d33b26e | 2012-03-02 16:12:14 +0000 | [diff] [blame] | 108 | SkAutoLockPixels alp(bm); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 109 | if (nullptr == bm.getPixels()) { |
tomhudson@google.com | d33b26e | 2012-03-02 16:12:14 +0000 | [diff] [blame] | 110 | return false; |
| 111 | } |
| 112 | |
| 113 | jpeg_compress_struct cinfo; |
| 114 | skjpeg_error_mgr sk_err; |
| 115 | skjpeg_destination_mgr sk_wstream(stream); |
| 116 | |
| 117 | // allocate these before set call setjmp |
scroggo | 565901d | 2015-12-10 10:44:13 -0800 | [diff] [blame] | 118 | SkAutoTMalloc<uint8_t> oneRow; |
tomhudson@google.com | d33b26e | 2012-03-02 16:12:14 +0000 | [diff] [blame] | 119 | |
| 120 | cinfo.err = jpeg_std_error(&sk_err); |
| 121 | sk_err.error_exit = skjpeg_error_exit; |
| 122 | if (setjmp(sk_err.fJmpBuf)) { |
| 123 | return false; |
| 124 | } |
tomhudson@google.com | d33b26e | 2012-03-02 16:12:14 +0000 | [diff] [blame] | 125 | |
mtklein@google.com | 8d725b2 | 2013-07-24 16:20:05 +0000 | [diff] [blame] | 126 | // Keep after setjmp or mark volatile. |
| 127 | const WriteScanline writer = ChooseWriter(bm); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 128 | if (nullptr == writer) { |
mtklein@google.com | 8d725b2 | 2013-07-24 16:20:05 +0000 | [diff] [blame] | 129 | return false; |
| 130 | } |
| 131 | |
| 132 | jpeg_create_compress(&cinfo); |
tomhudson@google.com | d33b26e | 2012-03-02 16:12:14 +0000 | [diff] [blame] | 133 | cinfo.dest = &sk_wstream; |
| 134 | cinfo.image_width = bm.width(); |
| 135 | cinfo.image_height = bm.height(); |
| 136 | cinfo.input_components = 3; |
msarett | c7d01d3 | 2016-04-18 14:21:55 -0700 | [diff] [blame] | 137 | |
| 138 | // FIXME: Can we take advantage of other in_color_spaces in libjpeg-turbo? |
tomhudson@google.com | d33b26e | 2012-03-02 16:12:14 +0000 | [diff] [blame] | 139 | cinfo.in_color_space = JCS_RGB; |
msarett | c7d01d3 | 2016-04-18 14:21:55 -0700 | [diff] [blame] | 140 | |
| 141 | // The gamma value is ignored by libjpeg-turbo. |
tomhudson@google.com | d33b26e | 2012-03-02 16:12:14 +0000 | [diff] [blame] | 142 | cinfo.input_gamma = 1; |
| 143 | |
| 144 | jpeg_set_defaults(&cinfo); |
msarett | c7d01d3 | 2016-04-18 14:21:55 -0700 | [diff] [blame] | 145 | |
| 146 | // Tells libjpeg-turbo to compute optimal Huffman coding tables |
| 147 | // for the image. This improves compression at the cost of |
| 148 | // slower encode performance. |
benjaminwagner | 0a35620 | 2016-01-14 18:13:32 -0800 | [diff] [blame] | 149 | cinfo.optimize_coding = TRUE; |
tomhudson@google.com | d33b26e | 2012-03-02 16:12:14 +0000 | [diff] [blame] | 150 | jpeg_set_quality(&cinfo, quality, TRUE /* limit to baseline-JPEG values */); |
tomhudson@google.com | d33b26e | 2012-03-02 16:12:14 +0000 | [diff] [blame] | 151 | |
| 152 | jpeg_start_compress(&cinfo, TRUE); |
| 153 | |
| 154 | const int width = bm.width(); |
scroggo | 565901d | 2015-12-10 10:44:13 -0800 | [diff] [blame] | 155 | uint8_t* oneRowP = oneRow.reset(width * 3); |
tomhudson@google.com | d33b26e | 2012-03-02 16:12:14 +0000 | [diff] [blame] | 156 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 157 | const SkPMColor* colors = bm.getColorTable() ? bm.getColorTable()->readColors() : nullptr; |
tomhudson@google.com | d33b26e | 2012-03-02 16:12:14 +0000 | [diff] [blame] | 158 | const void* srcRow = bm.getPixels(); |
| 159 | |
| 160 | while (cinfo.next_scanline < cinfo.image_height) { |
| 161 | JSAMPROW row_pointer[1]; /* pointer to JSAMPLE row[s] */ |
| 162 | |
| 163 | writer(oneRowP, srcRow, width, colors); |
| 164 | row_pointer[0] = oneRowP; |
| 165 | (void) jpeg_write_scanlines(&cinfo, row_pointer, 1); |
| 166 | srcRow = (const void*)((const char*)srcRow + bm.rowBytes()); |
| 167 | } |
| 168 | |
| 169 | jpeg_finish_compress(&cinfo); |
| 170 | jpeg_destroy_compress(&cinfo); |
| 171 | |
| 172 | return true; |
| 173 | } |
| 174 | }; |
| 175 | |
| 176 | /////////////////////////////////////////////////////////////////////////////// |
robertphillips@google.com | ec51cb8 | 2012-03-23 18:13:47 +0000 | [diff] [blame] | 177 | DEFINE_ENCODER_CREATOR(JPEGImageEncoder); |
| 178 | /////////////////////////////////////////////////////////////////////////////// |
tomhudson@google.com | d33b26e | 2012-03-02 16:12:14 +0000 | [diff] [blame] | 179 | |
robertphillips@google.com | 8570b5c | 2012-03-20 17:40:58 +0000 | [diff] [blame] | 180 | static SkImageEncoder* sk_libjpeg_efactory(SkImageEncoder::Type t) { |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame^] | 181 | return (SkEncodedImageFormat::kJPEG == (SkEncodedImageFormat)t) ? new SkJPEGImageEncoder : nullptr; |
tomhudson@google.com | d33b26e | 2012-03-02 16:12:14 +0000 | [diff] [blame] | 182 | } |
| 183 | |
mtklein@google.com | bd6343b | 2013-09-04 17:20:18 +0000 | [diff] [blame] | 184 | static SkImageEncoder_EncodeReg gEReg(sk_libjpeg_efactory); |