/src/imagemagick/Magick++/lib/ImageRef.cpp
Line | Count | Source (jump to first uncovered line) |
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 | 725k | : _image(0), |
22 | 725k | _mutexLock(), |
23 | 725k | _options(new Options), |
24 | 725k | _refCount(1) |
25 | 725k | { |
26 | 725k | GetPPException; |
27 | 725k | _image=AcquireImage(_options->imageInfo(),exceptionInfo); |
28 | 725k | ThrowPPException(false); |
29 | 725k | } |
30 | | |
31 | | Magick::ImageRef::ImageRef(MagickCore::Image *image_) |
32 | 0 | : _image(image_), |
33 | 0 | _mutexLock(), |
34 | 0 | _options(new Options), |
35 | 0 | _refCount(1) |
36 | 0 | { |
37 | 0 | } |
38 | | |
39 | | Magick::ImageRef::~ImageRef(void) |
40 | 725k | { |
41 | | // Deallocate image |
42 | 725k | if (_image != (MagickCore::Image*) NULL) |
43 | 725k | _image=DestroyImageList(_image); |
44 | | |
45 | | // Deallocate image options |
46 | 725k | delete _options; |
47 | 725k | _options=(Options *) NULL; |
48 | 725k | } |
49 | | |
50 | | size_t Magick::ImageRef::decrease() |
51 | 725k | { |
52 | 725k | size_t |
53 | 725k | count; |
54 | | |
55 | 725k | _mutexLock.lock(); |
56 | 725k | if (_refCount == 0) |
57 | 0 | { |
58 | 0 | _mutexLock.unlock(); |
59 | 0 | throwExceptionExplicit(MagickCore::OptionError, |
60 | 0 | "Invalid call to decrease"); |
61 | 0 | return(0); |
62 | 0 | } |
63 | 725k | count=(size_t) (--_refCount); |
64 | 725k | _mutexLock.unlock(); |
65 | 725k | return(count); |
66 | 725k | } |
67 | | |
68 | | MagickCore::Image *&Magick::ImageRef::image(void) |
69 | 2.84M | { |
70 | 2.84M | return(_image); |
71 | 2.84M | } |
72 | | |
73 | | void Magick::ImageRef::increase() |
74 | 0 | { |
75 | 0 | _mutexLock.lock(); |
76 | 0 | _refCount++; |
77 | 0 | _mutexLock.unlock(); |
78 | 0 | } |
79 | | |
80 | | bool Magick::ImageRef::isShared() |
81 | 1.46M | { |
82 | 1.46M | bool |
83 | 1.46M | isShared; |
84 | | |
85 | 1.46M | _mutexLock.lock(); |
86 | 1.46M | isShared=(_refCount > 1); |
87 | 1.46M | _mutexLock.unlock(); |
88 | 1.46M | return(isShared); |
89 | 1.46M | } |
90 | | |
91 | | void Magick::ImageRef::options(Magick::Options *options_) |
92 | 0 | { |
93 | 0 | delete _options; |
94 | 0 | _options=options_; |
95 | 0 | } |
96 | | |
97 | | Magick::Options *Magick::ImageRef::options(void) |
98 | 3.86M | { |
99 | 3.86M | return(_options); |
100 | 3.86M | } |
101 | | |
102 | | Magick::ImageRef *Magick::ImageRef::replaceImage(ImageRef *imgRef, |
103 | | MagickCore::Image *replacement_) |
104 | 723k | { |
105 | 723k | Magick::ImageRef |
106 | 723k | *instance; |
107 | | |
108 | 723k | imgRef->_mutexLock.lock(); |
109 | 723k | if (imgRef->_refCount == 1) |
110 | 723k | { |
111 | | // We can replace the image if we own it. |
112 | 723k | instance=imgRef; |
113 | 723k | if (imgRef->_image != (MagickCore::Image*) NULL) |
114 | 723k | (void) DestroyImageList(imgRef->_image); |
115 | 723k | imgRef->_image=replacement_; |
116 | 723k | imgRef->_mutexLock.unlock(); |
117 | 723k | } |
118 | 0 | else |
119 | 0 | { |
120 | | // We don't own the image, create a new ImageRef instance. |
121 | 0 | instance=new ImageRef(replacement_,imgRef->_options); |
122 | 0 | imgRef->_refCount--; |
123 | 0 | imgRef->_mutexLock.unlock(); |
124 | 0 | } |
125 | 723k | return(instance); |
126 | 723k | } |
127 | | |
128 | | std::string Magick::ImageRef::signature(const bool force_) |
129 | 0 | { |
130 | 0 | const char |
131 | 0 | *property; |
132 | | |
133 | | // Re-calculate image signature if necessary |
134 | 0 | GetPPException; |
135 | 0 | _mutexLock.lock(); |
136 | 0 | property=(const char *) NULL; |
137 | 0 | if (!force_ && (_image->taint == MagickFalse)) |
138 | 0 | property=GetImageProperty(_image,"Signature",exceptionInfo); |
139 | 0 | if (property == (const char *) NULL) |
140 | 0 | { |
141 | 0 | (void) SignatureImage(_image,exceptionInfo); |
142 | 0 | property=GetImageProperty(_image,"Signature",exceptionInfo); |
143 | 0 | } |
144 | 0 | _mutexLock.unlock(); |
145 | 0 | ThrowPPException(true); |
146 | |
|
147 | 0 | return(std::string(property)); |
148 | 0 | } |
149 | | |
150 | | Magick::ImageRef::ImageRef(MagickCore::Image *image_,const Options *options_) |
151 | 0 | : _image(image_), |
152 | 0 | _mutexLock(), |
153 | 0 | _options(0), |
154 | 0 | _refCount(1) |
155 | 0 | { |
156 | 0 | _options=new Options(*options_); |
157 | 0 | } |