OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
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 #include "SkImageEncoder.h" | 8 #include "SkImageEncoder_argb.h" |
9 #include "SkBitmap.h" | 9 #include "SkBitmap.h" |
10 #include "SkColorPriv.h" | 10 #include "SkColorPriv.h" |
11 #include "SkStream.h" | 11 #include "SkStream.h" |
12 #include "SkTemplates.h" | 12 #include "SkTemplates.h" |
13 | 13 |
14 class SkARGBImageEncoder : public SkImageEncoder { | |
15 protected: | |
16 virtual bool onEncode(SkWStream* stream, const SkBitmap& bm, int quality) SK
_OVERRIDE; | |
17 | |
18 private: | |
19 typedef SkImageEncoder INHERITED; | |
20 }; | |
21 | 14 |
22 typedef void (*ScanlineImporter)(const uint8_t* in, uint8_t* argb, int width, | 15 typedef void (*ScanlineImporter)(const uint8_t* in, uint8_t* argb, int width, |
23 const SkPMColor* SK_RESTRICT ctable); | 16 const SkPMColor* SK_RESTRICT ctable); |
24 | 17 |
25 static void ARGB_8888_To_ARGB(const uint8_t* in, uint8_t* argb, int width, const
SkPMColor*) { | 18 static void ARGB_8888_To_ARGB(const uint8_t* in, uint8_t* argb, int width, const
SkPMColor*) { |
26 const uint32_t* SK_RESTRICT src = (const uint32_t*)in; | 19 const uint32_t* SK_RESTRICT src = (const uint32_t*)in; |
27 for (int i = 0; i < width; ++i) { | 20 for (int i = 0; i < width; ++i) { |
28 const uint32_t c = *src++; | 21 const uint32_t c = *src++; |
29 argb[0] = SkGetPackedA32(c); | 22 argb[0] = SkGetPackedA32(c); |
30 argb[1] = SkGetPackedR32(c); | 23 argb[1] = SkGetPackedR32(c); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 return RGB_565_To_ARGB; | 72 return RGB_565_To_ARGB; |
80 case kARGB_4444_SkColorType: | 73 case kARGB_4444_SkColorType: |
81 return ARGB_4444_To_ARGB; | 74 return ARGB_4444_To_ARGB; |
82 case kIndex_8_SkColorType: | 75 case kIndex_8_SkColorType: |
83 return Index8_To_ARGB; | 76 return Index8_To_ARGB; |
84 default: | 77 default: |
85 return NULL; | 78 return NULL; |
86 } | 79 } |
87 } | 80 } |
88 | 81 |
89 bool SkARGBImageEncoder::onEncode(SkWStream* stream, const SkBitmap& bitmap, int
) { | 82 bool SkImageEncoder_argb::onEncode(SkWStream* stream, const SkBitmap& bitmap, in
t) { |
90 const ScanlineImporter scanline_import = ChooseImporter(bitmap.colorType()); | 83 const ScanlineImporter scanline_import = ChooseImporter(bitmap.colorType()); |
91 if (NULL == scanline_import) { | 84 if (NULL == scanline_import) { |
92 return false; | 85 return false; |
93 } | 86 } |
94 | 87 |
95 SkAutoLockPixels alp(bitmap); | 88 SkAutoLockPixels alp(bitmap); |
96 const uint8_t* src = (uint8_t*)bitmap.getPixels(); | 89 const uint8_t* src = (uint8_t*)bitmap.getPixels(); |
97 if (NULL == bitmap.getPixels()) { | 90 if (NULL == bitmap.getPixels()) { |
98 return false; | 91 return false; |
99 } | 92 } |
100 | 93 |
101 SkAutoLockColors ctLocker; | 94 SkAutoLockColors ctLocker; |
102 const SkPMColor* colors = ctLocker.lockColors(bitmap); | 95 const SkPMColor* colors = ctLocker.lockColors(bitmap); |
103 | 96 |
104 const int argbStride = bitmap.width() * 4; | 97 const int argbStride = bitmap.width() * 4; |
105 SkAutoTDeleteArray<uint8_t> ada(new uint8_t[argbStride]); | 98 SkAutoTDeleteArray<uint8_t> ada(new uint8_t[argbStride]); |
106 uint8_t* argb = ada.get(); | 99 uint8_t* argb = ada.get(); |
107 for (int y = 0; y < bitmap.height(); ++y) { | 100 for (int y = 0; y < bitmap.height(); ++y) { |
108 scanline_import(src + y * bitmap.rowBytes(), argb, bitmap.width(), color
s); | 101 scanline_import(src + y * bitmap.rowBytes(), argb, bitmap.width(), color
s); |
109 stream->write(argb, argbStride); | 102 stream->write(argb, argbStride); |
110 } | 103 } |
111 | 104 |
112 return true; | 105 return true; |
113 } | 106 } |
114 | |
115 | |
116 /////////////////////////////////////////////////////////////////////////////// | |
117 DEFINE_ENCODER_CREATOR(ARGBImageEncoder); | |
118 /////////////////////////////////////////////////////////////////////////////// | |
OLD | NEW |