Magick++  6.9.3
Pixels.cpp
Go to the documentation of this file.
1 // This may look like C code, but it is really -*- C++ -*-
2 //
3 // Copyright Bob Friesenhahn, 1999, 2000, 2001, 2002, 2003
4 // Copyright Dirk Lemstra 2014
5 //
6 // Pixels Implementation
7 //
8 
9 #define MAGICKCORE_IMPLEMENTATION 1
10 #define MAGICK_PLUSPLUS_IMPLEMENTATION 1
11 
12 #include "Magick++/Include.h"
13 #include <string> // This is here to compile with Visual C++
14 #include "Magick++/Thread.h"
15 #include "Magick++/Exception.h"
16 #include "Magick++/Pixels.h"
17 
19  : _image(image_),
20  _x(0),
21  _y(0),
22  _columns(0),
23  _rows(0)
24 {
26  _view=AcquireVirtualCacheView(image_.image(),exceptionInfo);
27  ThrowPPException(image_.quiet());
28 }
29 
31 {
32  if (_view != (MagickCore::CacheView *) NULL)
33  _view=DestroyCacheView(_view);
34 }
35 
36 Magick::PixelPacket* Magick::Pixels::get(const ssize_t x_,const ssize_t y_,
37  const size_t columns_,const size_t rows_)
38 {
39  _x=x_;
40  _y=y_;
41  _columns=columns_;
42  _rows=rows_;
43 
45  PixelPacket* pixels=GetCacheViewAuthenticPixels(_view,x_,y_,columns_,rows_,
46  exceptionInfo);
47  ThrowPPException(_image.quiet());
48 
49  return pixels;
50 }
51 
52 const Magick::PixelPacket* Magick::Pixels::getConst(const ssize_t x_,
53  const ssize_t y_,const size_t columns_,const size_t rows_)
54 {
55  _x=x_;
56  _y=y_;
57  _columns=columns_;
58  _rows=rows_;
59 
61  const PixelPacket* pixels=GetCacheViewVirtualPixels(_view,x_,y_,columns_,
62  rows_,exceptionInfo);
63  ThrowPPException(_image.quiet());
64 
65  return pixels;
66 }
67 
68 Magick::PixelPacket* Magick::Pixels::set(const ssize_t x_,const ssize_t y_,
69  const size_t columns_,const size_t rows_)
70 {
71  _x=x_;
72  _y=y_;
73  _columns=columns_;
74  _rows=rows_;
75 
77  PixelPacket* pixels=QueueCacheViewAuthenticPixels(_view,x_,y_,columns_,rows_,
78  exceptionInfo);
79  ThrowPPException(_image.quiet());
80 
81  return pixels;
82 }
83 
85 {
87  (void) SyncCacheViewAuthenticPixels(_view,exceptionInfo);
88  ThrowPPException(_image.quiet());
89 }
90 
91 Magick::IndexPacket* Magick::Pixels::indexes (void)
92 {
93  IndexPacket* pixel_indexes=GetCacheViewAuthenticIndexQueue(_view);
94 
95  if (!pixel_indexes)
96  _image.throwImageException();
97 
98  return pixel_indexes;
99 }
100 
102  const StorageType type_)
103 {
104  init(image_,0,0,image_.columns(),image_.rows(),map_,type_);
105 }
106 
107 Magick::PixelData::PixelData(Magick::Image &image_,const ::ssize_t x_,
108  const ::ssize_t y_,const size_t width_,const size_t height_,std::string map_,
109  const StorageType type_)
110 {
111  init(image_,x_,y_,width_,height_,map_,type_);
112 }
113 
115 {
116  relinquish();
117 }
118 
119 const void *Magick::PixelData::data(void) const
120 {
121  return(_data);
122 }
123 
124 ::ssize_t Magick::PixelData::length(void) const
125 {
126  return(_length);
127 }
128 
129 ::ssize_t Magick::PixelData::size(void) const
130 {
131  return(_size);
132 }
133 
134 void Magick::PixelData::init(Magick::Image &image_,const ::ssize_t x_,
135  const ::ssize_t y_,const size_t width_,const size_t height_,
136  std::string map_,const StorageType type_)
137 {
138  size_t
139  size;
140 
141  _data=(void *) NULL;
142  _length=0;
143  _size=0;
144  if ((x_ < 0) || (width_ == 0) || (y_ < 0) || (height_ == 0) ||
145  (x_ > (ssize_t) image_.columns()) || ((width_ + x_) > image_.columns())
146  || (y_ > (ssize_t) image_.rows()) || ((height_ + y_) > image_.rows())
147  || (map_.length() == 0))
148  return;
149 
150  switch(type_)
151  {
152  case CharPixel:
153  size=sizeof(unsigned char);
154  break;
155  case DoublePixel:
156  size=sizeof(double);
157  break;
158  case FloatPixel:
159  size=sizeof(float);
160  break;
161  case IntegerPixel:
162  case LongPixel:
163  size=sizeof(unsigned int);
164  break;
165  case QuantumPixel:
166  size=sizeof(Quantum);
167  break;
168  case ShortPixel:
169  size=sizeof(unsigned short);
170  break;
171  default:
172  throwExceptionExplicit(OptionError,"Invalid type");
173  return;
174  }
175 
176  _length=width_*height_*map_.length();
177  _size=_length*size;
178  _data=AcquireMagickMemory(_size);
179 
181  MagickCore::ExportImagePixels(image_.constImage(),x_,y_,width_,height_,
182  map_.c_str(),type_,_data,exceptionInfo);
183  if (exceptionInfo->severity != UndefinedException)
184  relinquish();
185  ThrowPPException(image_.quiet());
186 }
187 
188 void Magick::PixelData::relinquish(void) throw()
189 {
190  if (_data != (void *)NULL)
191  _data=RelinquishMagickMemory(_data);
192  _length=0;
193  _size=0;
194 }
void quiet(const bool quiet_)
Definition: Image.cpp:1379
const PixelPacket * getConst(const ::ssize_t x_, const ::ssize_t y_, const size_t columns_, const size_t rows_)
Definition: Pixels.cpp:52
MagickCore::Image *& image(void)
Definition: Image.cpp:5023
size_t columns(void) const
Definition: Image.h:1621
void sync(void)
Definition: Pixels.cpp:84
Pixels(Magick::Image &image_)
Definition: Pixels.cpp:18
const MagickCore::Image * constImage(void) const
Definition: Image.cpp:5028
~Pixels(void)
Definition: Pixels.cpp:30
MagickPPExport void throwExceptionExplicit(const MagickCore::ExceptionType severity_, const char *reason_, const char *description_=(char *) NULL)
IndexPacket * indexes(void)
Definition: Pixels.cpp:91
#define ThrowPPException(quiet)
Definition: Include.h:1510
::ssize_t size(void) const
Definition: Pixels.cpp:129
PixelData(Magick::Image &image_, std::string map_, const StorageType type_)
Definition: Pixels.cpp:101
::ssize_t length(void) const
Definition: Pixels.cpp:124
size_t rows(void) const
Definition: Image.h:1640
const void * data(void) const
Definition: Pixels.cpp:119
#define GetPPException
Definition: Include.h:1506
PixelPacket * get(const ::ssize_t x_, const ::ssize_t y_, const size_t columns_, const size_t rows_)
Definition: Pixels.cpp:36
PixelPacket * set(const ::ssize_t x_, const ::ssize_t y_, const size_t columns_, const size_t rows_)
Definition: Pixels.cpp:68