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