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

Side by Side Diff: Source/platform/image-encoders/skia/JPEGImageEncoder.cpp

Issue 1314763008: Remove JPEG encoder SkBitmap API (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Patch for landing. Created 5 years, 3 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 | Annotate | Revision Log
« no previous file with comments | « Source/platform/image-encoders/skia/JPEGImageEncoder.h ('k') | no next file » | 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 (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
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"
35 #include "SkColorPriv.h" 34 #include "SkColorPriv.h"
36 #include "platform/geometry/IntSize.h" 35 #include "platform/geometry/IntSize.h"
37 #include "platform/graphics/ImageBuffer.h" 36 #include "platform/graphics/ImageBuffer.h"
37
38 extern "C" { 38 extern "C" {
39 #include <setjmp.h> 39 #include <setjmp.h>
40 #include <stdio.h> // jpeglib.h needs stdio.h FILE 40 #include <stdio.h> // jpeglib.h needs stdio.h FILE
41 #include "jpeglib.h" 41 #include "jpeglib.h"
42 } 42 }
43 43
44 namespace blink { 44 namespace blink {
45 45
46 struct JPEGOutputBuffer : public jpeg_destination_mgr { 46 struct JPEGOutputBuffer : public jpeg_destination_mgr {
47 Vector<unsigned char>* output; 47 Vector<unsigned char>* output;
(...skipping 24 matching lines...) Expand all
72 const size_t size = out->buffer.size() - out->free_in_buffer; 72 const size_t size = out->buffer.size() - out->free_in_buffer;
73 out->output->append(out->buffer.data(), size); 73 out->output->append(out->buffer.data(), size);
74 } 74 }
75 75
76 static void handleError(j_common_ptr common) 76 static void handleError(j_common_ptr common)
77 { 77 {
78 jmp_buf* jumpBufferPtr = static_cast<jmp_buf*>(common->client_data); 78 jmp_buf* jumpBufferPtr = static_cast<jmp_buf*>(common->client_data);
79 longjmp(*jumpBufferPtr, -1); 79 longjmp(*jumpBufferPtr, -1);
80 } 80 }
81 81
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) 82 static void RGBAtoRGB(const unsigned char* pixels, unsigned pixelCount, unsigned char* output)
93 { 83 {
84 // Per <canvas> spec, composite the input image pixels source-over on black.
85
94 for (; pixelCount-- > 0; pixels += 4) { 86 for (; pixelCount-- > 0; pixels += 4) {
95 // Do source-over composition on black.
96 unsigned char alpha = pixels[3]; 87 unsigned char alpha = pixels[3];
97 if (alpha != 255) { 88 if (alpha != 255) {
98 *output++ = SkMulDiv255Round(pixels[0], alpha); 89 *output++ = SkMulDiv255Round(pixels[0], alpha);
99 *output++ = SkMulDiv255Round(pixels[1], alpha); 90 *output++ = SkMulDiv255Round(pixels[1], alpha);
100 *output++ = SkMulDiv255Round(pixels[2], alpha); 91 *output++ = SkMulDiv255Round(pixels[2], alpha);
101 } else { 92 } else {
102 *output++ = pixels[0]; 93 *output++ = pixels[0];
103 *output++ = pixels[1]; 94 *output++ = pixels[1];
104 *output++ = pixels[2]; 95 *output++ = pixels[2];
105 } 96 }
106 } 97 }
107 } 98 }
108 99
109 static void disableSubsamplingForHighQuality(jpeg_compress_struct* cinfo, int qu ality) 100 static void disableSubsamplingForHighQuality(jpeg_compress_struct* cinfo, int qu ality)
110 { 101 {
111 if (quality < 100) 102 if (quality < 100)
112 return; 103 return;
113 104
114 for (int i = 0; i < MAX_COMPONENTS; ++i) { 105 for (int i = 0; i < MAX_COMPONENTS; ++i) {
115 cinfo->comp_info[i].h_samp_factor = 1; 106 cinfo->comp_info[i].h_samp_factor = 1;
116 cinfo->comp_info[i].v_samp_factor = 1; 107 cinfo->comp_info[i].v_samp_factor = 1;
117 } 108 }
118 } 109 }
119 110
120 static bool encodePixels(IntSize imageSize, const unsigned char* inputPixels, bo ol premultiplied, int quality, Vector<unsigned char>* output) 111 static bool encodePixels(IntSize imageSize, const unsigned char* inputPixels, in t quality, Vector<unsigned char>* output)
121 { 112 {
122 if (imageSize.width() <= 0 || imageSize.height() <= 0) 113 if (imageSize.width() <= 0 || imageSize.height() <= 0)
123 return false; 114 return false;
124 115
125 JPEGOutputBuffer destination; 116 JPEGOutputBuffer destination;
126 destination.output = output; 117 destination.output = output;
127 Vector<JSAMPLE> row; 118 Vector<JSAMPLE> row;
128 119
129 jpeg_compress_struct cinfo; 120 jpeg_compress_struct cinfo;
130 jpeg_error_mgr error; 121 jpeg_error_mgr error;
131 cinfo.err = jpeg_std_error(&error); 122 cinfo.err = jpeg_std_error(&error);
132 error.error_exit = handleError; 123 error.error_exit = handleError;
133 jmp_buf jumpBuffer; 124 jmp_buf jumpBuffer;
134 cinfo.client_data = &jumpBuffer; 125 cinfo.client_data = &jumpBuffer;
135 126
136 if (setjmp(jumpBuffer)) { 127 if (setjmp(jumpBuffer)) {
137 jpeg_destroy_compress(&cinfo); 128 jpeg_destroy_compress(&cinfo);
138 return false; 129 return false;
139 } 130 }
140 131
141 jpeg_create_compress(&cinfo); 132 jpeg_create_compress(&cinfo);
142 cinfo.dest = &destination; 133 cinfo.dest = &destination;
143 cinfo.dest->init_destination = prepareOutput; 134 cinfo.dest->init_destination = prepareOutput;
144 cinfo.dest->empty_output_buffer = writeOutput; 135 cinfo.dest->empty_output_buffer = writeOutput;
145 cinfo.dest->term_destination = finishOutput; 136 cinfo.dest->term_destination = finishOutput;
146 137
147 cinfo.image_height = imageSize.height(); 138 cinfo.image_height = imageSize.height();
148 cinfo.image_width = imageSize.width(); 139 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; 140 cinfo.in_color_space = JCS_RGB;
175 cinfo.input_components = 3; 141 cinfo.input_components = 3;
176 142
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); 143 jpeg_set_defaults(&cinfo);
183 jpeg_set_quality(&cinfo, quality, TRUE); 144 jpeg_set_quality(&cinfo, quality, TRUE);
184 disableSubsamplingForHighQuality(&cinfo, quality); 145 disableSubsamplingForHighQuality(&cinfo, quality);
185 jpeg_start_compress(&cinfo, TRUE); 146 jpeg_start_compress(&cinfo, TRUE);
186 147
187 unsigned char* pixels = const_cast<unsigned char*>(inputPixels); 148 unsigned char* pixels = const_cast<unsigned char*>(inputPixels);
188 row.resize(cinfo.image_width * cinfo.input_components); 149 row.resize(cinfo.image_width * cinfo.input_components);
189 const size_t pixelRowStride = cinfo.image_width * 4; 150 const size_t pixelRowStride = cinfo.image_width * 4;
190 while (cinfo.next_scanline < cinfo.image_height) { 151 while (cinfo.next_scanline < cinfo.image_height) {
191 JSAMPLE* rowData = row.data(); 152 JSAMPLE* rowData = row.data();
192 extractRowRGB(pixels, cinfo.image_width, rowData); 153 RGBAtoRGB(pixels, cinfo.image_width, rowData);
193 jpeg_write_scanlines(&cinfo, &rowData, 1); 154 jpeg_write_scanlines(&cinfo, &rowData, 1);
194 pixels += pixelRowStride; 155 pixels += pixelRowStride;
195 } 156 }
196 157
197 jpeg_finish_compress(&cinfo); 158 jpeg_finish_compress(&cinfo);
198 jpeg_destroy_compress(&cinfo); 159 jpeg_destroy_compress(&cinfo);
199 return true; 160 return true;
200 } 161 }
201 162
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) 163 bool JPEGImageEncoder::encode(const ImageDataBuffer& imageData, int quality, Vec tor<unsigned char>* output)
213 { 164 {
214 if (!imageData.pixels()) 165 if (!imageData.pixels())
215 return false; 166 return false;
216 167
217 return encodePixels(IntSize(imageData.width(), imageData.height()), imageDat a.pixels(), false, quality, output); 168 return encodePixels(IntSize(imageData.width(), imageData.height()), imageDat a.pixels(), quality, output);
218 } 169 }
219 170
220 } // namespace blink 171 } // namespace blink
OLDNEW
« no previous file with comments | « Source/platform/image-encoders/skia/JPEGImageEncoder.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698