Magick++ 7.1.1
Loading...
Searching...
No Matches
ImageRef.cpp
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
20Magick::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
31Magick::ImageRef::ImageRef(MagickCore::Image *image_)
32 : _image(image_),
33 _mutexLock(),
34 _options(new Options),
35 _refCount(1)
36{
37}
38
39Magick::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
50size_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=(size_t) (--_refCount);
64 _mutexLock.unlock();
65 return(count);
66}
67
68MagickCore::Image *&Magick::ImageRef::image(void)
69{
70 return(_image);
71}
72
73void Magick::ImageRef::increase()
74{
75 _mutexLock.lock();
76 _refCount++;
77 _mutexLock.unlock();
78}
79
80bool Magick::ImageRef::isShared()
81{
82 bool
83 isShared;
84
85 _mutexLock.lock();
86 isShared=(_refCount > 1);
87 _mutexLock.unlock();
88 return(isShared);
89}
90
91void Magick::ImageRef::options(Magick::Options *options_)
92{
93 delete _options;
94 _options=options_;
95}
96
97Magick::Options *Magick::ImageRef::options(void)
98{
99 return(_options);
100}
101
102Magick::ImageRef *Magick::ImageRef::replaceImage(ImageRef *imgRef,
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
128std::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
150Magick::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}