OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/gfx/chromeos/codec/jpeg_codec_robust_slow.h" | 5 #include "ui/gfx//codec/chromeos/jpeg_codec_robust_slow.h" |
6 | 6 |
7 #include <setjmp.h> | 7 #include <setjmp.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 | 10 |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "third_party/skia/include/core/SkBitmap.h" | |
13 #include "third_party/skia/include/core/SkColorPriv.h" | |
14 | 12 |
15 extern "C" { | 13 extern "C" { |
16 // IJG provides robust JPEG decode | 14 // IJG provides robust JPEG decode |
17 #include "third_party/libjpeg/jpeglib.h" | 15 #include "third_party/libjpeg/jpeglib.h" |
18 } | 16 } |
19 | 17 |
18 #include "third_party/skia/include/core/SkBitmap.h" | |
sky
2017/03/15 20:27:34
Is there a reason these need to be moved down?
chrishtr
2017/03/15 20:29:00
Yes - the presubmit check complained about the inc
| |
19 #include "third_party/skia/include/core/SkColorPriv.h" | |
20 | |
20 namespace gfx { | 21 namespace gfx { |
21 | 22 |
22 // Encoder/decoder shared stuff ------------------------------------------------ | 23 // Encoder/decoder shared stuff ------------------------------------------------ |
23 | 24 |
24 namespace { | 25 namespace { |
25 | 26 |
26 // used to pass error info through the JPEG library | 27 // used to pass error info through the JPEG library |
27 struct CoderErrorMgr { | 28 struct CoderErrorMgr { |
28 jpeg_error_mgr pub; | 29 jpeg_error_mgr pub; |
29 jmp_buf setjmp_buffer; | 30 jmp_buf setjmp_buffer; |
30 }; | 31 }; |
31 | 32 |
32 void ErrorExit(jpeg_common_struct* cinfo) { | 33 void ErrorExit(jpeg_common_struct* cinfo) { |
33 CoderErrorMgr *err = reinterpret_cast<CoderErrorMgr*>(cinfo->err); | 34 CoderErrorMgr* err = reinterpret_cast<CoderErrorMgr*>(cinfo->err); |
34 | 35 |
35 // Return control to the setjmp point. | 36 // Return control to the setjmp point. |
36 longjmp(err->setjmp_buffer, false); | 37 longjmp(err->setjmp_buffer, false); |
37 } | 38 } |
38 | 39 |
39 } // namespace | 40 } // namespace |
40 | 41 |
41 // Decoder -------------------------------------------------------------------- | 42 // Decoder -------------------------------------------------------------------- |
42 | 43 |
43 namespace { | 44 namespace { |
44 | 45 |
45 struct JpegDecoderState { | 46 struct JpegDecoderState { |
46 JpegDecoderState(const unsigned char* in, size_t len) | 47 JpegDecoderState(const unsigned char* in, size_t len) |
47 : input_buffer(in), input_buffer_length(len) { | 48 : input_buffer(in), input_buffer_length(len) {} |
48 } | |
49 | 49 |
50 const unsigned char* input_buffer; | 50 const unsigned char* input_buffer; |
51 size_t input_buffer_length; | 51 size_t input_buffer_length; |
52 }; | 52 }; |
53 | 53 |
54 // Callback to initialize the source. | 54 // Callback to initialize the source. |
55 // | 55 // |
56 // From the JPEG library: | 56 // From the JPEG library: |
57 // "Initialize source. This is called by jpeg_read_header() before any data is | 57 // "Initialize source. This is called by jpeg_read_header() before any data is |
58 // actually read. May leave bytes_in_buffer set to 0 (in which case a | 58 // actually read. May leave bytes_in_buffer set to 0 (in which case a |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
105 cinfo->src->next_input_byte += num_bytes; | 105 cinfo->src->next_input_byte += num_bytes; |
106 } | 106 } |
107 } | 107 } |
108 | 108 |
109 // Our source doesn't need any cleanup, so this is a NOP. | 109 // Our source doesn't need any cleanup, so this is a NOP. |
110 // | 110 // |
111 // From the JPEG library: | 111 // From the JPEG library: |
112 // "Terminate source --- called by jpeg_finish_decompress() after all data has | 112 // "Terminate source --- called by jpeg_finish_decompress() after all data has |
113 // been read to clean up JPEG source manager. NOT called by jpeg_abort() or | 113 // been read to clean up JPEG source manager. NOT called by jpeg_abort() or |
114 // jpeg_destroy()." | 114 // jpeg_destroy()." |
115 void TermSource(j_decompress_ptr cinfo) { | 115 void TermSource(j_decompress_ptr cinfo) {} |
116 } | |
117 | 116 |
118 #if !defined(JCS_EXTENSIONS) | 117 #if !defined(JCS_EXTENSIONS) |
119 // Converts one row of rgb data to rgba data by adding a fully-opaque alpha | 118 // Converts one row of rgb data to rgba data by adding a fully-opaque alpha |
120 // value. | 119 // value. |
121 void AddAlpha(const unsigned char* rgb, int pixel_width, unsigned char* rgba) { | 120 void AddAlpha(const unsigned char* rgb, int pixel_width, unsigned char* rgba) { |
122 for (int x = 0; x < pixel_width; x++) { | 121 for (int x = 0; x < pixel_width; x++) { |
123 memcpy(&rgba[x * 4], &rgb[x * 3], 3); | 122 memcpy(&rgba[x * 4], &rgb[x * 3], 3); |
124 rgba[x * 4 + 3] = 0xff; | 123 rgba[x * 4 + 3] = 0xff; |
125 } | 124 } |
126 } | 125 } |
127 | 126 |
128 // Converts one row of RGB data to BGRA by reordering the color components and | 127 // Converts one row of RGB data to BGRA by reordering the color components and |
129 // adding alpha values of 0xff. | 128 // adding alpha values of 0xff. |
130 void RGBtoBGRA(const unsigned char* bgra, int pixel_width, unsigned char* rgb) | 129 void RGBtoBGRA(const unsigned char* bgra, int pixel_width, unsigned char* rgb) { |
131 { | |
132 for (int x = 0; x < pixel_width; x++) { | 130 for (int x = 0; x < pixel_width; x++) { |
133 const unsigned char* pixel_in = &bgra[x * 3]; | 131 const unsigned char* pixel_in = &bgra[x * 3]; |
134 unsigned char* pixel_out = &rgb[x * 4]; | 132 unsigned char* pixel_out = &rgb[x * 4]; |
135 pixel_out[0] = pixel_in[2]; | 133 pixel_out[0] = pixel_in[2]; |
136 pixel_out[1] = pixel_in[1]; | 134 pixel_out[1] = pixel_in[1]; |
137 pixel_out[2] = pixel_in[0]; | 135 pixel_out[2] = pixel_in[0]; |
138 pixel_out[3] = 0xff; | 136 pixel_out[3] = 0xff; |
139 } | 137 } |
140 } | 138 } |
141 #endif // !defined(JCS_EXTENSIONS) | 139 #endif // !defined(JCS_EXTENSIONS) |
142 | 140 |
143 // This class destroys the given jpeg_decompress object when it goes out of | 141 // This class destroys the given jpeg_decompress object when it goes out of |
144 // scope. It simplifies the error handling in Decode (and even applies to the | 142 // scope. It simplifies the error handling in Decode (and even applies to the |
145 // success case). | 143 // success case). |
146 class DecompressDestroyer { | 144 class DecompressDestroyer { |
147 public: | 145 public: |
148 DecompressDestroyer() : cinfo_(NULL) { | 146 DecompressDestroyer() : cinfo_(NULL) {} |
149 } | 147 ~DecompressDestroyer() { DestroyManagedObject(); } |
150 ~DecompressDestroyer() { | |
151 DestroyManagedObject(); | |
152 } | |
153 void SetManagedObject(jpeg_decompress_struct* ci) { | 148 void SetManagedObject(jpeg_decompress_struct* ci) { |
154 DestroyManagedObject(); | 149 DestroyManagedObject(); |
155 cinfo_ = ci; | 150 cinfo_ = ci; |
156 } | 151 } |
157 void DestroyManagedObject() { | 152 void DestroyManagedObject() { |
158 if (cinfo_) { | 153 if (cinfo_) { |
159 jpeg_destroy_decompress(cinfo_); | 154 jpeg_destroy_decompress(cinfo_); |
160 cinfo_ = NULL; | 155 cinfo_ = NULL; |
161 } | 156 } |
162 } | 157 } |
158 | |
163 private: | 159 private: |
164 jpeg_decompress_struct* cinfo_; | 160 jpeg_decompress_struct* cinfo_; |
165 }; | 161 }; |
166 | 162 |
167 } // namespace | 163 } // namespace |
168 | 164 |
169 bool JPEGCodecRobustSlow::Decode(const unsigned char* input, size_t input_size, | 165 bool JPEGCodecRobustSlow::Decode(const unsigned char* input, |
166 size_t input_size, | |
170 ColorFormat format, | 167 ColorFormat format, |
171 std::vector<unsigned char>* output, int* w, | 168 std::vector<unsigned char>* output, |
169 int* w, | |
172 int* h) { | 170 int* h) { |
173 jpeg_decompress_struct cinfo; | 171 jpeg_decompress_struct cinfo; |
174 DecompressDestroyer destroyer; | 172 DecompressDestroyer destroyer; |
175 destroyer.SetManagedObject(&cinfo); | 173 destroyer.SetManagedObject(&cinfo); |
176 output->clear(); | 174 output->clear(); |
177 | 175 |
178 // We set up the normal JPEG error routines, then override error_exit. | 176 // We set up the normal JPEG error routines, then override error_exit. |
179 // This must be done before the call to create_decompress. | 177 // This must be done before the call to create_decompress. |
180 CoderErrorMgr errmgr; | 178 CoderErrorMgr errmgr; |
181 cinfo.err = jpeg_std_error(&errmgr.pub); | 179 cinfo.err = jpeg_std_error(&errmgr.pub); |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
334 int data_length = w * h * 4; | 332 int data_length = w * h * 4; |
335 | 333 |
336 SkBitmap* bitmap = new SkBitmap(); | 334 SkBitmap* bitmap = new SkBitmap(); |
337 bitmap->allocN32Pixels(w, h); | 335 bitmap->allocN32Pixels(w, h); |
338 memcpy(bitmap->getAddr32(0, 0), &data_vector[0], data_length); | 336 memcpy(bitmap->getAddr32(0, 0), &data_vector[0], data_length); |
339 | 337 |
340 return bitmap; | 338 return bitmap; |
341 } | 339 } |
342 | 340 |
343 } // namespace gfx | 341 } // namespace gfx |
OLD | NEW |