• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // This may look like C code, but it is really -*- C++ -*-
2 //
3 // Copyright Bob Friesenhahn, 1999, 2000, 2003
4 //
5 // Test reading/writing BLOBs using Magick++
6 //
7 
8 #include <Magick++.h>
9 #include <string>
10 #include <iostream>
11 #include <fstream>
12 
13 #if defined(MISSING_STD_IOS_BINARY)
14 #  define IOS_IN_BINARY ios::in
15 #else
16 #  define IOS_IN_BINARY ios::in | ios::binary
17 #endif
18 
19 using namespace std;
20 
21 using namespace Magick;
22 
23 // A derived Blob class to exercise updateNoCopy()
24 class myBlob : public Blob
25 {
26 public:
27   // Construct from open binary stream
myBlob(ifstream & stream_)28   myBlob( ifstream &stream_ )
29     : Blob()
30     {
31       unsigned char* blobData = new unsigned char[100000];
32       char* c= reinterpret_cast<char *>(blobData);
33       size_t blobLen=0;
34       while( (blobLen< 100000) && stream_.get(*c) )
35         {
36           c++;
37           blobLen++;
38         }
39       if ((!stream_.eof()) || (blobLen == 0))
40         {
41           cout << "Failed to stream into blob!" << endl;
42           exit(1);
43         }
44 
45       // Insert data into blob
46       updateNoCopy( reinterpret_cast<unsigned char*>(blobData), blobLen,
47                     Blob::NewAllocator );
48     }
49 };
50 
51 
main(int,char ** argv)52 int main( int /*argc*/, char ** argv)
53 {
54 
55   // Initialize ImageMagick install location for Windows
56   InitializeMagick(*argv);
57 
58   int failures=0;
59 
60   try
61     {
62       string srcdir("");
63       if(getenv("SRCDIR") != 0)
64         srcdir = getenv("SRCDIR");
65 
66       string testimage;
67 
68       //
69       // Test reading BLOBs
70       //
71       {
72         string signature("");
73         {
74           Image image(srcdir + "test_image.miff");
75           signature = image.signature();
76         }
77 
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 );
81         if( !in )
82           {
83             cout << "Failed to open file " << testimage << " for input!" << endl;
84             exit(1);
85           }
86         unsigned char* blobData = new unsigned char[100000];
87         char* c=reinterpret_cast<char *>(blobData);
88         size_t blobLen=0;
89         while( (blobLen< 100000) && in.get(*c) )
90           {
91             c++;
92             blobLen++;
93           }
94         if ((!in.eof()) || (blobLen == 0))
95           {
96             cout << "Failed to read file " << testimage << " for input!" << endl;
97             exit(1);
98           }
99         in.close();
100 
101         // Construct Magick++ Blob
102         Blob blob(static_cast<const unsigned char*>(blobData), blobLen);
103         delete [] blobData;
104 
105         // If construction of image fails, an exception should be thrown
106         {
107           // Construct with blob data only
108           Image image( blob );
109           if ( image.signature() != signature )
110             {
111               ++failures;
112               cout << "Line: " << __LINE__
113                    << "  Image signature "
114                    << image.signature()
115                    << " != "
116                    << signature << endl;
117             }
118         }
119 
120         {
121           // Construct with image geometry and blob data
122           Image image(  blob, Geometry(148,99));
123           if ( image.signature() != signature )
124             {
125               ++failures;
126               cout << "Line: " << __LINE__
127                    << "  Image signature "
128                    << image.signature()
129                    << " != "
130                    << signature << endl;
131             }
132         }
133 
134         {
135           // Construct default image, and then read in blob data
136           Image image;
137           image.read( blob );
138           if ( image.signature() != signature )
139             {
140               ++failures;
141               cout << "Line: " << __LINE__
142                    << "  Image signature "
143                    << image.signature()
144                    << " != "
145                    << signature << endl;
146             }
147         }
148 
149         {
150           // Construct default image, and then read in blob data with
151           // image geometry
152           Image image;
153           image.read( blob, Geometry(148,99) );
154           if ( image.signature() != signature )
155             {
156               ++failures;
157               cout << "Line: " << __LINE__
158                    << "  Image signature "
159                    << image.signature()
160                    << " != "
161                    << signature << endl;
162             }
163         }
164 
165       }
166 
167       // Test writing BLOBs
168       {
169         Blob blob;
170         string signature("");
171         {
172           Image image(srcdir + "test_image.miff");
173           image.magick("MIFF");
174           image.write( &blob );
175           signature = image.signature();
176         }
177         {
178           Image image(blob);
179           if ( image.signature() != signature )
180             {
181               ++failures;
182               cout << "Line: " << __LINE__
183                    << "  Image signature "
184                    << image.signature()
185                    << " != "
186                    << signature << endl;
187               image.display();
188             }
189         }
190 
191       }
192       // Test writing BLOBs via STL writeImages
193       {
194         Blob blob;
195 
196         list<Image> first;
197         readImages( &first, srcdir + "test_image_anim.miff" );
198         writeImages( first.begin(), first.end(), &blob, true );
199       }
200 
201       // Test constructing a BLOB from a derived class
202       {
203 
204         string signature("");
205         {
206           Image image(srcdir + "test_image.miff");
207           signature = image.signature();
208         }
209 
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 );
213         if( !in )
214           {
215             cout << "Failed to open file for input!" << endl;
216             exit(1);
217           }
218 
219         myBlob blob( in );
220         in.close();
221 
222         Image image( blob );
223         if ( image.signature() != signature )
224           {
225             ++failures;
226             cout << "Line: " << __LINE__
227                  << "  Image signature "
228                  << image.signature()
229                  << " != "
230                  << signature << endl;
231           }
232       }
233     }
234 
235   catch( Exception &error_ )
236     {
237       cout << "Caught exception: " << error_.what() << endl;
238       return 1;
239     }
240   catch( exception &error_ )
241     {
242       cout << "Caught exception: " << error_.what() << endl;
243       return 1;
244     }
245 
246   if ( failures )
247     {
248       cout << failures << " failures" << endl;
249       return 1;
250     }
251 
252   return 0;
253 }
254 
255 
256