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 | 1134426 | 2009-07-08 20:09:23 +0000 | [diff] [blame] | 8 | #include "SampleCode.h" |
| 9 | #include "SkView.h" |
| 10 | #include "SkCanvas.h" |
| 11 | #include "SkImageDecoder.h" |
| 12 | #include "SkStream.h" |
| 13 | |
| 14 | static const struct { |
| 15 | SkBitmap::Config fPrefConfig; |
| 16 | bool fDither; |
| 17 | } gRec[] = { |
| 18 | { SkBitmap::kIndex8_Config, false }, |
| 19 | { SkBitmap::kARGB_8888_Config, false }, |
| 20 | { SkBitmap::kARGB_4444_Config, false }, |
| 21 | { SkBitmap::kARGB_4444_Config, true }, |
| 22 | { SkBitmap::kRGB_565_Config, false }, |
| 23 | { SkBitmap::kRGB_565_Config, true }, |
| 24 | }; |
| 25 | |
| 26 | class DecodeView : public SkView { |
| 27 | public: |
| 28 | SkBitmap fBitmap[SK_ARRAY_COUNT(gRec)]; |
| 29 | |
robertphillips@google.com | 622a170 | 2012-07-31 19:23:02 +0000 | [diff] [blame] | 30 | DecodeView() { |
reed@android.com | 1134426 | 2009-07-08 20:09:23 +0000 | [diff] [blame] | 31 | SkFILEStream stream("/skimages/index.png"); |
| 32 | SkImageDecoder* codec = SkImageDecoder::Factory(&stream); |
bsalomon@google.com | 5d56195 | 2011-04-01 18:43:22 +0000 | [diff] [blame] | 33 | if (codec) { |
| 34 | for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) { |
| 35 | stream.rewind(); |
| 36 | codec->setDitherImage(gRec[i].fDither); |
| 37 | codec->decode(&stream, &fBitmap[i], gRec[i].fPrefConfig, |
| 38 | SkImageDecoder::kDecodePixels_Mode); |
| 39 | } |
robertphillips@google.com | 622a170 | 2012-07-31 19:23:02 +0000 | [diff] [blame] | 40 | SkDELETE(codec); |
reed@android.com | 1134426 | 2009-07-08 20:09:23 +0000 | [diff] [blame] | 41 | } |
| 42 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 43 | |
reed@android.com | 1134426 | 2009-07-08 20:09:23 +0000 | [diff] [blame] | 44 | protected: |
| 45 | // overrides from SkEventSink |
| 46 | virtual bool onQuery(SkEvent* evt) { |
| 47 | if (SampleCode::TitleQ(*evt)) { |
| 48 | SampleCode::TitleR(evt, "ImageDecoder"); |
| 49 | return true; |
| 50 | } |
| 51 | return this->INHERITED::onQuery(evt); |
| 52 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 53 | |
reed@android.com | 1134426 | 2009-07-08 20:09:23 +0000 | [diff] [blame] | 54 | void drawBG(SkCanvas* canvas) { |
| 55 | canvas->drawColor(0xFFDDDDDD); |
| 56 | // canvas->drawColor(SK_ColorWHITE); |
| 57 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 58 | |
reed@android.com | 1134426 | 2009-07-08 20:09:23 +0000 | [diff] [blame] | 59 | virtual void onDraw(SkCanvas* canvas) { |
| 60 | this->drawBG(canvas); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 61 | |
reed@android.com | 1134426 | 2009-07-08 20:09:23 +0000 | [diff] [blame] | 62 | canvas->translate(SkIntToScalar(10), SkIntToScalar(20)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 63 | |
reed@android.com | 1134426 | 2009-07-08 20:09:23 +0000 | [diff] [blame] | 64 | for (size_t i = 0; i < SK_ARRAY_COUNT(fBitmap); i++) { |
| 65 | canvas->drawBitmap(fBitmap[i], 0, 0); |
| 66 | canvas->translate(SkIntToScalar(fBitmap[i].width()), 0); |
| 67 | } |
| 68 | } |
| 69 | private: |
| 70 | typedef SkView INHERITED; |
| 71 | }; |
| 72 | |
| 73 | ////////////////////////////////////////////////////////////////////////////// |
| 74 | |
| 75 | static SkView* MyFactory() { return new DecodeView; } |
| 76 | static SkViewRegister reg(MyFactory); |
| 77 | |