blob: 48759c0b9e4faaa8495739ba67735de17dd75302 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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.com11344262009-07-08 20:09:23 +00008#include "SampleCode.h"
9#include "SkView.h"
10#include "SkCanvas.h"
11#include "SkImageDecoder.h"
12#include "SkStream.h"
13
14static 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
26class DecodeView : public SkView {
27public:
28 SkBitmap fBitmap[SK_ARRAY_COUNT(gRec)];
29
robertphillips@google.com622a1702012-07-31 19:23:02 +000030 DecodeView() {
reed@android.com11344262009-07-08 20:09:23 +000031 SkFILEStream stream("/skimages/index.png");
32 SkImageDecoder* codec = SkImageDecoder::Factory(&stream);
bsalomon@google.com5d561952011-04-01 18:43:22 +000033 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.com622a1702012-07-31 19:23:02 +000040 SkDELETE(codec);
reed@android.com11344262009-07-08 20:09:23 +000041 }
42 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000043
reed@android.com11344262009-07-08 20:09:23 +000044protected:
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.comae933ce2012-08-23 18:19:56 +000053
reed@android.com11344262009-07-08 20:09:23 +000054 void drawBG(SkCanvas* canvas) {
55 canvas->drawColor(0xFFDDDDDD);
56// canvas->drawColor(SK_ColorWHITE);
57 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000058
reed@android.com11344262009-07-08 20:09:23 +000059 virtual void onDraw(SkCanvas* canvas) {
60 this->drawBG(canvas);
rmistry@google.comae933ce2012-08-23 18:19:56 +000061
reed@android.com11344262009-07-08 20:09:23 +000062 canvas->translate(SkIntToScalar(10), SkIntToScalar(20));
rmistry@google.comae933ce2012-08-23 18:19:56 +000063
reed@android.com11344262009-07-08 20:09:23 +000064 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 }
69private:
70 typedef SkView INHERITED;
71};
72
73//////////////////////////////////////////////////////////////////////////////
74
75static SkView* MyFactory() { return new DecodeView; }
76static SkViewRegister reg(MyFactory);
77