OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2007 The Android Open Source Project | 2 * Copyright 2007 The Android Open Source Project |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 | 8 |
9 #include "SkImageDecoder.h" | 9 #include "SkImageDecoder.h" |
10 #include "SkImageEncoder.h" | 10 #include "SkImageEncoder.h" |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 SkASSERT(cinfo != NULL); | 58 SkASSERT(cinfo != NULL); |
59 SkASSERT(src_mgr != NULL); | 59 SkASSERT(src_mgr != NULL); |
60 jpeg_create_decompress(cinfo); | 60 jpeg_create_decompress(cinfo); |
61 overwrite_mem_buffer_size(cinfo); | 61 overwrite_mem_buffer_size(cinfo); |
62 cinfo->src = src_mgr; | 62 cinfo->src = src_mgr; |
63 } | 63 } |
64 | 64 |
65 #ifdef SK_BUILD_FOR_ANDROID | 65 #ifdef SK_BUILD_FOR_ANDROID |
66 class SkJPEGImageIndex { | 66 class SkJPEGImageIndex { |
67 public: | 67 public: |
68 SkJPEGImageIndex(SkStream* stream, SkImageDecoder* decoder) | 68 SkJPEGImageIndex(SkStreamRewindable* stream, SkImageDecoder* decoder) |
69 : fSrcMgr(stream, decoder) | 69 : fSrcMgr(stream, decoder) |
70 , fInfoInitialized(false) | 70 , fInfoInitialized(false) |
71 , fHuffmanCreated(false) | 71 , fHuffmanCreated(false) |
72 , fDecompressStarted(false) | 72 , fDecompressStarted(false) |
73 { | 73 { |
74 SkDEBUGCODE(fReadHeaderSucceeded = false;) | 74 SkDEBUGCODE(fReadHeaderSucceeded = false;) |
75 } | 75 } |
76 | 76 |
77 ~SkJPEGImageIndex() { | 77 ~SkJPEGImageIndex() { |
78 if (fHuffmanCreated) { | 78 if (fHuffmanCreated) { |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 SkDELETE(fImageIndex); | 188 SkDELETE(fImageIndex); |
189 } | 189 } |
190 #endif | 190 #endif |
191 | 191 |
192 virtual Format getFormat() const { | 192 virtual Format getFormat() const { |
193 return kJPEG_Format; | 193 return kJPEG_Format; |
194 } | 194 } |
195 | 195 |
196 protected: | 196 protected: |
197 #ifdef SK_BUILD_FOR_ANDROID | 197 #ifdef SK_BUILD_FOR_ANDROID |
198 virtual bool onBuildTileIndex(SkStream *stream, int *width, int *height) SK_
OVERRIDE; | 198 virtual bool onBuildTileIndex(SkStreamRewindable *stream, int *width, int *h
eight) SK_OVERRIDE; |
199 virtual bool onDecodeSubset(SkBitmap* bitmap, const SkIRect& rect) SK_OVERRI
DE; | 199 virtual bool onDecodeSubset(SkBitmap* bitmap, const SkIRect& rect) SK_OVERRI
DE; |
200 #endif | 200 #endif |
201 virtual bool onDecode(SkStream* stream, SkBitmap* bm, Mode) SK_OVERRIDE; | 201 virtual bool onDecode(SkStream* stream, SkBitmap* bm, Mode) SK_OVERRIDE; |
202 | 202 |
203 private: | 203 private: |
204 #ifdef SK_BUILD_FOR_ANDROID | 204 #ifdef SK_BUILD_FOR_ANDROID |
205 SkJPEGImageIndex* fImageIndex; | 205 SkJPEGImageIndex* fImageIndex; |
206 int fImageWidth; | 206 int fImageWidth; |
207 int fImageHeight; | 207 int fImageHeight; |
208 #endif | 208 #endif |
(...skipping 421 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
630 if (!skip_src_rows(&cinfo, srcRow, | 630 if (!skip_src_rows(&cinfo, srcRow, |
631 cinfo.output_height - cinfo.output_scanline)) { | 631 cinfo.output_height - cinfo.output_scanline)) { |
632 return return_false(cinfo, *bm, "skip rows"); | 632 return return_false(cinfo, *bm, "skip rows"); |
633 } | 633 } |
634 jpeg_finish_decompress(&cinfo); | 634 jpeg_finish_decompress(&cinfo); |
635 | 635 |
636 return true; | 636 return true; |
637 } | 637 } |
638 | 638 |
639 #ifdef SK_BUILD_FOR_ANDROID | 639 #ifdef SK_BUILD_FOR_ANDROID |
640 bool SkJPEGImageDecoder::onBuildTileIndex(SkStream* stream, int *width, int *hei
ght) { | 640 bool SkJPEGImageDecoder::onBuildTileIndex(SkStreamRewindable* stream, int *width
, int *height) { |
641 | 641 |
642 SkAutoTDelete<SkJPEGImageIndex> imageIndex(SkNEW_ARGS(SkJPEGImageIndex, (str
eam, this))); | 642 SkAutoTDelete<SkJPEGImageIndex> imageIndex(SkNEW_ARGS(SkJPEGImageIndex, (str
eam, this))); |
643 jpeg_decompress_struct* cinfo = imageIndex->cinfo(); | 643 jpeg_decompress_struct* cinfo = imageIndex->cinfo(); |
644 | 644 |
645 skjpeg_error_mgr sk_err; | 645 skjpeg_error_mgr sk_err; |
646 set_error_mgr(cinfo, &sk_err); | 646 set_error_mgr(cinfo, &sk_err); |
647 | 647 |
648 // All objects need to be instantiated before this setjmp call so that | 648 // All objects need to be instantiated before this setjmp call so that |
649 // they will be cleaned up properly if an error occurs. | 649 // they will be cleaned up properly if an error occurs. |
650 if (setjmp(sk_err.fJmpBuf)) { | 650 if (setjmp(sk_err.fJmpBuf)) { |
(...skipping 462 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1113 | 1113 |
1114 return true; | 1114 return true; |
1115 } | 1115 } |
1116 }; | 1116 }; |
1117 | 1117 |
1118 /////////////////////////////////////////////////////////////////////////////// | 1118 /////////////////////////////////////////////////////////////////////////////// |
1119 DEFINE_DECODER_CREATOR(JPEGImageDecoder); | 1119 DEFINE_DECODER_CREATOR(JPEGImageDecoder); |
1120 DEFINE_ENCODER_CREATOR(JPEGImageEncoder); | 1120 DEFINE_ENCODER_CREATOR(JPEGImageEncoder); |
1121 /////////////////////////////////////////////////////////////////////////////// | 1121 /////////////////////////////////////////////////////////////////////////////// |
1122 | 1122 |
1123 static bool is_jpeg(SkStream* stream) { | 1123 static bool is_jpeg(SkStreamRewindable* stream) { |
1124 static const unsigned char gHeader[] = { 0xFF, 0xD8, 0xFF }; | 1124 static const unsigned char gHeader[] = { 0xFF, 0xD8, 0xFF }; |
1125 static const size_t HEADER_SIZE = sizeof(gHeader); | 1125 static const size_t HEADER_SIZE = sizeof(gHeader); |
1126 | 1126 |
1127 char buffer[HEADER_SIZE]; | 1127 char buffer[HEADER_SIZE]; |
1128 size_t len = stream->read(buffer, HEADER_SIZE); | 1128 size_t len = stream->read(buffer, HEADER_SIZE); |
1129 | 1129 |
1130 if (len != HEADER_SIZE) { | 1130 if (len != HEADER_SIZE) { |
1131 return false; // can't read enough | 1131 return false; // can't read enough |
1132 } | 1132 } |
1133 if (memcmp(buffer, gHeader, HEADER_SIZE)) { | 1133 if (memcmp(buffer, gHeader, HEADER_SIZE)) { |
1134 return false; | 1134 return false; |
1135 } | 1135 } |
1136 return true; | 1136 return true; |
1137 } | 1137 } |
1138 | 1138 |
1139 static SkImageDecoder* sk_libjpeg_dfactory(SkStream* stream) { | 1139 |
| 1140 static SkImageDecoder* sk_libjpeg_dfactory(SkStreamRewindable* stream) { |
1140 if (is_jpeg(stream)) { | 1141 if (is_jpeg(stream)) { |
1141 return SkNEW(SkJPEGImageDecoder); | 1142 return SkNEW(SkJPEGImageDecoder); |
1142 } | 1143 } |
1143 return NULL; | 1144 return NULL; |
1144 } | 1145 } |
1145 | 1146 |
1146 static SkImageDecoder::Format get_format_jpeg(SkStream* stream) { | 1147 static SkImageDecoder::Format get_format_jpeg(SkStreamRewindable* stream) { |
1147 if (is_jpeg(stream)) { | 1148 if (is_jpeg(stream)) { |
1148 return SkImageDecoder::kJPEG_Format; | 1149 return SkImageDecoder::kJPEG_Format; |
1149 } | 1150 } |
1150 return SkImageDecoder::kUnknown_Format; | 1151 return SkImageDecoder::kUnknown_Format; |
1151 } | 1152 } |
1152 | 1153 |
1153 static SkImageEncoder* sk_libjpeg_efactory(SkImageEncoder::Type t) { | 1154 static SkImageEncoder* sk_libjpeg_efactory(SkImageEncoder::Type t) { |
1154 return (SkImageEncoder::kJPEG_Type == t) ? SkNEW(SkJPEGImageEncoder) : NULL; | 1155 return (SkImageEncoder::kJPEG_Type == t) ? SkNEW(SkJPEGImageEncoder) : NULL; |
1155 } | 1156 } |
1156 | 1157 |
1157 | |
1158 static SkImageDecoder_DecodeReg gDReg(sk_libjpeg_dfactory); | 1158 static SkImageDecoder_DecodeReg gDReg(sk_libjpeg_dfactory); |
1159 static SkImageDecoder_FormatReg gFormatReg(get_format_jpeg); | 1159 static SkImageDecoder_FormatReg gFormatReg(get_format_jpeg); |
1160 static SkImageEncoder_EncodeReg gEReg(sk_libjpeg_efactory); | 1160 static SkImageEncoder_EncodeReg gEReg(sk_libjpeg_efactory); |
OLD | NEW |