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