Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(171)

Side by Side Diff: src/image/SkImage_Raster.cpp

Issue 1377473002: SkImage::NewFromBitmap works with Index8 (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: 2015-09-28 (Monday) 14:48:00 EDT Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | tests/ImageTest.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2012 Google Inc. 2 * Copyright 2012 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 "SkImage_Base.h" 8 #include "SkImage_Base.h"
9 #include "SkBitmap.h" 9 #include "SkBitmap.h"
10 #include "SkCanvas.h" 10 #include "SkCanvas.h"
11 #include "SkColorTable.h" 11 #include "SkColorTable.h"
12 #include "SkData.h" 12 #include "SkData.h"
13 #include "SkImagePriv.h" 13 #include "SkImagePriv.h"
14 #include "SkPixelRef.h" 14 #include "SkPixelRef.h"
15 #include "SkSurface.h" 15 #include "SkSurface.h"
16 16
17 #if SK_SUPPORT_GPU 17 #if SK_SUPPORT_GPU
18 #include "GrContext.h" 18 #include "GrContext.h"
19 #include "SkGr.h" 19 #include "SkGr.h"
20 #include "SkGrPriv.h" 20 #include "SkGrPriv.h"
21 #endif 21 #endif
22 22
23 class SkImage_Raster : public SkImage_Base { 23 class SkImage_Raster : public SkImage_Base {
24 public: 24 public:
25 static bool ValidArgs(const Info& info, size_t rowBytes, SkColorTable* ctabl e, 25 static bool ValidArgs(const Info& info, size_t rowBytes, bool hasColorTable,
26 size_t* minSize) { 26 size_t* minSize) {
27 const int maxDimension = SK_MaxS32 >> 2; 27 const int maxDimension = SK_MaxS32 >> 2;
28 28
29 if (info.width() <= 0 || info.height() <= 0) { 29 if (info.width() <= 0 || info.height() <= 0) {
30 return false; 30 return false;
31 } 31 }
32 if (info.width() > maxDimension || info.height() > maxDimension) { 32 if (info.width() > maxDimension || info.height() > maxDimension) {
33 return false; 33 return false;
34 } 34 }
35 if ((unsigned)info.colorType() > (unsigned)kLastEnum_SkColorType) { 35 if ((unsigned)info.colorType() > (unsigned)kLastEnum_SkColorType) {
36 return false; 36 return false;
37 } 37 }
38 if ((unsigned)info.alphaType() > (unsigned)kLastEnum_SkAlphaType) { 38 if ((unsigned)info.alphaType() > (unsigned)kLastEnum_SkAlphaType) {
39 return false; 39 return false;
40 } 40 }
41 41
42 if (kUnknown_SkColorType == info.colorType()) { 42 if (kUnknown_SkColorType == info.colorType()) {
43 return false; 43 return false;
44 } 44 }
45 45
46 const bool needsCT = kIndex_8_SkColorType == info.colorType(); 46 const bool needsCT = kIndex_8_SkColorType == info.colorType();
47 const bool hasCT = nullptr != ctable; 47 if (needsCT != hasColorTable) {
48 if (needsCT != hasCT) {
49 return false; 48 return false;
50 } 49 }
51 50
52 if (rowBytes < SkImageMinRowBytes(info)) { 51 if (rowBytes < SkImageMinRowBytes(info)) {
53 return false; 52 return false;
54 } 53 }
55 54
56 size_t size = info.getSafeSize(rowBytes); 55 size_t size = info.getSafeSize(rowBytes);
57 if (0 == size) { 56 if (0 == size) {
58 return false; 57 return false;
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after
220 surface->getCanvas()->drawImage(this, SkIntToScalar(-subset.x()), SkIntToSca lar(-subset.y()), 219 surface->getCanvas()->drawImage(this, SkIntToScalar(-subset.x()), SkIntToSca lar(-subset.y()),
221 nullptr); 220 nullptr);
222 return surface->newImageSnapshot(); 221 return surface->newImageSnapshot();
223 } 222 }
224 223
225 /////////////////////////////////////////////////////////////////////////////// 224 ///////////////////////////////////////////////////////////////////////////////
226 225
227 SkImage* SkImage::NewRasterCopy(const SkImageInfo& info, const void* pixels, siz e_t rowBytes, 226 SkImage* SkImage::NewRasterCopy(const SkImageInfo& info, const void* pixels, siz e_t rowBytes,
228 SkColorTable* ctable) { 227 SkColorTable* ctable) {
229 size_t size; 228 size_t size;
230 if (!SkImage_Raster::ValidArgs(info, rowBytes, ctable, &size) || !pixels) { 229 if (!SkImage_Raster::ValidArgs(info, rowBytes, ctable != nullptr, &size) || !pixels) {
231 return nullptr; 230 return nullptr;
232 } 231 }
233 232
234 // Here we actually make a copy of the caller's pixel data 233 // Here we actually make a copy of the caller's pixel data
235 SkAutoDataUnref data(SkData::NewWithCopy(pixels, size)); 234 SkAutoDataUnref data(SkData::NewWithCopy(pixels, size));
236 return new SkImage_Raster(info, data, rowBytes, ctable, nullptr); 235 return new SkImage_Raster(info, data, rowBytes, ctable, nullptr);
237 } 236 }
238 237
239 238
240 SkImage* SkImage::NewRasterData(const SkImageInfo& info, SkData* data, size_t ro wBytes) { 239 SkImage* SkImage::NewRasterData(const SkImageInfo& info, SkData* data, size_t ro wBytes) {
241 size_t size; 240 size_t size;
242 if (!SkImage_Raster::ValidArgs(info, rowBytes, nullptr, &size) || !data) { 241 if (!SkImage_Raster::ValidArgs(info, rowBytes, false, &size) || !data) {
243 return nullptr; 242 return nullptr;
244 } 243 }
245 244
246 // did they give us enough data? 245 // did they give us enough data?
247 if (data->size() < size) { 246 if (data->size() < size) {
248 return nullptr; 247 return nullptr;
249 } 248 }
250 249
251 SkColorTable* ctable = nullptr; 250 SkColorTable* ctable = nullptr;
252 return new SkImage_Raster(info, data, rowBytes, ctable, nullptr); 251 return new SkImage_Raster(info, data, rowBytes, ctable, nullptr);
253 } 252 }
254 253
255 SkImage* SkImage::NewFromRaster(const SkImageInfo& info, const void* pixels, siz e_t rowBytes, 254 SkImage* SkImage::NewFromRaster(const SkImageInfo& info, const void* pixels, siz e_t rowBytes,
256 RasterReleaseProc proc, ReleaseContext ctx) { 255 RasterReleaseProc proc, ReleaseContext ctx) {
257 size_t size; 256 size_t size;
258 if (!SkImage_Raster::ValidArgs(info, rowBytes, nullptr, &size) || !pixels) { 257 if (!SkImage_Raster::ValidArgs(info, rowBytes, false, &size) || !pixels) {
259 return nullptr; 258 return nullptr;
260 } 259 }
261 260
262 SkColorTable* ctable = nullptr; 261 SkColorTable* ctable = nullptr;
263 SkAutoDataUnref data(SkData::NewWithProc(pixels, size, proc, ctx)); 262 SkAutoDataUnref data(SkData::NewWithProc(pixels, size, proc, ctx));
264 return new SkImage_Raster(info, data, rowBytes, ctable, nullptr); 263 return new SkImage_Raster(info, data, rowBytes, ctable, nullptr);
265 } 264 }
266 265
267 SkImage* SkNewImageFromPixelRef(const SkImageInfo& info, SkPixelRef* pr, 266 SkImage* SkNewImageFromPixelRef(const SkImageInfo& info, SkPixelRef* pr,
268 const SkIPoint& pixelRefOrigin, size_t rowBytes, 267 const SkIPoint& pixelRefOrigin, size_t rowBytes,
269 const SkSurfaceProps* props) { 268 const SkSurfaceProps* props) {
270 if (!SkImage_Raster::ValidArgs(info, rowBytes, nullptr, nullptr)) { 269 if (!SkImage_Raster::ValidArgs(info, rowBytes, false, nullptr)) {
271 return nullptr; 270 return nullptr;
272 } 271 }
273 return new SkImage_Raster(info, pr, pixelRefOrigin, rowBytes, props); 272 return new SkImage_Raster(info, pr, pixelRefOrigin, rowBytes, props);
274 } 273 }
275 274
276 SkImage* SkNewImageFromRasterBitmap(const SkBitmap& bm, const SkSurfaceProps* pr ops, 275 SkImage* SkNewImageFromRasterBitmap(const SkBitmap& bm, const SkSurfaceProps* pr ops,
277 ForceCopyMode forceCopy) { 276 ForceCopyMode forceCopy) {
278 SkASSERT(nullptr == bm.getTexture()); 277 SkASSERT(nullptr == bm.getTexture());
279 278
280 if (!SkImage_Raster::ValidArgs(bm.info(), bm.rowBytes(), nullptr, nullptr)) { 279 bool hasColorTable = false;
280 if (kIndex_8_SkColorType == bm.colorType()) {
281 SkAutoLockPixels autoLockPixels(bm);
282 hasColorTable = bm.getColorTable() != nullptr;
283 }
284
285 if (!SkImage_Raster::ValidArgs(bm.info(), bm.rowBytes(), hasColorTable, null ptr)) {
281 return nullptr; 286 return nullptr;
282 } 287 }
283 288
284 SkImage* image = nullptr; 289 SkImage* image = nullptr;
285 if (kYes_ForceCopyMode == forceCopy || !bm.isImmutable()) { 290 if (kYes_ForceCopyMode == forceCopy || !bm.isImmutable()) {
286 SkBitmap tmp(bm); 291 SkBitmap tmp(bm);
287 tmp.lockPixels(); 292 tmp.lockPixels();
288 if (tmp.getPixels()) { 293 if (tmp.getPixels()) {
289 image = SkImage::NewRasterCopy(tmp.info(), tmp.getPixels(), tmp.rowB ytes(), 294 image = SkImage::NewRasterCopy(tmp.info(), tmp.getPixels(), tmp.rowB ytes(),
290 tmp.getColorTable()); 295 tmp.getColorTable());
(...skipping 24 matching lines...) Expand all
315 // pixelref since the caller might call setImmutable() themselves 320 // pixelref since the caller might call setImmutable() themselves
316 // (thus changing our state). 321 // (thus changing our state).
317 if (fBitmap.isImmutable()) { 322 if (fBitmap.isImmutable()) {
318 bitmap->setInfo(fBitmap.info(), fBitmap.rowBytes()); 323 bitmap->setInfo(fBitmap.info(), fBitmap.rowBytes());
319 bitmap->setPixelRef(fBitmap.pixelRef(), fBitmap.pixelRefOrigin()); 324 bitmap->setPixelRef(fBitmap.pixelRef(), fBitmap.pixelRefOrigin());
320 return true; 325 return true;
321 } 326 }
322 } 327 }
323 return this->INHERITED::onAsLegacyBitmap(bitmap, mode); 328 return this->INHERITED::onAsLegacyBitmap(bitmap, mode);
324 } 329 }
OLDNEW
« no previous file with comments | « no previous file | tests/ImageTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698