1 /* input-magick.c: import files via image magick
2 
3    Copyright (C) 1999, 2000, 2001 Martin Weber.
4 
5    This library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public License
7    as published by the Free Software Foundation; either version 2.1 of
8    the License, or (at your option) any later version.
9 
10    This library is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14 
15    You should have received a copy of the GNU Lesser General Public
16    License along with this library; if not, write to the Free Software
17    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18    USA. */
19 
20 /* This code was tested with ImageMagick 5.2.1-5.5.2
21    it doesn't work with earlier versions */
22 
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif /* Def: HAVE_CONFIG_H */
26 
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/types.h> /* Needed for correct interpretation of magick/api.h */
31 #include <magick/api.h>
32 #include "input-magick.h"
33 #include "bitmap.h"
34 
input_magick_reader(at_string filename,at_input_opts_type * opts,at_msg_func msg_func,at_address msg_data)35 at_bitmap_type input_magick_reader(at_string filename,
36 				   at_input_opts_type * opts,
37 				   at_msg_func msg_func,
38 				   at_address msg_data)
39 {
40   Image *image = NULL;
41   ImageInfo *image_info;
42   ImageType image_type;
43   unsigned int i,j,point,np,runcount;
44   at_bitmap_type bitmap;
45   PixelPacket p;
46   PixelPacket *pixel=&p;
47   ExceptionInfo exception;
48 #if (MagickLibVersion < 0x0538)
49   MagickIncarnate("");
50 #else
51   InitializeMagick("");
52 #endif
53   GetExceptionInfo(&exception);
54   image_info=CloneImageInfo((ImageInfo *) NULL);
55   (void) strcpy(image_info->filename,filename);
56   image_info->antialias = 0;
57 
58   image=ReadImage(image_info,&exception);
59   if (image == (Image *) NULL) {
60 #if (MagickLibVersion <= 0x0525)
61     /* MagickError(exception.severity,exception.message,exception.qualifier); */
62     if (msg_func)
63       msg_func (exception.qualifier, AT_MSG_FATAL, msg_data);
64     goto cleanup;
65 #else
66     /* MagickError(exception.severity,exception.reason,exception.description); */
67     if (msg_func)
68       msg_func (exception.reason, AT_MSG_FATAL, msg_data);
69     goto cleanup;
70 #endif
71   }
72 #if (MagickLibVersion < 0x0540)
73   image_type=GetImageType(image);
74 #else
75   image_type=GetImageType(image, &exception);
76 #endif
77   if(image_type == BilevelType || image_type == GrayscaleType)
78     np=1;
79   else
80     np=3;
81 
82   bitmap = at_bitmap_init(NULL, image->columns, image->rows, np);
83 
84   for(j=0,runcount=0,point=0;j<image->rows;j++)
85     for(i=0;i<image->columns;i++) {
86       p=GetOnePixel(image,i,j);
87       AT_BITMAP_BITS(bitmap)[point++]=pixel->red; /* if gray: red=green=blue */
88       if(np==3) {
89         AT_BITMAP_BITS(bitmap)[point++]=pixel->green;
90         AT_BITMAP_BITS(bitmap)[point++]=pixel->blue;
91       }
92     }
93 
94   DestroyImage(image);
95  cleanup:
96   DestroyImageInfo(image_info);
97   return(bitmap);
98 }
99