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

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

Issue 155763004: add ways to peer into an image to get its pixels (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Created 6 years, 10 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 | Annotate | Revision Log
« no previous file with comments | « src/image/SkImage_Base.h ('k') | tests/SurfaceTest.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 "SkImagePriv.h" 9 #include "SkImagePriv.h"
10 #include "SkBitmap.h" 10 #include "SkBitmap.h"
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
48 return true; 48 return true;
49 } 49 }
50 50
51 static SkImage* NewEmpty(); 51 static SkImage* NewEmpty();
52 52
53 SkImage_Raster(const SkImageInfo&, SkData*, size_t rb); 53 SkImage_Raster(const SkImageInfo&, SkData*, size_t rb);
54 virtual ~SkImage_Raster(); 54 virtual ~SkImage_Raster();
55 55
56 virtual void onDraw(SkCanvas*, SkScalar, SkScalar, const SkPaint*) SK_OVERRI DE; 56 virtual void onDraw(SkCanvas*, SkScalar, SkScalar, const SkPaint*) SK_OVERRI DE;
57 virtual void onDrawRectToRect(SkCanvas*, const SkRect*, const SkRect&, const SkPaint*) SK_OVERRIDE; 57 virtual void onDrawRectToRect(SkCanvas*, const SkRect*, const SkRect&, const SkPaint*) SK_OVERRIDE;
58 virtual bool onReadPixels(SkBitmap*, const SkIRect&) const SK_OVERRIDE;
59 virtual const void* onPeekPixels(SkImageInfo*, size_t* /*rowBytes*/) const S K_OVERRIDE;
58 virtual bool getROPixels(SkBitmap*) const SK_OVERRIDE; 60 virtual bool getROPixels(SkBitmap*) const SK_OVERRIDE;
59 61
60 // exposed for SkSurface_Raster via SkNewImageFromPixelRef 62 // exposed for SkSurface_Raster via SkNewImageFromPixelRef
61 SkImage_Raster(const SkImageInfo&, SkPixelRef*, size_t rowBytes); 63 SkImage_Raster(const SkImageInfo&, SkPixelRef*, size_t rowBytes);
62 64
63 SkPixelRef* getPixelRef() const { return fBitmap.pixelRef(); } 65 SkPixelRef* getPixelRef() const { return fBitmap.pixelRef(); }
64 66
65 private: 67 private:
66 SkImage_Raster() : INHERITED(0, 0) {} 68 SkImage_Raster() : INHERITED(0, 0) {}
67 69
68 SkBitmap fBitmap; 70 SkBitmap fBitmap;
69 71
70 typedef SkImage_Base INHERITED; 72 typedef SkImage_Base INHERITED;
71 }; 73 };
72 74
73 /////////////////////////////////////////////////////////////////////////////// 75 ///////////////////////////////////////////////////////////////////////////////
74 76
75 SkImage* SkImage_Raster::NewEmpty() { 77 SkImage* SkImage_Raster::NewEmpty() {
76 // Returns lazily created singleton 78 // Returns lazily created singleton
77 static SkImage* gEmpty; 79 static SkImage* gEmpty;
78 if (NULL == gEmpty) { 80 if (NULL == gEmpty) {
79 gEmpty = SkNEW(SkImage_Raster); 81 gEmpty = SkNEW(SkImage_Raster);
80 } 82 }
81 gEmpty->ref(); 83 gEmpty->ref();
82 return gEmpty; 84 return gEmpty;
83 } 85 }
84 86
87 static void release_data(void* addr, void* context) {
88 SkData* data = static_cast<SkData*>(context);
89 data->unref();
90 }
91
85 SkImage_Raster::SkImage_Raster(const Info& info, SkData* data, size_t rowBytes) 92 SkImage_Raster::SkImage_Raster(const Info& info, SkData* data, size_t rowBytes)
86 : INHERITED(info.fWidth, info.fHeight) { 93 : INHERITED(info.fWidth, info.fHeight)
87 fBitmap.setConfig(info, rowBytes); 94 {
88 SkAutoTUnref<SkPixelRef> ref( 95 data->ref();
89 SkMallocPixelRef::NewWithData(info, rowBytes, NULL, data, 0)); 96 void* addr = const_cast<void*>(data->data());
90 fBitmap.setPixelRef(ref); 97
98 fBitmap.installPixels(info, addr, rowBytes, release_data, data);
91 fBitmap.setImmutable(); 99 fBitmap.setImmutable();
100 fBitmap.lockPixels();
92 } 101 }
93 102
94 SkImage_Raster::SkImage_Raster(const Info& info, SkPixelRef* pr, size_t rowBytes ) 103 SkImage_Raster::SkImage_Raster(const Info& info, SkPixelRef* pr, size_t rowBytes )
95 : INHERITED(info.fWidth, info.fHeight) 104 : INHERITED(info.fWidth, info.fHeight)
96 { 105 {
97 fBitmap.setConfig(info, rowBytes); 106 fBitmap.setConfig(info, rowBytes);
98 fBitmap.setPixelRef(pr); 107 fBitmap.setPixelRef(pr);
108 fBitmap.lockPixels();
99 } 109 }
100 110
101 SkImage_Raster::~SkImage_Raster() {} 111 SkImage_Raster::~SkImage_Raster() {}
102 112
103 void SkImage_Raster::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPa int* paint) { 113 void SkImage_Raster::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPa int* paint) {
104 canvas->drawBitmap(fBitmap, x, y, paint); 114 canvas->drawBitmap(fBitmap, x, y, paint);
105 } 115 }
106 116
107 void SkImage_Raster::onDrawRectToRect(SkCanvas* canvas, const SkRect* src, const SkRect& dst, const SkPaint* paint) { 117 void SkImage_Raster::onDrawRectToRect(SkCanvas* canvas, const SkRect* src,
118 const SkRect& dst, const SkPaint* paint) {
108 canvas->drawBitmapRectToRect(fBitmap, src, dst, paint); 119 canvas->drawBitmapRectToRect(fBitmap, src, dst, paint);
109 } 120 }
110 121
122 bool SkImage_Raster::onReadPixels(SkBitmap* dst, const SkIRect& subset) const {
123 if (dst->pixelRef()) {
124 return this->INHERITED::onReadPixels(dst, subset);
125 } else {
126 SkBitmap src;
127 if (!fBitmap.extractSubset(&src, subset)) {
128 return false;
129 }
130 return src.copyTo(dst, src.config());
131 }
132 }
133
134 const void* SkImage_Raster::onPeekPixels(SkImageInfo* infoPtr,
135 size_t* rowBytesPtr) const {
136 SkImageInfo info;
137 if (!fBitmap.asImageInfo(&info) || !fBitmap.getPixels()) {
138 return false;
139 }
140 *infoPtr = info;
141 *rowBytesPtr = fBitmap.rowBytes();
142 return fBitmap.getPixels();
143 }
144
111 bool SkImage_Raster::getROPixels(SkBitmap* dst) const { 145 bool SkImage_Raster::getROPixels(SkBitmap* dst) const {
112 *dst = fBitmap; 146 *dst = fBitmap;
113 return true; 147 return true;
114 } 148 }
115 149
116 /////////////////////////////////////////////////////////////////////////////// 150 ///////////////////////////////////////////////////////////////////////////////
117 151
118 SkImage* SkImage::NewRasterCopy(const SkImageInfo& info, const void* pixels, siz e_t rowBytes) { 152 SkImage* SkImage::NewRasterCopy(const SkImageInfo& info, const void* pixels, siz e_t rowBytes) {
119 if (!SkImage_Raster::ValidArgs(info, rowBytes)) { 153 if (!SkImage_Raster::ValidArgs(info, rowBytes)) {
120 return NULL; 154 return NULL;
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
155 } 189 }
156 190
157 SkImage* SkNewImageFromPixelRef(const SkImageInfo& info, SkPixelRef* pr, 191 SkImage* SkNewImageFromPixelRef(const SkImageInfo& info, SkPixelRef* pr,
158 size_t rowBytes) { 192 size_t rowBytes) {
159 return SkNEW_ARGS(SkImage_Raster, (info, pr, rowBytes)); 193 return SkNEW_ARGS(SkImage_Raster, (info, pr, rowBytes));
160 } 194 }
161 195
162 SkPixelRef* SkBitmapImageGetPixelRef(SkImage* image) { 196 SkPixelRef* SkBitmapImageGetPixelRef(SkImage* image) {
163 return ((SkImage_Raster*)image)->getPixelRef(); 197 return ((SkImage_Raster*)image)->getPixelRef();
164 } 198 }
OLDNEW
« no previous file with comments | « src/image/SkImage_Base.h ('k') | tests/SurfaceTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698