aboutsummaryrefslogtreecommitdiffhomepage
path: root/samplecode/SampleImageDir.cpp
blob: 922910ce32f3fc3f05d51db8706a5270eaaf5bc2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#include "SampleCode.h"
#include "SkView.h"
#include "SkCanvas.h"
#include "SkGradientShader.h"
#include "SkGraphics.h"
#include "SkImageDecoder.h"
#include "SkPath.h"
#include "SkPorterDuff.h"
#include "SkRegion.h"
#include "SkShader.h"
#include "SkUtils.h"
#include "SkXfermode.h"
#include "SkComposeShader.h"
#include "SkColorPriv.h"
#include "SkColorFilter.h"
#include "SkTime.h"
#include "SkTypeface.h"

#include "SkImageRef_GlobalPool.h"
#include "SkOSFile.h"
#include "SkStream.h"

#include "SkBlurDrawLooper.h"
#include "SkColorMatrixFilter.h"

static void drawmarshmallow(SkCanvas* canvas) {
    SkBitmap bitmap;
    SkPaint paint;
    SkRect r;
    SkMatrix m;

    SkImageDecoder::DecodeFile("/Users/reed/Downloads/3elfs.jpg", &bitmap);
    SkShader* s = SkShader::CreateBitmapShader(bitmap,
                                               SkShader::kRepeat_TileMode,
                                               SkShader::kRepeat_TileMode);
    paint.setShader(s)->unref();
    m.setTranslate(SkIntToScalar(250), SkIntToScalar(134));
    s->setLocalMatrix(m);

    r.set(SkIntToScalar(250),
          SkIntToScalar(134),
          SkIntToScalar(250 + 449),
          SkIntToScalar(134 + 701));
    paint.setFlags(2);

    canvas->drawRect(r, paint);
}

static void DrawRoundRect(SkCanvas& canvas) {
   bool ret = false;
   SkPaint  paint;
   SkBitmap bitmap;
   SkMatrix matrix;
   matrix.reset();

   bitmap.setConfig(SkBitmap::kARGB_8888_Config, 1370, 812);
   bitmap.allocPixels();
#if 0
    SkCanvas canvas;
    canvas.setBitmapDevice(bitmap);
#endif

   // set up clipper
   SkRect skclip;
   skclip.set(SkIntToFixed(284), SkIntToFixed(40), SkIntToFixed(1370), SkIntToFixed(708));

//   ret = canvas.clipRect(skclip);
//   SkASSERT(ret);

   matrix.set(SkMatrix::kMTransX, SkFloatToFixed(-1153.28));
   matrix.set(SkMatrix::kMTransY, SkFloatToFixed(1180.50));

   matrix.set(SkMatrix::kMScaleX, SkFloatToFixed(0.177171));
   matrix.set(SkMatrix::kMScaleY, SkFloatToFixed(0.177043));

   matrix.set(SkMatrix::kMSkewX, SkFloatToFixed(0.126968));
   matrix.set(SkMatrix::kMSkewY, SkFloatToFixed(-0.126876));

   matrix.set(SkMatrix::kMPersp0, SkFloatToFixed(0.0));
   matrix.set(SkMatrix::kMPersp1, SkFloatToFixed(0.0));

   ret = canvas.concat(matrix);

   paint.setAntiAlias(true);
   paint.setColor(0xb2202020);
   paint.setStyle(SkPaint::kStroke_Style);
   paint.setStrokeWidth(SkFloatToFixed(68.13));

   SkRect r;
   r.set(SkFloatToFixed(-313.714417), SkFloatToFixed(-4.826389), SkFloatToFixed(18014.447266), SkFloatToFixed(1858.154541));
   canvas.drawRoundRect(r, SkFloatToFixed(91.756363), SkFloatToFixed(91.756363), paint);
}

static bool SetImageRef(SkBitmap* bitmap, SkStream* stream,
                        SkBitmap::Config pref, const char name[] = NULL) {
#if 0
    // test buffer streams
    SkStream* str = new SkBufferStream(stream, 717);
    stream->unref();
    stream = str;
#endif

    SkImageRef* ref = new SkImageRef_GlobalPool(stream, pref, 1);
    ref->setURI(name);
    if (!ref->getInfo(bitmap)) {
        delete ref;
        return false;
    }
    bitmap->setPixelRef(ref)->unref();
    return true;
}

//#define SPECIFIC_IMAGE  "/skimages/72.jpg"
#define SPECIFIC_IMAGE  "/Users/reed/Downloads/3elfs.jpg"

#define IMAGE_DIR       "/skimages/"
#define IMAGE_SUFFIX    ".gif"

class ImageDirView : public SkView {
public:
    SkBitmap*   fBitmaps;
    SkString*   fStrings;
    int         fBitmapCount;
    int         fCurrIndex;
    SkScalar    fSaturation;
    SkScalar    fAngle;

