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