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