1 // This may look like C code, but it is really -*- C++ -*-
3 // Copyright Bob Friesenhahn, 1999, 2000, 2003
5 // Test reading/writing BLOBs using Magick++
13 #if defined(MISSING_STD_IOS_BINARY)
14 # define IOS_IN_BINARY ios::in
16 # define IOS_IN_BINARY ios::in | ios::binary
21 using namespace Magick;
23 // A derived Blob class to exercise updateNoCopy()
24 class myBlob : public Blob
27 // Construct from open binary stream
28 myBlob( ifstream &stream_ )
31 unsigned char* blobData = new unsigned char[100000];
32 char* c= reinterpret_cast<char *>(blobData);
34 while( (blobLen< 100000) && stream_.get(*c) )
39 if ((!stream_.eof()) || (blobLen == 0))
41 cout << "Failed to stream into blob!" << endl;
45 // Insert data into blob
46 updateNoCopy( reinterpret_cast<unsigned char*>(blobData), blobLen,
52 int main( int /*argc*/, char ** argv)
55 // Initialize ImageMagick install location for Windows
56 InitializeMagick(*argv);
63 if(getenv("SRCDIR") != 0)
64 srcdir = getenv("SRCDIR");
74 Image image(srcdir + "test_image.miff");
75 signature = image.signature();
78 // Read raw data from file into BLOB
79 testimage = srcdir + "test_image.miff";
80 ifstream in( testimage.c_str(), ios::in | IOS_IN_BINARY );
83 cout << "Failed to open file " << testimage << " for input!" << endl;
86 unsigned char* blobData = new unsigned char[100000];
87 char* c=reinterpret_cast<char *>(blobData);
89 while( (blobLen< 100000) && in.get(*c) )
94 if ((!in.eof()) || (blobLen == 0))
96 cout << "Failed to read file " << testimage << " for input!" << endl;
101 // Construct Magick++ Blob
102 Blob blob(static_cast<const unsigned char*>(blobData), blobLen);
105 // If construction of image fails, an exception should be thrown
107 // Construct with blob data only
109 if ( image.signature() != signature )
112 cout << "Line: " << __LINE__
113 << " Image signature "
116 << signature << endl;
121 // Construct with image geometry and blob data
122 Image image( blob, Geometry(148,99));
123 if ( image.signature() != signature )
126 cout << "Line: " << __LINE__
127 << " Image signature "
130 << signature << endl;
135 // Construct default image, and then read in blob data
138 if ( image.signature() != signature )
141 cout << "Line: " << __LINE__
142 << " Image signature "
145 << signature << endl;
150 // Construct default image, and then read in blob data with
153 image.read( blob, Geometry(148,99) );
154 if ( image.signature() != signature )
157 cout << "Line: " << __LINE__
158 << " Image signature "
161 << signature << endl;
167 // Test writing BLOBs
170 string signature("");
172 Image image(srcdir + "test_image.miff");
173 image.magick("MIFF");
174 image.write( &blob );
175 signature = image.signature();
179 if ( image.signature() != signature )
182 cout << "Line: " << __LINE__
183 << " Image signature "
186 << signature << endl;
192 // Test writing BLOBs via STL writeImages
197 readImages( &first, srcdir + "test_image_anim.miff" );
198 writeImages( first.begin(), first.end(), &blob, true );
201 // Test constructing a BLOB from a derived class
204 string signature("");
206 Image image(srcdir + "test_image.miff");
207 signature = image.signature();
210 // Read raw data from file into BLOB
211 testimage = srcdir + "test_image.miff";
212 ifstream in( testimage.c_str(), ios::in | IOS_IN_BINARY );
215 cout << "Failed to open file for input!" << endl;
223 if ( image.signature() != signature )
226 cout << "Line: " << __LINE__
227 << " Image signature "
230 << signature << endl;
235 catch( Exception &error_ )
237 cout << "Caught exception: " << error_.what() << endl;
240 catch( exception &error_ )
242 cout << "Caught exception: " << error_.what() << endl;
248 cout << failures << " failures" << endl;