blob: b09b2ebef60e66766fdf46beeb533d226b4abefe [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.com9dc54652009-07-07 21:18:10 +0000131 if (NULL == codec ||
132 !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