/src/imagemagick/Magick++/lib/Pixels.cpp
Line | Count | Source |
1 | | // This may look like C code, but it is really -*- C++ -*- |
2 | | // |
3 | | // Copyright Bob Friesenhahn, 1999, 2000, 2001, 2002, 2003 |
4 | | // |
5 | | // Copyright @ 2013 ImageMagick Studio LLC, a non-profit organization |
6 | | // dedicated to making software imaging solutions freely available. |
7 | | // |
8 | | // Pixels Implementation |
9 | | // |
10 | | |
11 | | #define MAGICKCORE_IMPLEMENTATION 1 |
12 | | #define MAGICK_PLUSPLUS_IMPLEMENTATION 1 |
13 | | |
14 | | #include <cstring> |
15 | | #include "Magick++/Include.h" |
16 | | #include <string> // This is here to compile with Visual C++ |
17 | | #include "Magick++/Thread.h" |
18 | | #include "Magick++/Exception.h" |
19 | | #include "Magick++/Pixels.h" |
20 | | |
21 | | Magick::Pixels::Pixels(Magick::Image &image_) |
22 | 0 | : _image(image_), |
23 | 0 | _x(0), |
24 | 0 | _y(0), |
25 | 0 | _columns(0), |
26 | 0 | _rows(0) |
27 | 0 | { |
28 | 0 | GetPPException; |
29 | 0 | _view=AcquireVirtualCacheView(image_.image(),exceptionInfo), |
30 | 0 | ThrowPPException(image_.quiet()); |
31 | 0 | } |
32 | | |
33 | | Magick::Pixels::~Pixels(void) |
34 | 0 | { |
35 | 0 | if (_view) |
36 | 0 | _view=DestroyCacheView(_view); |
37 | 0 | } |
38 | | |
39 | | Magick::Quantum* Magick::Pixels::get(const ssize_t x_,const ssize_t y_, |
40 | | const size_t columns_,const size_t rows_) |
41 | 0 | { |
42 | 0 | _x=x_; |
43 | 0 | _y=y_; |
44 | 0 | _columns=columns_; |
45 | 0 | _rows=rows_; |
46 | |
|
47 | 0 | GetPPException; |
48 | 0 | Quantum* pixels=GetCacheViewAuthenticPixels(_view,x_,y_,columns_,rows_, |
49 | 0 | exceptionInfo); |
50 | 0 | ThrowPPException(_image.quiet()); |
51 | |
|
52 | 0 | return pixels; |
53 | 0 | } |
54 | | |
55 | | const Magick::Quantum* Magick::Pixels::getConst(const ssize_t x_, |
56 | | const ssize_t y_,const size_t columns_,const size_t rows_) |
57 | 0 | { |
58 | 0 | _x=x_; |
59 | 0 | _y=y_; |
60 | 0 | _columns=columns_; |
61 | 0 | _rows=rows_; |
62 | |
|
63 | 0 | GetPPException; |
64 | 0 | const Quantum* pixels=GetCacheViewVirtualPixels(_view,x_,y_,columns_,rows_, |
65 | 0 | exceptionInfo); |
66 | 0 | ThrowPPException(_image.quiet()); |
67 | |
|
68 | 0 | return pixels; |
69 | 0 | } |
70 | | |
71 | | ssize_t Magick::Pixels::offset(PixelChannel channel) const |
72 | 0 | { |
73 | 0 | if (_image.constImage()->channel_map[channel].traits == UndefinedPixelTrait) |
74 | 0 | return -1; |
75 | 0 | return _image.constImage()->channel_map[channel].offset; |
76 | 0 | } |
77 | | |
78 | | Magick::Quantum* Magick::Pixels::set(const ssize_t x_,const ssize_t y_, |
79 | | const size_t columns_,const size_t rows_) |
80 | 0 | { |
81 | 0 | _x=x_; |
82 | 0 | _y=y_; |
83 | 0 | _columns=columns_; |
84 | 0 | _rows=rows_; |
85 | |
|
86 | 0 | GetPPException; |
87 | 0 | Quantum* pixels=QueueCacheViewAuthenticPixels(_view,x_,y_,columns_,rows_, |
88 | 0 | exceptionInfo); |
89 | 0 | ThrowPPException(_image.quiet()); |
90 | |
|
91 | 0 | return pixels; |
92 | 0 | } |
93 | | |
94 | | void Magick::Pixels::sync(void) |
95 | 0 | { |
96 | 0 | GetPPException; |
97 | 0 | (void) SyncCacheViewAuthenticPixels(_view,exceptionInfo); |
98 | 0 | ThrowPPException(_image.quiet()); |
99 | 0 | } |
100 | | |
101 | | // Return pixel meta content |
102 | | void* Magick::Pixels::metacontent(void) |
103 | 0 | { |
104 | 0 | void* pixel_metacontent=GetCacheViewAuthenticMetacontent(_view); |
105 | |
|
106 | 0 | return pixel_metacontent; |
107 | 0 | } |
108 | | |
109 | | Magick::PixelData::PixelData(Magick::Image &image_,std::string map_, |
110 | | const StorageType type_) |
111 | 0 | { |
112 | 0 | init(image_,0,0,image_.columns(),image_.rows(),map_,type_); |
113 | 0 | } |
114 | | |
115 | | Magick::PixelData::PixelData(Magick::Image &image_,const ::ssize_t x_, |
116 | | const ::ssize_t y_,const size_t width_,const size_t height_,std::string map_, |
117 | | const StorageType type_) |
118 | 0 | { |
119 | 0 | init(image_,x_,y_,width_,height_,map_,type_); |
120 | 0 | } |
121 | | |
122 | | Magick::PixelData::~PixelData(void) |
123 | 0 | { |
124 | 0 | relinquish(); |
125 | 0 | } |
126 | | |
127 | | const void *Magick::PixelData::data(void) const |
128 | 0 | { |
129 | 0 | return(_data); |
130 | 0 | } |
131 | | |
132 | | ::ssize_t Magick::PixelData::length(void) const |
133 | 0 | { |
134 | 0 | return(_length); |
135 | 0 | } |
136 | | |
137 | | ::ssize_t Magick::PixelData::size(void) const |
138 | 0 | { |
139 | 0 | return(_size); |
140 | 0 | } |
141 | | |
142 | | void Magick::PixelData::init(Magick::Image &image_,const ::ssize_t x_, |
143 | | const ::ssize_t y_,const size_t width_,const size_t height_, |
144 | | std::string map_,const StorageType type_) |
145 | 0 | { |
146 | 0 | size_t |
147 | 0 | size; |
148 | |
|
149 | 0 | _data=(void *) NULL; |
150 | 0 | _length=0; |
151 | 0 | _size=0; |
152 | 0 | if ((x_ < 0) || (width_ == 0) || (y_ < 0) || (height_ == 0) || |
153 | 0 | (x_ > (ssize_t) image_.columns()) || (((ssize_t) width_ + x_) > (ssize_t) image_.columns()) |
154 | 0 | || (y_ > (ssize_t) image_.rows()) || (((ssize_t) height_ + y_) > (ssize_t) image_.rows()) |
155 | 0 | || (map_.length() == 0)) |
156 | 0 | return; |
157 | | |
158 | 0 | switch(type_) |
159 | 0 | { |
160 | 0 | case CharPixel: |
161 | 0 | size=sizeof(unsigned char); |
162 | 0 | break; |
163 | 0 | case DoublePixel: |
164 | 0 | size=sizeof(double); |
165 | 0 | break; |
166 | 0 | case FloatPixel: |
167 | 0 | size=sizeof(float); |
168 | 0 | break; |
169 | 0 | case LongPixel: |
170 | 0 | size=sizeof(unsigned int); |
171 | 0 | break; |
172 | 0 | case LongLongPixel: |
173 | 0 | size=sizeof(MagickSizeType); |
174 | 0 | break; |
175 | 0 | case QuantumPixel: |
176 | 0 | size=sizeof(Quantum); |
177 | 0 | break; |
178 | 0 | case ShortPixel: |
179 | 0 | size=sizeof(unsigned short); |
180 | 0 | break; |
181 | 0 | default: |
182 | 0 | throwExceptionExplicit(MagickCore::OptionError,"Invalid type"); |
183 | 0 | return; |
184 | 0 | } |
185 | | |
186 | 0 | _length=(ssize_t) (width_*height_*map_.length()); |
187 | 0 | _size=_length*(ssize_t) size; |
188 | 0 | _data=AcquireMagickMemory((size_t) _size); |
189 | |
|
190 | 0 | GetPPException; |
191 | 0 | MagickCore::ExportImagePixels(image_.image(),x_,y_,width_,height_, |
192 | 0 | map_.c_str(),type_,_data,exceptionInfo); |
193 | 0 | if (exceptionInfo->severity != MagickCore::UndefinedException) |
194 | 0 | relinquish(); |
195 | 0 | ThrowPPException(image_.quiet()); |
196 | 0 | } |
197 | | |
198 | | void Magick::PixelData::relinquish(void) throw() |
199 | 0 | { |
200 | 0 | if (_data != (void *)NULL) |
201 | 0 | _data=RelinquishMagickMemory(_data); |
202 | 0 | _length=0; |
203 | 0 | _size=0; |
204 | 0 | } |