OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "gm.h" | 8 #include "gm.h" |
9 #include "SkCanvas.h" | 9 #include "SkCanvas.h" |
10 #include "SkImage.h" | 10 #include "SkImage.h" |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 ////////////////////////////////////////////////////////////////////////////////
/////////////////// | 101 ////////////////////////////////////////////////////////////////////////////////
/////////////////// |
102 | 102 |
103 static SkImageGenerator* make_pic_generator(GrContext*, SkPicture* pic) { | 103 static SkImageGenerator* make_pic_generator(GrContext*, SkPicture* pic) { |
104 SkMatrix matrix; | 104 SkMatrix matrix; |
105 matrix.setTranslate(-100, -100); | 105 matrix.setTranslate(-100, -100); |
106 return SkImageGenerator::NewFromPicture(SkISize::Make(100, 100), pic, &matri
x, nullptr); | 106 return SkImageGenerator::NewFromPicture(SkISize::Make(100, 100), pic, &matri
x, nullptr); |
107 } | 107 } |
108 | 108 |
109 class RasterGenerator : public SkImageGenerator { | 109 class RasterGenerator : public SkImageGenerator { |
110 public: | 110 public: |
111 RasterGenerator(const SkBitmap& bm) : SkImageGenerator(bm.info()), fBM(bm) {
} | 111 RasterGenerator(const SkBitmap& bm) : SkImageGenerator(bm.info()), fBM(bm) { |
| 112 fBM.lockPixels(); |
| 113 } |
112 protected: | 114 protected: |
113 bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, | 115 bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, |
114 SkPMColor*, int*) override { | 116 SkPMColor* ctable, int* ctableCount) override { |
115 return fBM.readPixels(info, pixels, rowBytes, 0, 0); | 117 SkASSERT(fBM.width() == info.width()); |
| 118 SkASSERT(fBM.height() == info.height()); |
| 119 |
| 120 if (info.colorType() == kIndex_8_SkColorType) { |
| 121 if (SkColorTable* ct = fBM.getColorTable()) { |
| 122 if (ctable) { |
| 123 memcpy(ctable, ct->readColors(), ct->count() * sizeof(SkPMCo
lor)); |
| 124 } |
| 125 if (ctableCount) { |
| 126 *ctableCount = ct->count(); |
| 127 } |
| 128 |
| 129 for (int y = 0; y < info.height(); ++y) { |
| 130 memcpy(pixels, fBM.getAddr8(0, y), fBM.width()); |
| 131 pixels = (char*)pixels + rowBytes; |
| 132 } |
| 133 return true; |
| 134 } else { |
| 135 return false; |
| 136 } |
| 137 } else { |
| 138 return fBM.readPixels(info, pixels, rowBytes, 0, 0); |
| 139 } |
116 } | 140 } |
117 private: | 141 private: |
118 SkBitmap fBM; | 142 SkBitmap fBM; |
119 }; | 143 }; |
120 static SkImageGenerator* make_ras_generator(GrContext*, SkPicture* pic) { | 144 static SkImageGenerator* make_ras_generator(GrContext*, SkPicture* pic) { |
121 SkBitmap bm; | 145 SkBitmap bm; |
122 bm.allocN32Pixels(100, 100); | 146 bm.allocN32Pixels(100, 100); |
123 SkCanvas canvas(bm); | 147 SkCanvas canvas(bm); |
124 canvas.clear(0); | 148 canvas.clear(0); |
125 canvas.translate(-100, -100); | 149 canvas.translate(-100, -100); |
126 canvas.drawPicture(pic); | 150 canvas.drawPicture(pic); |
127 return new RasterGenerator(bm); | 151 return new RasterGenerator(bm); |
128 } | 152 } |
129 | 153 |
| 154 // so we can create a color-table |
| 155 static int find_closest(SkPMColor c, const SkPMColor table[], int count) { |
| 156 const int cr = SkGetPackedR32(c); |
| 157 const int cg = SkGetPackedG32(c); |
| 158 const int cb = SkGetPackedB32(c); |
| 159 |
| 160 int minDist = 999999999; |
| 161 int index = 0; |
| 162 for (int i = 0; i < count; ++i) { |
| 163 int dr = SkAbs32((int)SkGetPackedR32(table[i]) - cr); |
| 164 int dg = SkAbs32((int)SkGetPackedG32(table[i]) - cg); |
| 165 int db = SkAbs32((int)SkGetPackedB32(table[i]) - cb); |
| 166 int dist = dr + dg + db; |
| 167 if (dist < minDist) { |
| 168 minDist = dist; |
| 169 index = i; |
| 170 } |
| 171 } |
| 172 return index; |
| 173 } |
| 174 |
| 175 static SkImageGenerator* make_ctable_generator(GrContext*, SkPicture* pic) { |
| 176 SkBitmap bm; |
| 177 bm.allocN32Pixels(100, 100); |
| 178 SkCanvas canvas(bm); |
| 179 canvas.clear(0); |
| 180 canvas.translate(-100, -100); |
| 181 canvas.drawPicture(pic); |
| 182 |
| 183 const SkPMColor colors[] = { |
| 184 SkPreMultiplyColor(SK_ColorRED), |
| 185 SkPreMultiplyColor(0), |
| 186 SkPreMultiplyColor(SK_ColorBLUE), |
| 187 }; |
| 188 const int count = SK_ARRAY_COUNT(colors); |
| 189 SkImageInfo info = SkImageInfo::Make(100, 100, kIndex_8_SkColorType, kPremul
_SkAlphaType); |
| 190 |
| 191 SkBitmap bm2; |
| 192 SkAutoTUnref<SkColorTable> ct(new SkColorTable(colors, count)); |
| 193 bm2.allocPixels(info, nullptr, ct); |
| 194 for (int y = 0; y < info.height(); ++y) { |
| 195 for (int x = 0; x < info.width(); ++x) { |
| 196 *bm2.getAddr8(x, y) = find_closest(*bm.getAddr32(x, y), colors, coun
t); |
| 197 } |
| 198 } |
| 199 return new RasterGenerator(bm2); |
| 200 } |
| 201 |
130 class EmptyGenerator : public SkImageGenerator { | 202 class EmptyGenerator : public SkImageGenerator { |
131 public: | 203 public: |
132 EmptyGenerator(const SkImageInfo& info) : SkImageGenerator(info) {} | 204 EmptyGenerator(const SkImageInfo& info) : SkImageGenerator(info) {} |
133 }; | 205 }; |
134 | 206 |
135 #if SK_SUPPORT_GPU | 207 #if SK_SUPPORT_GPU |
136 class TextureGenerator : public SkImageGenerator { | 208 class TextureGenerator : public SkImageGenerator { |
137 public: | 209 public: |
138 TextureGenerator(GrContext* ctx, const SkImageInfo& info, SkPicture* pic) | 210 TextureGenerator(GrContext* ctx, const SkImageInfo& info, SkPicture* pic) |
139 : SkImageGenerator(info) | 211 : SkImageGenerator(info) |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
290 canvas->scale(2, 2); | 362 canvas->scale(2, 2); |
291 this->drawSet(canvas); | 363 this->drawSet(canvas); |
292 canvas->restore(); | 364 canvas->restore(); |
293 } | 365 } |
294 | 366 |
295 private: | 367 private: |
296 typedef skiagm::GM INHERITED; | 368 typedef skiagm::GM INHERITED; |
297 }; | 369 }; |
298 DEF_GM( return new ImageCacheratorGM("picture", make_pic_generator); ) | 370 DEF_GM( return new ImageCacheratorGM("picture", make_pic_generator); ) |
299 DEF_GM( return new ImageCacheratorGM("raster", make_ras_generator); ) | 371 DEF_GM( return new ImageCacheratorGM("raster", make_ras_generator); ) |
| 372 DEF_GM( return new ImageCacheratorGM("ctable", make_ctable_generator); ) |
300 #if SK_SUPPORT_GPU | 373 #if SK_SUPPORT_GPU |
301 DEF_GM( return new ImageCacheratorGM("texture", make_tex_generator); ) | 374 DEF_GM( return new ImageCacheratorGM("texture", make_tex_generator); ) |
302 #endif | 375 #endif |
303 | 376 |
304 | 377 |
305 | 378 |
OLD | NEW |