blob: e4197bf0c7c627252c3d2b5453299d877025d7dd [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001#include "SampleCode.h"
2#include "SkView.h"
3#include "SkCanvas.h"
4#include "SkGradientShader.h"
5#include "SkGraphics.h"
reed@android.comb08eb2b2009-01-06 20:16:26 +00006#include "SkImageEncoder.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +00007#include "SkPath.h"
8#include "SkPorterDuff.h"
9#include "SkRegion.h"
10#include "SkShader.h"
11#include "SkUtils.h"
12#include "SkXfermode.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000013#include "SkColorPriv.h"
14#include "SkColorFilter.h"
15#include "SkTime.h"
16#include "SkTypeface.h"
17
18#include "SkImageRef.h"
19#include "SkStream.h"
20
21static void make_image(SkBitmap* bm, SkBitmap::Config config, int configIndex) {
22 const int width = 98;
23 const int height = 100;
24 SkBitmap device;
25
26 device.setConfig(SkBitmap::kARGB_8888_Config, width, height);
27 device.allocPixels();
28
29 SkCanvas canvas(device);
30 SkPaint paint;
31
32 paint.setAntiAlias(true);
33 canvas.drawColor(SK_ColorRED);
34 paint.setColor(SK_ColorBLUE);
35 canvas.drawCircle(SkIntToScalar(width)/2, SkIntToScalar(height)/2,
36 SkIntToScalar(width)/2, paint);
37
38 bm->setConfig(config, width, height);
39 switch (config) {
40 case SkBitmap::kARGB_8888_Config:
41 bm->swap(device);
42 break;
43 case SkBitmap::kRGB_565_Config: {
44 bm->allocPixels();
45 for (int y = 0; y < height; y++) {
46 for (int x = 0; x < width; x++) {
47 *bm->getAddr16(x, y) = SkPixel32ToPixel16(*device.getAddr32(x, y));
48 }
49 }
50 break;
51 }
52 case SkBitmap::kIndex8_Config: {
53 SkPMColor colors[256];
54 for (int i = 0; i < 256; i++) {
55 if (configIndex & 1) {
56 colors[i] = SkPackARGB32(255-i, 0, 0, 255-i);
57 } else {
58 colors[i] = SkPackARGB32(0xFF, i, 0, 255-i);
59 }
60 }
61 SkColorTable* ctable = new SkColorTable(colors, 256);
62 bm->allocPixels(ctable);
63 ctable->unref();
64
65 for (int y = 0; y < height; y++) {
66 for (int x = 0; x < width; x++) {
67 *bm->getAddr8(x, y) = SkGetPackedR32(*device.getAddr32(x, y));
68 }
69 }
70 break;
71 }
72 default:
73 break;
74 }
75}
76
77// configs to build the original bitmap in. Can be at most these 3
78static const SkBitmap::Config gConfigs[] = {
79 SkBitmap::kARGB_8888_Config,
80 SkBitmap::kRGB_565_Config,
81 SkBitmap::kIndex8_Config, // opaque
82 SkBitmap::kIndex8_Config // alpha
83};
84
85static const char* const gConfigLabels[] = {
86 "8888", "565", "Index8", "Index8 alpha"
87};
88
89// types to encode into. Can be at most these 3. Must match up with gExt[]
90static const SkImageEncoder::Type gTypes[] = {
91 SkImageEncoder::kJPEG_Type,
92 SkImageEncoder::kPNG_Type
93};
94
95// must match up with gTypes[]
96static const char* const gExt[] = {
97 ".jpg", ".png"
98};
99
100static const char* gPath = "/encoded/";
101
102static void make_name(SkString* name, int config, int ext) {
103 name->set(gPath);
104 name->append(gConfigLabels[config]);
105 name->append(gExt[ext]);
106}
107
108#include <sys/stat.h>
109
110class EncodeView : public SkView {
111public:
112 SkBitmap* fBitmaps;
113 size_t fBitmapCount;
114
115 EncodeView() {
116 #if 1
117 (void)mkdir(gPath, S_IRWXU | S_IRWXG | S_IRWXO);
118
119 fBitmapCount = SK_ARRAY_COUNT(gConfigs);
120 fBitmaps = new SkBitmap[fBitmapCount];
121 for (size_t i = 0; i < fBitmapCount; i++) {
122 make_image(&fBitmaps[i], gConfigs[i], i);
123
124 for (size_t j = 0; j < SK_ARRAY_COUNT(gExt); j++) {
125 SkString path;
126 make_name(&path, i, j);
127
128 // remove any previous run of this file
129 remove(path.c_str());
130
131 SkImageEncoder* codec = SkImageEncoder::Create(gTypes[j]);
reed@android.comb08eb2b2009-01-06 20:16:26 +0000132 if (!codec->encodeFile(path.c_str(), fBitmaps[i], 100)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000133 SkDebugf("------ failed to encode %s\n", path.c_str());
134 remove(path.c_str()); // remove any partial file
135 }
136 delete codec;
137 }
138 }
139 #else
140 fBitmaps = NULL;
141 fBitmapCount = 0;
142 #endif
143 }
144
145 virtual ~EncodeView() {
146 delete[] fBitmaps;
147 }
148
149protected:
150 // overrides from SkEventSink
151 virtual bool onQuery(SkEvent* evt) {
152 if (SampleCode::TitleQ(*evt)) {
153 SampleCode::TitleR(evt, "ImageEncoder");
154 return true;
155 }
156 return this->INHERITED::onQuery(evt);
157 }
158
159 void drawBG(SkCanvas* canvas) {
160 canvas->drawColor(0xFFDDDDDD);
161// canvas->drawColor(SK_ColorWHITE);
162 }
163
164 virtual void onDraw(SkCanvas* canvas) {
165 this->drawBG(canvas);
166
167 if (fBitmapCount == 0) {
168 return;
169 }
170
171 SkPaint paint;
172 if (false) {
173// SkColor colors[] = { 0xFE000000, SK_ColorWHITE };
174 SkColor colors[] = { SK_ColorRED, SK_ColorBLUE };
175 SkShader* shader = SkGradientShader::CreateSweep(SkIntToScalar(50), SkIntToScalar(50),
176 colors, NULL, 2);
177 paint.setShader(shader)->unref();
178
179 SkRect r;
180 r.set(0, 0, SkIntToScalar(100), SkIntToScalar(100));
181 canvas->drawRect(r, paint);
182
183 canvas->translate(SkIntToScalar(200), SkIntToScalar(200));
184 paint.setAntiAlias(true);
185 paint.setStyle(SkPaint::kStroke_Style);
186 paint.setStrokeWidth(SkIntToScalar(10));
187 canvas->drawOval(r, paint);
188 return;
189 }
190
191 paint.setAntiAlias(true);
192 paint.setTextAlign(SkPaint::kCenter_Align);
193
194 canvas->translate(SkIntToScalar(10), SkIntToScalar(20));
195
196 SkScalar x = 0, y = 0, maxX = 0;
197 const int SPACER = 10;
198
199 for (size_t i = 0; i < fBitmapCount; i++) {
200 canvas->drawText(gConfigLabels[i], strlen(gConfigLabels[i]),
201 x + SkIntToScalar(fBitmaps[i].width()) / 2, 0,
202 paint);
203 y = paint.getTextSize();
204
205 canvas->drawBitmap(fBitmaps[i], x, y);
206
207 SkScalar yy = y;
208 for (size_t j = 0; j < SK_ARRAY_COUNT(gExt); j++) {
209 yy += SkIntToScalar(fBitmaps[i].height() + 10);
210
211 SkBitmap bm;
212 SkString name;
213
214 make_name(&name, i, j);
215
216 SkImageDecoder::DecodeFile(name.c_str(), &bm);
217 canvas->drawBitmap(bm, x, yy);
218 }
219
220 x += SkIntToScalar(fBitmaps[i].width() + SPACER);
221 if (x > maxX) {
222 maxX = x;
223 }
224 }
225
226 y = (paint.getTextSize() + SkIntToScalar(fBitmaps[0].height())) * 3 / 2;
227 x = maxX + SkIntToScalar(10);
228 paint.setTextAlign(SkPaint::kLeft_Align);
229
230 for (size_t j = 0; j < SK_ARRAY_COUNT(gExt); j++) {
231 canvas->drawText(gExt[j], strlen(gExt[j]), x, y, paint);
232 y += SkIntToScalar(fBitmaps[0].height() + SPACER);
233 }
234 }
235
236 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) {
237 this->inval(NULL);
238 return this->INHERITED::onFindClickHandler(x, y);
239 }
240
241 virtual bool onClick(Click* click) {
242 return this->INHERITED::onClick(click);
243 }
244
245private:
246 typedef SkView INHERITED;
247};
248
249//////////////////////////////////////////////////////////////////////////////
250
251static SkView* MyFactory() { return new EncodeView; }
252static SkViewRegister reg(MyFactory);
253