1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stddef.h>
4 #include <magick/api.h>
5 
6 #include "loader.h"
7 #include "viewer.h"
8 
9 extern char *binary;
10 
11 static int first = 1;
12 static ExceptionInfo exception;
13 
14 struct magick_state {
15     ImageInfo *image_info;
16     Image *image;
17 };
18 
19 static void*
magick_init(FILE * fp,char * filename,int * width,int * height)20 magick_init(FILE *fp, char *filename, int *width, int *height)
21 {
22     struct magick_state *h;
23 
24     /* libmagick wants a filename */
25     fclose(fp);
26 
27     if (first) {
28 	/* init library first time */
29 	MagickIncarnate(binary);
30 	GetExceptionInfo(&exception);
31 	first = 0;
32     }
33 
34     h = malloc(sizeof(*h));
35     memset(h,0,sizeof(*h));
36 
37     h->image_info=CloneImageInfo(NULL);
38     strcpy(h->image_info->filename,filename);
39     h->image = ReadImage(h->image_info,&exception);
40     if (NULL == h->image) {
41 	MagickError(exception.severity,exception.reason,exception.description);
42 	goto oops;
43     }
44 
45     *width  = h->image->rows;
46     *height = h->image->columns;
47     return h;
48 
49  oops:
50     if (h->image)
51 	DestroyImage(h->image);
52     if (h->image_info)
53 	DestroyImageInfo(h->image_info);
54     free(h);
55     return NULL;
56 }
57 
58 static void
magick_read(unsigned char * dst,int line,void * data)59 magick_read(unsigned char *dst, int line, void *data)
60 {
61     struct magick_state *h = data;
62     DispatchImage (h->image,0,line,h->image->columns, 1,
63 		   "RGB", 0, data);
64 }
65 
66 static void
magick_done(void * data)67 magick_done(void *data)
68 {
69     struct magick_state *h = data;
70 
71     DestroyImageInfo(h->image_info);
72     DestroyImage(h->image);
73     free(h);
74 }
75 
76 static struct ida_loader magick_loader = {
77     name:  "libmagick",
78     init:  magick_init,
79     read:  magick_read,
80     done:  magick_done,
81 };
82 
init_rd(void)83 static void __init init_rd(void)
84 {
85     load_register(&magick_loader);
86 }
87