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

Side by Side Diff: samplecode/SampleEncode.cpp

Issue 169063002: use SkColorType instead of SkBitmap::Config in samplecode (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Created 6 years, 10 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 | « samplecode/SampleDitherBitmap.cpp ('k') | samplecode/SampleFatBits.cpp » ('j') | 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 /* 2 /*
3 * Copyright 2011 Google Inc. 3 * Copyright 2011 Google Inc.
4 * 4 *
5 * Use of this source code is governed by a BSD-style license that can be 5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file. 6 * found in the LICENSE file.
7 */ 7 */
8 #include "SampleCode.h" 8 #include "SampleCode.h"
9 #include "SkView.h" 9 #include "SkView.h"
10 #include "SkCanvas.h" 10 #include "SkCanvas.h"
11 #include "SkData.h" 11 #include "SkData.h"
12 #include "SkDecodingImageGenerator.h" 12 #include "SkDecodingImageGenerator.h"
13 #include "SkGradientShader.h" 13 #include "SkGradientShader.h"
14 #include "SkGraphics.h" 14 #include "SkGraphics.h"
15 #include "SkImageDecoder.h" 15 #include "SkImageDecoder.h"
16 #include "SkImageEncoder.h" 16 #include "SkImageEncoder.h"
17 #include "SkPath.h" 17 #include "SkPath.h"
18 #include "SkRegion.h" 18 #include "SkRegion.h"
19 #include "SkShader.h" 19 #include "SkShader.h"
20 #include "SkUtils.h" 20 #include "SkUtils.h"
21 #include "SkXfermode.h" 21 #include "SkXfermode.h"
22 #include "SkColorPriv.h" 22 #include "SkColorPriv.h"
23 #include "SkColorFilter.h" 23 #include "SkColorFilter.h"
24 #include "SkTime.h" 24 #include "SkTime.h"
25 #include "SkTypeface.h" 25 #include "SkTypeface.h"
26 26
27 #include "SkStream.h" 27 #include "SkStream.h"
28 28
29 static void make_image(SkBitmap* bm, SkBitmap::Config config, int configIndex) { 29 static void make_image(SkBitmap* bm, SkColorType ct, int configIndex) {
30 const int width = 98; 30 const int width = 98;
31 const int height = 100; 31 const int height = 100;
32 const SkImageInfo info = SkImageInfo::Make(width, height, ct, kPremul_SkAlph aType);
33
32 SkBitmap device; 34 SkBitmap device;
33 35 device.allocN32Pixels(width, height);
34 device.setConfig(SkBitmap::kARGB_8888_Config, width, height);
35 device.allocPixels();
36
37 SkCanvas canvas(device); 36 SkCanvas canvas(device);
38 SkPaint paint; 37 SkPaint paint;
39 38
40 paint.setAntiAlias(true); 39 paint.setAntiAlias(true);
41 canvas.drawColor(SK_ColorRED); 40 canvas.drawColor(SK_ColorRED);
42 paint.setColor(SK_ColorBLUE); 41 paint.setColor(SK_ColorBLUE);
43 canvas.drawCircle(SkIntToScalar(width)/2, SkIntToScalar(height)/2, 42 canvas.drawCircle(SkIntToScalar(width)/2, SkIntToScalar(height)/2,
44 SkIntToScalar(width)/2, paint); 43 SkIntToScalar(width)/2, paint);
45 44
46 bm->setConfig(config, width, height); 45 switch (ct) {
47 switch (config) { 46 case kPMColor_SkColorType:
48 case SkBitmap::kARGB_8888_Config:
49 bm->swap(device); 47 bm->swap(device);
50 break; 48 break;
51 case SkBitmap::kRGB_565_Config: { 49 case kRGB_565_SkColorType: {
52 bm->allocPixels(); 50 bm->allocPixels(info);
53 for (int y = 0; y < height; y++) { 51 for (int y = 0; y < height; y++) {
54 for (int x = 0; x < width; x++) { 52 for (int x = 0; x < width; x++) {
55 *bm->getAddr16(x, y) = SkPixel32ToPixel16(*device.getAddr32( x, y)); 53 *bm->getAddr16(x, y) = SkPixel32ToPixel16(*device.getAddr32( x, y));
56 } 54 }
57 } 55 }
58 break; 56 break;
59 } 57 }
60 case SkBitmap::kIndex8_Config: { 58 case kIndex_8_SkColorType: {
61 SkPMColor colors[256]; 59 SkPMColor colors[256];
62 for (int i = 0; i < 256; i++) { 60 for (int i = 0; i < 256; i++) {
63 if (configIndex & 1) { 61 if (configIndex & 1) {
64 colors[i] = SkPackARGB32(255-i, 0, 0, 255-i); 62 colors[i] = SkPackARGB32(255-i, 0, 0, 255-i);
65 } else { 63 } else {
66 colors[i] = SkPackARGB32(0xFF, i, 0, 255-i); 64 colors[i] = SkPackARGB32(0xFF, i, 0, 255-i);
67 } 65 }
68 } 66 }
69 SkColorTable* ctable = new SkColorTable(colors, 256); 67 SkColorTable* ctable = new SkColorTable(colors, 256);
70 bm->allocPixels(ctable); 68 bm->allocPixels(info, NULL, ctable);
71 ctable->unref(); 69 ctable->unref();
72
73 for (int y = 0; y < height; y++) { 70 for (int y = 0; y < height; y++) {
74 for (int x = 0; x < width; x++) { 71 for (int x = 0; x < width; x++) {
75 *bm->getAddr8(x, y) = SkGetPackedR32(*device.getAddr32(x, y) ); 72 *bm->getAddr8(x, y) = SkGetPackedR32(*device.getAddr32(x, y) );
76 } 73 }
77 } 74 }
78 break; 75 break;
79 } 76 }
80 default: 77 default:
81 break; 78 SkASSERT(0);
82 } 79 }
83 } 80 }
84 81
85 // configs to build the original bitmap in. Can be at most these 3 82 // configs to build the original bitmap in. Can be at most these 3
86 static const SkBitmap::Config gConfigs[] = { 83 static const SkColorType gColorTypes[] = {
87 SkBitmap::kARGB_8888_Config, 84 kPMColor_SkColorType,
88 SkBitmap::kRGB_565_Config, 85 kRGB_565_SkColorType,
89 SkBitmap::kIndex8_Config, // opaque 86 kIndex_8_SkColorType, // opaque
90 SkBitmap::kIndex8_Config // alpha 87 kIndex_8_SkColorType // alpha
91 }; 88 };
92 89
93 static const char* const gConfigLabels[] = { 90 static const char* const gConfigLabels[] = {
94 "8888", "565", "Index8", "Index8 alpha" 91 "8888", "565", "Index8", "Index8 alpha"
95 }; 92 };
96 93
97 // types to encode into. Can be at most these 3. Must match up with gExt[] 94 // types to encode into. Can be at most these 3. Must match up with gExt[]
98 static const SkImageEncoder::Type gTypes[] = { 95 static const SkImageEncoder::Type gTypes[] = {
99 SkImageEncoder::kJPEG_Type, 96 SkImageEncoder::kJPEG_Type,
100 SkImageEncoder::kPNG_Type 97 SkImageEncoder::kPNG_Type
101 }; 98 };
102 99
103 // must match up with gTypes[] 100 // must match up with gTypes[]
104 static const char* const gExt[] = { 101 static const char* const gExt[] = {
105 ".jpg", ".png" 102 ".jpg", ".png"
106 }; 103 };
107 104
108 #include <sys/stat.h> 105 #include <sys/stat.h>
109 106
110 class EncodeView : public SampleView { 107 class EncodeView : public SampleView {
111 public: 108 public:
112 SkBitmap* fBitmaps; 109 SkBitmap* fBitmaps;
113 SkAutoDataUnref* fEncodedPNGs; 110 SkAutoDataUnref* fEncodedPNGs;
114 SkAutoDataUnref* fEncodedJPEGs; 111 SkAutoDataUnref* fEncodedJPEGs;
115 int fBitmapCount; 112 int fBitmapCount;
116 113
117 EncodeView() { 114 EncodeView() {
118 #if 1 115 fBitmapCount = SK_ARRAY_COUNT(gColorTypes);
119 fBitmapCount = SK_ARRAY_COUNT(gConfigs);
120 fBitmaps = new SkBitmap[fBitmapCount]; 116 fBitmaps = new SkBitmap[fBitmapCount];
121 fEncodedPNGs = new SkAutoDataUnref[fBitmapCount]; 117 fEncodedPNGs = new SkAutoDataUnref[fBitmapCount];
122 fEncodedJPEGs = new SkAutoDataUnref[fBitmapCount]; 118 fEncodedJPEGs = new SkAutoDataUnref[fBitmapCount];
123 for (int i = 0; i < fBitmapCount; i++) { 119 for (int i = 0; i < fBitmapCount; i++) {
124 make_image(&fBitmaps[i], gConfigs[i], i); 120 make_image(&fBitmaps[i], gColorTypes[i], i);
125 121
126 for (size_t j = 0; j < SK_ARRAY_COUNT(gTypes); j++) { 122 for (size_t j = 0; j < SK_ARRAY_COUNT(gTypes); j++) {
127 SkAutoTDelete<SkImageEncoder> codec( 123 SkAutoTDelete<SkImageEncoder> codec(
128 SkImageEncoder::Create(gTypes[j])); 124 SkImageEncoder::Create(gTypes[j]));
129 if (NULL == codec.get()) { 125 if (NULL == codec.get()) {
130 SkDebugf("[%s:%d] failed to encode %s%s\n", 126 SkDebugf("[%s:%d] failed to encode %s%s\n",
131 __FILE__, __LINE__,gConfigLabels[i], gExt[j]); 127 __FILE__, __LINE__,gConfigLabels[i], gExt[j]);
132 continue; 128 continue;
133 } 129 }
134 SkAutoDataUnref data(codec->encodeData(fBitmaps[i], 100)); 130 SkAutoDataUnref data(codec->encodeData(fBitmaps[i], 100));
135 if (NULL == data.get()) { 131 if (NULL == data.get()) {
136 SkDebugf("[%s:%d] failed to encode %s%s\n", 132 SkDebugf("[%s:%d] failed to encode %s%s\n",
137 __FILE__, __LINE__,gConfigLabels[i], gExt[j]); 133 __FILE__, __LINE__,gConfigLabels[i], gExt[j]);
138 continue; 134 continue;
139 } 135 }
140 if (SkImageEncoder::kJPEG_Type == gTypes[j]) { 136 if (SkImageEncoder::kJPEG_Type == gTypes[j]) {
141 fEncodedJPEGs[i].reset(data.detach()); 137 fEncodedJPEGs[i].reset(data.detach());
142 } else if (SkImageEncoder::kPNG_Type == gTypes[j]) { 138 } else if (SkImageEncoder::kPNG_Type == gTypes[j]) {
143 fEncodedPNGs[i].reset(data.detach()); 139 fEncodedPNGs[i].reset(data.detach());
144 } 140 }
145 } 141 }
146 } 142 }
147 #else
148 fBitmaps = NULL;
149 fBitmapCount = 0;
150 #endif
151 this->setBGColor(0xFFDDDDDD); 143 this->setBGColor(0xFFDDDDDD);
152 } 144 }
153 145
154 virtual ~EncodeView() { 146 virtual ~EncodeView() {
155 delete[] fBitmaps; 147 delete[] fBitmaps;
156 delete[] fEncodedPNGs; 148 delete[] fEncodedPNGs;
157 delete[] fEncodedJPEGs; 149 delete[] fEncodedJPEGs;
158 } 150 }
159 151
160 protected: 152 protected:
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
235 } 227 }
236 228
237 private: 229 private:
238 typedef SampleView INHERITED; 230 typedef SampleView INHERITED;
239 }; 231 };
240 232
241 ////////////////////////////////////////////////////////////////////////////// 233 //////////////////////////////////////////////////////////////////////////////
242 234
243 static SkView* MyFactory() { return new EncodeView; } 235 static SkView* MyFactory() { return new EncodeView; }
244 static SkViewRegister reg(MyFactory); 236 static SkViewRegister reg(MyFactory);
OLDNEW
« no previous file with comments | « samplecode/SampleDitherBitmap.cpp ('k') | samplecode/SampleFatBits.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698