Magick++  7.1.0
ImageRef.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
4 //
5 // Copyright @ 2014 ImageMagick Studio LLC, a non-profit organization
6 // dedicated to making software imaging solutions freely available.
7 //
8 // Implementation of ImageRef
9 //
10 // This is an internal implementation class.
11 //
12 
13 #define MAGICKCORE_IMPLEMENTATION 1
14 #define MAGICK_PLUSPLUS_IMPLEMENTATION 1
15 
16 #include "Magick++/ImageRef.h"
17 #include "Magick++/Exception.h"
18 #include "Magick++/Options.h"
19 
21  : _image(0),
22  _mutexLock(),
23  _options(new Options),
24  _refCount(1)
25 {
27  _image=AcquireImage(_options->imageInfo(),exceptionInfo);
28  ThrowPPException(false);
29 }
30 
32  : _image(image_),
33  _mutexLock(),
34  _options(new Options),
35  _refCount(1)
36 {
37 }
38 
40 {
41  // Deallocate image
42  if (_image != (MagickCore::Image*) NULL)
43  _image=DestroyImageList(_image);
44 
45  // Deallocate image options
46  delete _options;
47  _options=(Options *) NULL;
48 }
49 
51 {
52  size_t
53  count;
54 
55  _mutexLock.lock();
56  if (_refCount == 0)
57  {
58  _mutexLock.unlock();
59  throwExceptionExplicit(MagickCore::OptionError,
60  "Invalid call to decrease");
61  return(0);
62  }
63  count=--_refCount;
64  _mutexLock.unlock();
65  return(count);
66 }
67 
69 {
70  return(_image);
71 }
72 
74 {
75  _mutexLock.lock();
76  _refCount++;
77  _mutexLock.unlock();
78 }
79 
81 {
82  bool
83  isShared;
84 
85  _mutexLock.lock();
86  isShared=(_refCount > 1);
87  _mutexLock.unlock();
88  return(isShared);
89 }
90 
92 {
93  delete _options;
94  _options=options_;
95 }
96 
98 {
99  return(_options);
100 }
101 
103  MagickCore::Image *replacement_)
104 {
106  *instance;
107 
108  imgRef->_mutexLock.lock();
109  if (imgRef->_refCount == 1)
110  {
111  // We can replace the image if we own it.
112  instance=imgRef;
113  if (imgRef->_image != (MagickCore::Image*) NULL)
114  (void) DestroyImageList(imgRef->_image);
115  imgRef->_image=replacement_;
116  imgRef->_mutexLock.unlock();
117  }
118  else
119  {
120  // We don't own the image, create a new ImageRef instance.
121  instance=new ImageRef(replacement_,imgRef->_options);
122  imgRef->_refCount--;
123  imgRef->_mutexLock.unlock();
124  }
125  return(instance);
126 }
127 
128 std::string Magick::ImageRef::signature(const bool force_)
129 {
130  const char
131  *property;
132 
133  // Re-calculate image signature if necessary
135  _mutexLock.lock();
136  property=(const char *) NULL;
137  if (!force_ && (_image->taint == MagickFalse))
138  property=GetImageProperty(_image,"Signature",exceptionInfo);
139  if (property == (const char *) NULL)
140  {
141  (void) SignatureImage(_image,exceptionInfo);
142  property=GetImageProperty(_image,"Signature",exceptionInfo);
143  }
144  _mutexLock.unlock();
145  ThrowPPException(true);
146 
147  return(std::string(property));
148 }
149 
151  : _image(image_),
152  _mutexLock(),
153  _options(0),
154  _refCount(1)
155 {
156  _options=new Options(*options_);
157 }
MagickCore::ImageInfo * imageInfo(void)
Definition: Options.cpp:950
void unlock(void)
Definition: Thread.cpp:97
size_t decrease()
Definition: ImageRef.cpp:50
void lock(void)
Definition: Thread.cpp:78
Options * options(void)
Definition: ImageRef.cpp:97
MagickPPExport void throwExceptionExplicit(const MagickCore::ExceptionType severity_, const char *reason_, const char *description_=(char *) NULL)
Definition: Exception.cpp:808
#define ThrowPPException(quiet)
Definition: Include.h:1580
static ImageRef * replaceImage(ImageRef *imgRef, MagickCore::Image *replacement_)
Definition: ImageRef.cpp:102
class MagickPPExport Image
Definition: Drawable.h:722
MagickCore::Image *& image(void)
Definition: ImageRef.cpp:68
#define GetPPException
Definition: Include.h:1561
std::string signature(const bool force_=false)
Definition: ImageRef.cpp:128