ImageRef.cpp revision 4b39a1ba9e9e98ae9847be7f1dee1034eab7076f
1// This may look like C code, but it is really -*- C++ -*-
2//
3// Copyright Bob Friesenhahn, 1999, 2000, 2001, 2002
4// Copyright Dirk Lemstra 2014-2015
5//
6// Implementation of ImageRef
7//
8// This is an internal implementation class.
9//
10
11#define MAGICKCORE_IMPLEMENTATION  1
12#define MAGICK_PLUSPLUS_IMPLEMENTATION 1
13
14#include "Magick++/ImageRef.h"
15#include "Magick++/Exception.h"
16#include "Magick++/Options.h"
17
18Magick::ImageRef::ImageRef(void)
19  : _image(0),
20    _mutexLock(),
21    _options(new Options),
22    _refCount(1)
23{
24  GetPPException;
25  _image=AcquireImage(_options->imageInfo(),exceptionInfo);
26  ThrowPPException(false);
27}
28
29Magick::ImageRef::ImageRef(MagickCore::Image *image_)
30  : _image(image_),
31    _mutexLock(),
32    _options(new Options),
33    _refCount(1)
34{
35}
36
37Magick::ImageRef::ImageRef(MagickCore::Image *image_,const Options *options_)
38  : _image(image_),
39    _mutexLock(),
40    _options(0),
41    _refCount(1)
42{
43  _options=new Options(*options_);
44}
45
46Magick::ImageRef::~ImageRef(void)
47{
48  // Deallocate image
49  if (_image != (MagickCore::Image*) NULL)
50    _image=DestroyImageList(_image);
51
52  // Deallocate image options
53  delete _options;
54  _options=(Options *) NULL;
55}
56
57size_t Magick::ImageRef::decrease()
58{
59  size_t
60    count;
61
62  _mutexLock.lock();
63  if (_refCount == 0)
64    {
65      _mutexLock.unlock();
66      throwExceptionExplicit(MagickCore::OptionError,
67        "Invalid call to decrease");
68    }
69  count=--_refCount;
70  _mutexLock.unlock();
71  return(count);
72}
73
74MagickCore::Image *&Magick::ImageRef::image(void)
75{
76  return(_image);
77}
78
79void Magick::ImageRef::increase()
80{
81  _mutexLock.lock();
82  _refCount++;
83  _mutexLock.unlock();
84}
85
86bool Magick::ImageRef::isShared()
87{
88  bool
89    isShared;
90
91  _mutexLock.lock();
92  isShared=(_refCount > 1);
93  _mutexLock.unlock();
94  return(isShared);
95}
96
97void  Magick::ImageRef::options(Magick::Options *options_)
98{
99  delete _options;
100  _options=options_;
101}
102
103Magick::Options *Magick::ImageRef::options(void)
104{
105  return(_options);
106}
107
108bool Magick::ImageRef::replaceImage(MagickCore::Image * replacement_)
109{
110  bool
111    replaced;
112
113  replaced=false;
114  _mutexLock.lock();
115  if (_refCount == 1)
116    {
117      if (_image != (MagickCore::Image*) NULL)
118        (void) DestroyImageList(_image);
119      _image=replacement_;
120      replaced=true;
121    }
122  _mutexLock.unlock();
123  return(replaced);
124}
125
126std::string Magick::ImageRef::signature(const bool force_)
127{
128  const char
129    *property;
130
131  // Re-calculate image signature if necessary
132  GetPPException;
133  _mutexLock.lock();
134  property=(const char *) NULL;
135  if (!force_ && (_image->taint == MagickFalse))
136    property=GetImageProperty(_image,"Signature",exceptionInfo);
137  if (property == (const char *) NULL)
138    {
139      (void) SignatureImage(_image,exceptionInfo);
140      property=GetImageProperty(_image,"Signature",exceptionInfo);
141    }
142  _mutexLock.unlock();
143  ThrowPPException(true);
144
145  return(std::string(property));
146}
147