1 // This may look like C code, but it is really -*- C++ -*-
3 // Copyright Bob Friesenhahn, 1999, 2000, 2001, 2002, 2004
5 // Implementation of Blob
8 #define MAGICKCORE_IMPLEMENTATION 1
9 #define MAGICK_PLUSPLUS_IMPLEMENTATION 1
11 #include "Magick++/Include.h"
12 #include "Magick++/Blob.h"
13 #include "Magick++/BlobRef.h"
18 // Implementation of Magick::Blob
21 // Default constructor
22 Magick::Blob::Blob ( void )
23 : _blobRef(new Magick::BlobRef( 0, 0 ))
27 // Construct with data
28 Magick::Blob::Blob ( const void* data_, size_t length_ )
29 : _blobRef(new Magick::BlobRef( data_, length_ ))
33 // Copy constructor (reference counted)
34 Magick::Blob::Blob ( const Magick::Blob& blob_ )
35 : _blobRef(blob_._blobRef)
37 // Increase reference count
38 Lock( &_blobRef->_mutexLock );
39 ++_blobRef->_refCount;
42 // Destructor (reference counted)
43 Magick::Blob::~Blob ()
45 bool doDelete = false;
47 Lock( &_blobRef->_mutexLock );
48 if ( --_blobRef->_refCount == 0 )
54 // Delete old blob reference with associated data
60 // Assignment operator (reference counted)
61 Magick::Blob& Magick::Blob::operator= ( const Magick::Blob& blob_ )
66 Lock( &blob_._blobRef->_mutexLock );
67 ++blob_._blobRef->_refCount;
69 bool doDelete = false;
71 Lock( &_blobRef->_mutexLock );
72 if ( --_blobRef->_refCount == 0 )
79 _blobRef = blob_._blobRef;
84 // Update object contents from Base64-encoded string representation.
85 void Magick::Blob::base64 ( const std::string base64_ )
89 unsigned char *decoded =
90 Base64Decode( base64_.c_str(), &length );
93 updateNoCopy( static_cast<void*>(decoded), length,
94 Magick::Blob::MallocAllocator );
97 // Return Base64-encoded string representation.
98 std::string Magick::Blob::base64 ( void )
100 size_t encoded_length = 0;
103 Base64Encode(static_cast<const unsigned char*>(data()), length(), &encoded_length);
107 std::string result(encoded,encoded_length);
108 encoded=(char *) RelinquishMagickMemory(encoded);
112 return std::string();
115 // Update object contents, making a copy of the supplied data.
116 // Any existing data in the object is deallocated.
117 void Magick::Blob::update ( const void* data_, size_t length_ )
119 bool doDelete = false;
121 Lock( &_blobRef->_mutexLock );
122 if ( --_blobRef->_refCount == 0 )
127 // Delete old blob reference with associated data
131 _blobRef = new Magick::BlobRef( data_, length_ );
134 // Update object contents, using supplied pointer directly (no copy)
135 // Any existing data in the object is deallocated. The user must
136 // ensure that the pointer supplied is not deleted or otherwise
137 // modified after it has been supplied to this method.
138 void Magick::Blob::updateNoCopy ( void* data_, size_t length_,
139 Magick::Blob::Allocator allocator_ )
141 bool doDelete = false;
143 Lock( &_blobRef->_mutexLock );
144 if ( --_blobRef->_refCount == 0 )
149 // Delete old blob reference with associated data
152 _blobRef = new Magick::BlobRef( 0, 0 );
153 _blobRef->_data = data_;
154 _blobRef->_length = length_;
155 _blobRef->_allocator = allocator_;
158 // Obtain pointer to data
159 const void* Magick::Blob::data( void ) const
161 return _blobRef->_data;
164 // Obtain data length
165 size_t Magick::Blob::length( void ) const
167 return _blobRef->_length;