	ImageDirView() {
        SkImageRef_GlobalPool::SetRAMBudget(320 * 1024);
        
#ifdef SPECIFIC_IMAGE
        fBitmaps = new SkBitmap[3];
        fStrings = new SkString[3];
        fBitmapCount = 3;
        const SkBitmap::Config configs[] = {
            SkBitmap::kARGB_8888_Config,
            SkBitmap::kRGB_565_Config,
            SkBitmap::kARGB_4444_Config
        };
        for (int i = 0; i < fBitmapCount; i++) {
#if 1
            SkStream* stream = new SkFILEStream(SPECIFIC_IMAGE);
            SetImageRef(&fBitmaps[i], stream, configs[i], SPECIFIC_IMAGE);
            stream->unref();
#else
            SkImageDecoder::DecodeFile(SPECIFIC_IMAGE, &fBitmaps[i]);
#endif
        }
#else
        int i, N = 0;
        SkOSFile::Iter  iter(IMAGE_DIR, IMAGE_SUFFIX);
        SkString    name;
        while (iter.next(&name)) {
            N += 1;
        }
        fBitmaps = new SkBitmap[N];
        fStrings = new SkString[N];
        iter.reset(IMAGE_DIR, IMAGE_SUFFIX);
        for (i = 0; i < N; i++) {
            iter.next(&name);
            SkString path(IMAGE_DIR);
            path.append(name);
            SkStream* stream = new SkFILEStream(path.c_str());
            
            SetImageRef(&fBitmaps[i], stream, SkBitmap::kNo_Config,
                        name.c_str());
            stream->unref();
            fStrings[i] = name;
        }
        fBitmapCount = N;
#endif
        fCurrIndex = 0;
        fDX = fDY = 0;
        
        fSaturation = SK_Scalar1;
        fAngle = 0;
        
        fScale = SK_Scalar1;
    }
    
    virtual ~ImageDirView() {
        delete[] fBitmaps;
        delete[] fStrings;

        SkImageRef_GlobalPool::DumpPool();
    }
    
protected:
    // overrides from SkEventSink
    virtual bool onQuery(SkEvent* evt) {
        if (SampleCode::TitleQ(*evt)) {
            SkString str("ImageDir: ");
#ifdef SPECIFIC_IMAGE
            str.append(SPECIFIC_IMAGE);
#else
            str.append(IMAGE_DIR);
#endif
            SampleCode::TitleR(evt, str.c_str());
            return true;
        }
        return this->INHERITED::onQuery(evt);
    }
    
    void drawBG(SkCanvas* canvas) {
//        canvas->drawColor(0xFFDDDDDD);
        canvas->drawColor(SK_ColorGRAY);
        canvas->drawColor(SK_ColorWHITE);
    }
    
    SkScalar fScale;
    virtual void onDraw(SkCanvas* canvas) {
        this->drawBG(canvas);
        
        if (true) {
            canvas->scale(SkIntToScalar(2), SkIntToScalar(2));
            drawmarshmallow(canvas);
            return;
        }
        
        if (false) {
            SkPaint p;
            p.setStyle(SkPaint::kStroke_Style);
            p.setStrokeWidth(SkIntToScalar(4));
            canvas->drawCircle(SkIntToScalar(100), SkIntToScalar(100), SkIntToScalar(50), p);
            p.setAntiAlias(true);
            canvas->drawCircle(SkIntToScalar(300), SkIntToScalar(100), SkIntToScalar(50), p);
        }
        if (false) {
            SkScalar cx = this->width()/2;
            SkScalar cy = this->height()/2;
            canvas->translate(cx, cy);
            canvas->scale(fScale, fScale);
            canvas->translate(-cx, -cy);
            DrawRoundRect(*canvas);
            return;
        }
        
        SkScalar scale = SK_Scalar1 * 999/1000;
//        scale = SK_Scalar1/2;
        
        canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
   //     canvas->scale(scale, scale);
        
        SkScalar x = SkIntToScalar(32), y = SkIntToScalar(32);
        SkPaint paint;
        
    //    x += fDX;
    //    y += fDY;
    
//        paint.setLooper(new SkBlurDrawLooper(SkIntToScalar(12), 0, 0, 0xDD000000))->unref();
        
#if 0
        for (int i = 0; i < fBitmapCount; i++) {
            SkPaint p;
            
#if 1
            const SkScalar cm[] = {
                SkIntToScalar(2), 0, 0, 0, SkIntToScalar(-255),
                0, SkIntToScalar(2), 0, 0, SkIntToScalar(-255),
                0, 0, SkIntToScalar(2), 0, SkIntToScalar(-255),
                0, 0, 0, SkIntToScalar(1), 0
            };
            SkColorFilter* cf = new SkColorMatrixFilter(cm);
            p.setColorFilter(cf)->unref();
#endif
            
            canvas->drawBitmap(fBitmaps[i], x, y, &p);
            x += SkIntToScalar(fBitmaps[i].width() + 10);
        }
        return;
#endif

        canvas->drawBitmap(fBitmaps[fCurrIndex], x, y, &paint);
#ifndef SPECIFIC_IMAGE
        if (true) {
            fCurrIndex += 1;
            if (fCurrIndex >= fBitmapCount) {
                fCurrIndex = 0;
            }
            this->inval(NULL);
        }
#endif
    }
    
    virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) {
        if (true) {
            fCurrIndex += 1;
            if (fCurrIndex >= fBitmapCount)
                fCurrIndex = 0;
            this->inval(NULL);
        }
        return new Click(this);
    }
    
    virtual bool onClick(Click* click)  {
        SkScalar center = this->width()/2;
        fSaturation = SkScalarDiv(click->fCurr.fX - center, center/2);
        center = this->height()/2;
        fAngle = SkScalarDiv(click->fCurr.fY - center, center) * 180;

        fDX += click->fCurr.fX - click->fPrev.fX;
        fDY += click->fCurr.fY - click->fPrev.fY;
        
        fScale = SkScalarDiv(click->fCurr.fX, this->width());

        this->inval(NULL);
        return true;
        return this->INHERITED::onClick(click);
    }
    
private:
    SkScalar fDX, fDY;
    typedef SkView INHERITED;
};

//////////////////////////////////////////////////////////////////////////////

static SkView* MyFactory() { return new ImageDirView; }
static SkViewRegister reg(MyFactory);