summaryrefslogtreecommitdiff
path: root/Magick++/lib/ImageRef.cpp
blob: c8b7b1936005d54d5e450b52855a32defb66f637 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// This may look like C code, but it is really -*- C++ -*-
//
// Copyright Bob Friesenhahn, 1999, 2000, 2001, 2002
// Copyright Dirk Lemstra 2014-2015
//
// Implementation of ImageRef
//
// This is an internal implementation class.
//

#define MAGICKCORE_IMPLEMENTATION  1
#define MAGICK_PLUSPLUS_IMPLEMENTATION 1

#include "Magick++/ImageRef.h"
#include "Magick++/Exception.h"
#include "Magick++/Options.h"

Magick::ImageRef::ImageRef(void)
  : _image(0),
    _mutexLock(),
    _options(new Options),
    _refCount(1)
{
  GetPPException;
  _image=AcquireImage(_options->imageInfo(),exceptionInfo);
  ThrowPPException(false);
}

Magick::ImageRef::ImageRef(MagickCore::Image *image_)
  : _image(image_),
    _mutexLock(),
    _options(new Options),
    _refCount(1)
{
}

Magick::ImageRef::ImageRef(MagickCore::Image *image_,const Options *options_)
  : _image(image_),
    _mutexLock(),
    _options(0),
    _refCount(1)
{
  _options=new Options(*options_);
}

Magick::ImageRef::~ImageRef(void)
{
  // Deallocate image
  if (_image != (MagickCore::Image*) NULL)
    _image=DestroyImageList(_image);

  // Deallocate image options
  delete _options;
  _options=(Options *) NULL;
}

size_t Magick::ImageRef::decrease()
{
  size_t
    count;

  _mutexLock.lock();
  if (_refCount == 0)
    {
      _mutexLock.unlock();
      throwExceptionExplicit(MagickCore::OptionError,
        "Invalid call to decrease");
    }
  count=--_refCount;
  _mutexLock.unlock();
  return(count);
}

MagickCore::Image *&Magick::ImageRef::image(void)
{
  return(_image);
}

void Magick::ImageRef::increase()
{
  _mutexLock.lock();
  _refCount++;
  _mutexLock.unlock();
}

bool Magick::ImageRef::isOwner()
{
  bool
    isOwner;

  _mutexLock.lock();
  isOwner=(_refCount == 1);
  _mutexLock.unlock();
  return(isOwner);
}

void  Magick::ImageRef::options(Magick::Options *options_)
{
  delete _options;
  _options=options_;
}

Magick::Options *Magick::ImageRef::options(void)
{
  return(_options);
}

bool Magick::ImageRef::replaceImage(MagickCore::Image * replacement_)
{
  bool
    replaced;

  replaced=false;
  _mutexLock.lock();
  if (_refCount == 1)
    {
      if (_image != (MagickCore::Image*) NULL)
        (void) DestroyImageList(_image);
      _image=replacement_;
      replaced=true;
    }
  _mutexLock.unlock();
  return(replaced);
}

std::string Magick::ImageRef::signature(const bool force_)
{
  const char
    *property;

  // Re-calculate image signature if necessary
  GetPPException;
  _mutexLock.lock();
  property=(const char *) NULL;
  if (!force_ && (_image->taint == MagickFalse))
    property=GetImageProperty(_image,"Signature",exceptionInfo);
  if (property == (const char *) NULL)
    {
      (void) SignatureImage(_image,exceptionInfo);
      property=GetImageProperty(_image,"Signature",exceptionInfo);
    }
  _mutexLock.unlock();
  ThrowPPException(true);

  return(std::string(property));
}