root / ImageMagick / trunk / tests / constitute.c

Revision 11686, 10.1 kB (checked in by cristy, 5 weeks ago)
Line 
1/*
2 *
3 * Test ExportImagePixels/ImportImagePixels operations via
4 * write/read/write/read sequence to detect any data corruption
5 * problems.
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  void
35    *pixels=0;
36
37  char
38    infile[MaxTextExtent],
39    map[MaxTextExtent];
40
41  int
42    arg = 1,
43    exit_status = 0,
44    rows = 0,
45    columns = 0,
46    pause = 0;
47
48  unsigned int
49    quantum_size=sizeof(unsigned char);
50
51  size_t
52    pixels_size;
53
54  double
55    fuzz_factor = 0.0;
56
57  ImageInfo
58    *imageInfo;
59
60  ExceptionInfo
61    exception;
62
63  MagickBooleanType
64    status;
65
66  StorageType
67    storage_type=CharPixel;
68
69  if (LocaleNCompare("constitute",argv[0],7) == 0)
70    MagickCoreGenesis((char *) NULL,MagickTrue);
71  else
72    MagickCoreGenesis(*argv,MagickTrue);
73
74  imageInfo=CloneImageInfo(0);
75  GetExceptionInfo( &exception );
76  fuzz_factor = 0.0;
77#if defined(MAGICKCORE_HDRI_SUPPORT)
78  fuzz_factor = 0.06;
79#endif
80
81  for (arg=1; arg < argc; arg++)
82    {
83      char
84        *option = argv[arg];
85   
86      if (*option == '-')
87        {
88          if (LocaleCompare("debug",option+1) == 0)
89            (void) SetLogEventMask(argv[++arg]);
90          else if (LocaleCompare("depth",option+1) == 0)
91            {
92              imageInfo->depth=MAGICKCORE_QUANTUM_DEPTH;
93              arg++;
94              if ((arg == argc) || !sscanf(argv[arg],"%ld",&imageInfo->depth))
95                {
96                  (void) printf("-depth argument missing or not integer\n");
97                  exit_status = 1;
98                  goto program_exit;
99                }
100              if(imageInfo->depth != 8 && imageInfo->depth != 16 && imageInfo->depth != 32)
101                {
102                  (void) printf("-depth (%ld) not 8, 16, or 32\n", imageInfo->depth);
103                  exit_status = 1;
104                  goto program_exit;
105                }
106            }
107          else if (LocaleCompare("log",option+1) == 0)
108            (void) SetLogFormat(argv[++arg]);
109          else if (LocaleCompare("pause",option+1) == 0)
110            pause=1;
111          else if (LocaleCompare("size",option+1) == 0)
112            {
113              arg++;
114              if ((arg == argc) || !IsGeometry(argv[arg]))
115                {
116                  (void) printf("-size argument missing or not geometry\n");
117                  exit_status = 1;
118                  goto program_exit;
119                }
120              (void) CloneString(&imageInfo->size,argv[arg]);
121            }
122          else if (LocaleCompare("storagetype",option+1) == 0)
123            {
124              arg++;
125              if ((arg == argc))
126                {
127                  (void) printf("-storagetype argument missing\n");
128                  exit_status = 1;
129                  goto program_exit;
130                }
131              if (LocaleCompare("Char",argv[arg]) == 0)
132                {
133                  storage_type=CharPixel;
134                  quantum_size=sizeof(unsigned char);
135                }
136              else if (LocaleCompare("Short",argv[arg]) == 0)
137                {
138                  storage_type=ShortPixel;
139                  quantum_size=sizeof(unsigned short);
140                }
141              else if (LocaleCompare("Integer",argv[arg]) == 0)
142                {
143                  storage_type=IntegerPixel;
144                  quantum_size=sizeof(unsigned int);
145                }
146              else if (LocaleCompare("Long",argv[arg]) == 0)
147                {
148                  storage_type=LongPixel;
149                  quantum_size=sizeof(unsigned long);
150                }
151              else if (LocaleCompare("Float",argv[arg]) == 0)
152                {
153                  storage_type=FloatPixel;
154                  quantum_size=sizeof(float);
155                }
156              else if (LocaleCompare("Double",argv[arg]) == 0)
157                {
158                  storage_type=DoublePixel;
159                  quantum_size=sizeof(double);
160                }
161              else
162                {
163                  (void) printf("Unrecognized storagetype argument %s\n",argv[arg]);
164                  exit_status = 1;
165                  goto program_exit;
166                }
167            }
168        }
169      else
170        break;
171    }
172  if (arg != argc-2)
173    {
174      (void) printf ( "Usage: %s -debug events -depth integer -log format -size geometry -storagetype type] infile map\n", argv[0] );
175      exit_status = 1;
176      goto program_exit;
177    }
178
179  (void) strncpy(infile, argv[arg], MaxTextExtent-1 );
180  arg++;
181  (void) strncpy( map, argv[arg], MaxTextExtent-1 );
182
183  for (arg=0; arg < argc; arg++)
184    (void) printf("%s ", argv[arg]);
185  (void) printf("\n");
186  (void) fflush(stdout);
187
188  /*
189   * Read original image
190   */
191  GetExceptionInfo( &exception );
192  imageInfo->dither = MagickFalse;
193  (void) strncpy( imageInfo->filename, infile, MaxTextExtent-1 );
194  (void) LogMagickEvent(CoderEvent,GetMagickModule(),
195                        "Reading image %s", imageInfo->filename);
196  original = ReadImage ( imageInfo, &exception );
197  if (exception.severity >= ErrorException)
198    CatchException(&exception);
199  if ( original == (Image *)NULL )
200    {
201      (void) printf ( "Failed to read original image %s\n", imageInfo->filename );
202      exit_status = 1;
203      goto program_exit;
204    }
205
206  /*  If a CMYK map is specified, make sure that input image is CMYK */
207  if (strchr(map,'c') || strchr(map,'C') || strchr(map,'m') || strchr(map,'M') ||
208      strchr(map,'y') || strchr(map,'y') || strchr(map,'k') || strchr(map,'k'))
209    SetImageColorspace(original,CMYKColorspace);
210
211  /*
212   * Obtain original image size if format requires it
213   */
214  rows    = original->rows;
215  columns = original->columns;
216
217  /*
218   * Save image to array
219   */
220  pixels_size=quantum_size*strlen(map)*rows*columns;
221  pixels=AcquireQuantumMemory(pixels_size,sizeof(unsigned char));
222  if( !pixels )
223    {
224      (void) printf ( "Failed to allocate memory for pixels\n");
225      exit_status = 1;
226      goto program_exit;
227    }
228  memset((void *) pixels, 0, pixels_size);
229
230  (void) LogMagickEvent(CoderEvent,GetMagickModule(),
231                        "Writing image to pixel array");
232  if( !ExportImagePixels(original,0,0,original->columns,original->rows,map,
233                     storage_type,pixels,&exception) )
234    {
235      (void) printf ( "ExportImagePixels returned error status\n");
236      if (exception.severity >= ErrorException)
237        CatchException(&exception);
238      exit_status = 1;
239      goto program_exit;
240    }
241
242   original =DestroyImageList( original );
243  original = (Image*)NULL;
244
245  /*
246   * Read image back from pixel array
247   */
248  original = AcquireImage((ImageInfo *) NULL);
249  SetImageExtent(original,columns,rows);
250  if (strchr(map,'c') || strchr(map,'C') || strchr(map,'m') || strchr(map,'M') ||
251      strchr(map,'y') || strchr(map,'y') || strchr(map,'k') || strchr(map,'k'))
252    original->colorspace=CMYKColorspace;
253  (void) SetImageBackgroundColor(original);
254
255  status = ImportImagePixels(original,0,0,columns,rows,map,storage_type,pixels);
256  if ( status == MagickFalse)
257    {
258      (void) printf ( "Failed to read image from pixels array\n" );
259      if (exception.severity >= ErrorException)
260        CatchException(&exception);
261      exit_status = 1;
262      goto program_exit;
263    }
264
265  /*
266   * Save image to pixel array
267   */
268  memset((void *) pixels, 0, pixels_size);
269  (void) LogMagickEvent(CoderEvent,GetMagickModule(),
270                        "Writing image to pixel array");
271  if( !ExportImagePixels(original,0,0,original->columns,original->rows,map,
272                     storage_type,pixels,&exception) )
273    {
274      (void) printf ( "ExportImagePixels returned error status\n" );
275      if (exception.severity >= ErrorException)
276        CatchException(&exception);
277      exit_status = 1;
278      goto program_exit;
279    }
280
281  /*
282   * Read image back from pixel array
283   */
284  (void) LogMagickEvent(CoderEvent,GetMagickModule(),
285                        "Reading image from pixel array");
286  final = AcquireImage((ImageInfo *) NULL);
287  SetImageExtent(final,columns,rows);
288  (void) SetImageBackgroundColor(final);
289  status = ImportImagePixels(final,0,0,columns,rows,map,storage_type,pixels);
290  if ( status == MagickFalse)
291    {
292      (void) printf ( "Failed to read image from pixels array\n" );
293      if (exception.severity >= ErrorException)
294        CatchException(&exception);
295      exit_status = 1;
296      goto program_exit;
297    }
298
299  pixels=(void *) RelinquishMagickMemory(pixels);
300
301  /*
302   * Check final output
303   */
304
305  if (original->colorspace != final->colorspace)
306    {
307      (void) printf("Original colorspace (%s) != final colorspace (%s)\n",
308       MagickOptionToMnemonic(MagickColorspaceOptions,original->colorspace),
309       MagickOptionToMnemonic(MagickColorspaceOptions,final->colorspace));
310      exit_status = 1;
311      goto program_exit;
312    }
313 
314  if ( !IsImagesEqual(original, final) &&
315      (original->error.normalized_mean_error > fuzz_factor) )
316    {
317      CatchException(&original->exception);
318      CatchException(&final->exception);
319      (void) printf( "Constitute check failed: %u/%g/%g\n",
320             (unsigned int) original->error.mean_error_per_pixel,
321             original->error.normalized_mean_error,
322             original->error.normalized_maximum_error);
323      exit_status = 1;
324      goto program_exit;
325    }
326
327  /* DisplayImages( imageInfo, final ); */
328
329 program_exit:
330  (void) fflush(stdout);
331  if (original)
332     original =DestroyImageList( original );
333  original = 0;
334  if (final)
335    {
336      if (getenv("SHOW_RESULT") != 0)
337                          DisplayImages( imageInfo, final );
338       final =DestroyImageList( final );
339    }
340  final = 0;
341  if (pixels)
342    pixels=(void *) RelinquishMagickMemory(pixels);
343  if (imageInfo)
344    imageInfo=DestroyImageInfo(imageInfo);
345  imageInfo = 0;
346
347  MagickCoreTerminus();
348
349  if (pause)
350    (void) getc(stdin);
351  return exit_status;
352}
Note: See TracBrowser for help on using the browser.