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
20 Magick::ImageRef::ImageRef(void)
21 : _image(0),
22 _mutexLock(),
23 _options(new Options),
24 _refCount(1)
25 {
26 GetPPException;
27 _image=AcquireImage(_options->imageInfo(),exceptionInfo);
28 ThrowPPException(false);
29 }
30
31 Magick::ImageRef::ImageRef(MagickCore::Image *image_)
32 : _image(image_),
33 _mutexLock(),
34 _options(new Options),
35 _refCount(1)
36 {
37 }
38
39 Magick::ImageRef::~ImageRef(void)
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
50 size_t Magick::ImageRef::decrease()
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
68 MagickCore::Image *&Magick::ImageRef::image(void)
69 {
70 return(_image);
71 }
72
73 void Magick::ImageRef::increase()
74 {
75 _mutexLock.lock();
76 _refCount++;
77 _mutexLock.unlock();
78 }
79
80 bool Magick::ImageRef::isShared()
81 {
82 bool
83 isShared;
84
85 _mutexLock.lock();
86 isShared=(_refCount > 1);
87 _mutexLock.unlock();
88 return(isShared);
89 }
90
91 void Magick::ImageRef::options(Magick::Options *options_)
92 {
93 delete _options;
94 _options=options_;
95 }
96
97 Magick::Options *Magick::ImageRef::options(void)
98 {
99 return(_options);
100 }
101
102 Magick::ImageRef *Magick::ImageRef::replaceImage(ImageRef *imgRef,
103 MagickCore::Image *replacement_)
104 {
105 Magick::ImageRef
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
134 GetPPException;
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
150 Magick::ImageRef::ImageRef(MagickCore::Image *image_,const Options *options_)
151 : _image(image_),
152 _mutexLock(),
153 _options(0),
154 _refCount(1)
155 {
156 _options=new Options(*options_);
157 }
ImageRef.h
Magick::Options
Definition: Options.h:30
Magick::Options::imageInfo
MagickCore::ImageInfo * imageInfo(void)
Definition: Options.cpp:950
Magick::ImageRef
Definition: ImageRef.h:28
Magick::MutexLock::unlock
void unlock(void)
Definition: Thread.cpp:97
Magick::ImageRef::decrease
size_t decrease()
Definition: ImageRef.cpp:50
Magick::MutexLock::lock
void lock(void)
Definition: Thread.cpp:78
Magick::ImageRef::options
Options * options(void)
Definition: ImageRef.cpp:97
Magick::throwExceptionExplicit
MagickPPExport void throwExceptionExplicit(const MagickCore::ExceptionType severity_, const char *reason_, const char *description_=(char *) NULL)
Definition: Exception.cpp:808
ThrowPPException
#define ThrowPPException(quiet)
Definition: Include.h:1580
Options.h
Magick::ImageRef::increase
void increase()
Definition: ImageRef.cpp:73
Magick::ImageRef::replaceImage
static ImageRef * replaceImage(ImageRef *imgRef, MagickCore::Image *replacement_)
Definition: ImageRef.cpp:102
Magick::ImageRef::isShared
bool isShared()
Definition: ImageRef.cpp:80
Magick::Image
class MagickPPExport Image
Definition: Drawable.h:722
Magick::ImageRef::ImageRef
ImageRef(void)
Definition: ImageRef.cpp:20
Magick::ImageRef::image
MagickCore::Image *& image(void)
Definition: ImageRef.cpp:68
GetPPException
#define GetPPException
Definition: Include.h:1561
Magick::ImageRef::~ImageRef
~ImageRef(void)
Definition: ImageRef.cpp:39
Exception.h
Magick::ImageRef::signature
std::string signature(const bool force_=false)
Definition: ImageRef.cpp:128