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