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