root / ImageMagick / trunk / tests / rwblob.c

Revision 12152, 10.3 kB (checked in by cristy, 9 days ago)
Line 
1/*
2 *
3 * Test BLOB operations via write/read/write/read sequence to detect
4 * any data corruption problems. This does not verify that the image is
5 * correct, only that encode/decode process is repeatable.
6 *
7 * Written by Bob Friesenhahn <bfriesen@simple.dallas.tx.us>
8 *
9 * The image returned by both reads must be identical in order for the
10 * test to pass.
11 *
12 */
13
14#if !defined(_VISUALC_)
15#include <magick/magick-config.h>
16#endif
17#include <stdio.h>
18#include <stdlib.h>
19#include <sys/types.h>
20#include <string.h>
21#if defined(_VISUALC_)
22#include <stdlib.h>
23#include <sys\types.h>
24#endif
25#include <time.h>
26#include <magick/MagickCore.h>
27
28int main ( int argc, char **argv )
29{
30  Image
31    *final = (Image *) NULL,
32    *original = (Image *) NULL;
33
34  const MagickInfo
35    *magick_info;
36
37  size_t
38    blob_length = 0;
39
40  char
41    *blob = NULL,
42    infile[MaxTextExtent],
43    format[MaxTextExtent],
44    size[MaxTextExtent];
45
46  int
47    arg = 1,
48    exit_status = 0,
49    rows = 0,
50    columns = 0,
51    pause = 0;
52
53  double
54    fuzz_factor = 0;
55
56  ImageInfo
57    *imageInfo;
58
59  ExceptionInfo
60    *exception;
61
62  if (LocaleNCompare("rwblob",argv[0],7) == 0)
63    MagickCoreGenesis((char *) NULL,MagickTrue);
64  else
65    MagickCoreGenesis(*argv,MagickTrue);
66
67  imageInfo=CloneImageInfo(0);
68  exception=AcquireExceptionInfo();
69
70  for (arg=1; arg < argc; arg++)
71    {
72      char
73        *option = argv[arg];
74   
75      if (*option == '-')
76        {
77          if (LocaleCompare("compress",option+1) == 0)
78            {
79              arg++;
80              option=argv[arg];
81              imageInfo->compression=UndefinedCompression;
82              if (LocaleCompare("None",option) == 0)
83                imageInfo->compression=NoCompression;
84              if (LocaleCompare("BZip",option) == 0)
85                imageInfo->compression=BZipCompression;
86              if (LocaleCompare("Fax",option) == 0)
87                imageInfo->compression=FaxCompression;
88              if (LocaleCompare("Group4",option) == 0)
89                imageInfo->compression=Group4Compression;
90              if (LocaleCompare("JPEG",option) == 0)
91                imageInfo->compression=JPEGCompression;
92              if (LocaleCompare("Lossless",option) == 0)
93                imageInfo->compression=LosslessJPEGCompression;
94              if (LocaleCompare("LZW",option) == 0)
95                imageInfo->compression=LZWCompression;
96              if (LocaleCompare("RLE",option) == 0)
97                imageInfo->compression=RLECompression;
98              if (LocaleCompare("Zip",option) == 0)
99                imageInfo->compression=ZipCompression;
100            }
101          else if (LocaleCompare("debug",option+1) == 0)
102              (void) SetLogEventMask(argv[++arg]);
103          else if (LocaleCompare("depth",option+1) == 0)
104            {
105              imageInfo->depth=MAGICKCORE_QUANTUM_DEPTH;
106              arg++;
107              if ((arg == argc) || !sscanf(argv[arg],"%ld",&imageInfo->depth))
108                {
109                  (void) printf("-depth argument missing or not integer\n");
110                  (void) fflush(stdout);
111                  exit_status = 1;
112                  goto program_exit;
113                }
114              if(imageInfo->depth != 8 && imageInfo->depth != 16 && imageInfo->depth != 32)
115                {
116                  (void) printf("-depth (%ld) not 8, 16, or 32\n", imageInfo->depth);
117                  (void) fflush(stdout);
118                  exit_status = 1;
119                  goto program_exit;
120                }
121            }
122          else if (LocaleCompare("log",option+1) == 0)
123              (void) SetLogFormat(argv[++arg]);
124          else if (LocaleCompare("pause",option+1) == 0)
125              pause=1;
126          else if (LocaleCompare("size",option+1) == 0)
127            {
128              arg++;
129              if ((arg == argc) || !IsGeometry(argv[arg]))
130                {
131                  (void) printf("-size argument missing or not geometry\n");
132                  (void) fflush(stdout);
133                  exit_status = 1;
134                  goto program_exit;
135                }
136              (void) CloneString(&imageInfo->size,argv[arg]);
137            }
138          else if (LocaleCompare("verbose",option+1) == 0)
139            {
140              imageInfo->verbose=MagickTrue;
141            }
142        }
143      else
144        break;
145    }
146  if (arg != argc-2)
147    {
148      (void) printf("arg=%d, argc=%d\n", arg, argc);
149      (void) printf ( "Usage: %s [-compress algorithm -debug events -depth integer -log format -size geometry -verbose] infile format\n", argv[0] );
150      (void) fflush(stdout);
151      exit_status = 1;
152      goto program_exit;
153    }
154
155  (void) strncpy(infile, argv[arg], MaxTextExtent-1 );
156  arg++;
157  (void) strncpy( format, argv[arg], MaxTextExtent-1 );
158
159  for (arg=0; arg < argc; arg++)
160    (void) printf("%s ", argv[arg]);
161  (void) printf("\n");
162  (void) fflush(stdout);
163
164  /*
165   * Read original image
166   */
167  imageInfo=DestroyImageInfo(imageInfo);
168  imageInfo=CloneImageInfo(0);
169  ClearMagickException(exception);
170  imageInfo->dither=MagickFalse;
171  (void) strncpy( imageInfo->filename, infile, MaxTextExtent-1 );
172  (void) LogMagickEvent(CoderEvent,GetMagickModule(),
173    "Reading image %s", imageInfo->filename);
174  original = ReadImage ( imageInfo, exception );
175  if (exception->severity >= ErrorException)
176    {
177      CatchException(exception);
178      exit_status = 1;
179      goto program_exit;
180    }
181  if ( original == (Image *)NULL )
182    {
183      (void) printf ( "Failed to read original image %s\n", imageInfo->filename );
184      (void) fflush(stdout);
185      exit_status = 1;
186      goto program_exit;
187    }
188
189  /*
190   * Obtain original image size if format requires it
191   */
192  rows    = original->rows;
193  columns = original->columns;
194  size[0]='\0';
195  magick_info=GetMagickInfo(format,exception);
196  if (magick_info && magick_info->raw)
197    FormatMagickString( size, MaxTextExtent, "%dx%d", columns, rows );
198
199  /*
200   * Save image to BLOB
201   */
202  blob_length = 8192;
203  (void) strncpy(original->magick,format,MaxTextExtent-1);
204  (void) CopyMagickString(imageInfo->filename,"",MaxTextExtent);
205  original->delay = 10;
206  (void) LogMagickEvent(CoderEvent,GetMagickModule(),
207    "Writing image to BLOB");
208  blob =(char *) ImageToBlob ( imageInfo, original, &blob_length, exception );
209  if (exception->severity >= ErrorException)
210    {
211      CatchException(exception);
212      exit_status = 1;
213      goto program_exit;
214    }
215  if ( blob == NULL )
216    {
217      (void) printf ( "Failed to write BLOB in format %s\n", imageInfo->magick );
218      (void) fflush(stdout);
219      exit_status = 1;
220      goto program_exit;
221    }
222  imageInfo->depth=original->depth;
223   original =DestroyImageList( original );
224  original = (Image*)NULL;
225
226  /*
227   * Read image back from BLOB
228   */
229  (void) strncpy( imageInfo->magick, format, MaxTextExtent-1 );
230  (void) CopyMagickString(imageInfo->filename,"",MaxTextExtent);
231  if ( size[0] != '\0' )
232    CloneString( &imageInfo->size, size );
233  (void) fflush(stdout);
234  (void) LogMagickEvent(CoderEvent,GetMagickModule(),
235    "Reading image %s", imageInfo->filename);
236  original = BlobToImage( imageInfo, blob, blob_length, exception );
237  if (exception->severity >= ErrorException)
238    {
239      CatchException(exception);
240      exit_status = 1;
241      goto program_exit;
242    }
243  if ( original == (Image *)NULL )
244    {
245      (void) printf ( "Failed to read image from BLOB in format %s\n",imageInfo->magick );
246      (void) fflush(stdout);
247      exit_status = 1;
248      goto program_exit;
249    }
250  blob=DestroyString(blob);
251
252  /*
253   * Save image to BLOB
254   */
255  blob_length = 8192;
256  (void) strncpy( original->magick, format, MaxTextExtent-1 );
257  (void) CopyMagickString(imageInfo->filename,"",MaxTextExtent);
258  original->delay = 10;
259  blob = (char *) ImageToBlob ( imageInfo, original, &blob_length, exception );
260  if (exception->severity >= ErrorException)
261    {
262      CatchException(exception);
263      exit_status = 1;
264      goto program_exit;
265    }
266  imageInfo->depth=original->depth;
267  if ( blob == NULL )
268    {
269      (void) printf ( "Failed to write BLOB in format %s\n", imageInfo->magick );
270      (void) fflush(stdout);
271      exit_status = 1;
272      goto program_exit;
273    }
274
275  /*
276   * Read image back from BLOB
277   */
278  (void) strncpy( imageInfo->magick, format, MaxTextExtent-1 );
279  (void) CopyMagickString(imageInfo->filename,"",MaxTextExtent);
280  if ( size[0] != '\0' )
281    CloneString( &imageInfo->size, size );
282  (void) LogMagickEvent(CoderEvent,GetMagickModule(),
283    "Reading image from BLOB");
284  final = BlobToImage( imageInfo, blob, blob_length, exception );
285  if (exception->severity >= ErrorException)
286    {
287      CatchException(exception);
288      exit_status = 1;
289      goto program_exit;
290    }
291  if ( final == (Image *)NULL )
292    {
293      (void) printf ( "Failed to read image from BLOB in format %s\n",imageInfo->magick );
294      (void) fflush(stdout);
295      exit_status = 1;
296      goto program_exit;
297    }
298  blob=DestroyString(blob);
299
300  /*
301   * Check final output
302   */
303
304  fuzz_factor = 0.0;
305#if defined(MAGICKCORE_HDRI_SUPPORT)
306  fuzz_factor = 0.06;
307#endif
308  if ( !strcmp( "JPEG", format ) ||
309       !strcmp( "JNG", format ) ||
310       !strcmp( "JPG", format ) ||
311       !strcmp( "JPG24", format ) ||
312       !strcmp( "JP2", format ) ||
313       !strcmp( "GRAY", format ) ||
314       !strcmp( "CMYK", format ) ||
315       !strcmp( "PAL", format ) ||
316       !strcmp( "PCD", format ) ||
317       !strcmp( "PCDS", format ) ||
318       !strcmp( "PGM", format ) ||
319       !strcmp( "PPM", format ) ||
320       !strcmp( "PNM", format ) ||
321       !strcmp( "SGI", format ) ||
322       !strcmp( "XPM", format ) ||
323       !strcmp( "UYVY", format ) ||
324       !strcmp( "YUV", format ) || (final->compression == JPEGCompression))
325    fuzz_factor = 0.06;
326 
327  SetImageColorspace(final,original->colorspace);
328  if ( !IsImagesEqual(original, final ) &&
329       (original->error.normalized_mean_error > fuzz_factor) )
330    {
331      (void) printf( "R/W file check for format \"%s\" failed: %u/%.6f/%.6fe\n",
332              format,(unsigned int) original->error.mean_error_per_pixel,
333              original->error.normalized_mean_error,
334              original->error.normalized_maximum_error);
335      (void) fflush(stdout);
336      exit_status = 1;
337      goto program_exit;
338    }
339
340 program_exit:
341  if (original)
342    original=DestroyImageList(original);
343  if (final)
344    final=DestroyImageList(final);
345
346  exception=DestroyExceptionInfo(exception);
347  imageInfo=DestroyImageInfo(imageInfo);
348  MagickCoreTerminus();
349
350  if (pause)
351    (void) getc(stdin);
352  return exit_status;
353}
Note: See TracBrowser for help on using the browser.