summaryrefslogtreecommitdiff
path: root/app/xlockmore/xlock/magick.c
blob: 763045e7bfda776b493a964f73fc7ff461a0fa75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#if !defined( lint ) && !defined( SABER )
static const char sccsid[] = "@(#)magick.c     5.00 00/10/26 xlockmore";

#endif

/*-
 * Utilities for Reading images using ImageMagick
 *
 * Copyright (c) 2000 by J.Jansen (joukj@hrem.nano.tudelft.nl)
 *
 * See xlock.c for copying information.
 *
 * Revision History:
 * 26-Oct-00: Created
 * 15-Mar-06: Corrected for other QuantumDepth than 8
 * 04-Apr-06: Corrected for TrueColor visuals
 */

#include "xlock.h"

#ifdef USE_MAGICK
#undef inline
#include "magick.h"

XLockImage xlockimage;

int
MagickFileToImage(ModeInfo * mi, char *filename, XImage ** image ,
		  Colormap m_cmap )
{
   Image* imageData = (Image*) NULL;
   ImageInfo imageInfo;
   ExceptionInfo exception;
   PixelPacket* pixdata;
   IndexPacket* tmpdata0;
   int i, fuzz, depth, format;
   unsigned long black, white, fgpix, bgpix;
   XColor      fgcol, bgcol;
   unsigned long* pixel_num;

   if ( MI_IS_VERBOSE( mi ) )
     {
	(void) printf ( "Reading image %s\n", filename );
     }

   /* Read image data from file */
/* PURIFY reports a memory leak on the next line */
   GetImageInfo( &imageInfo );
   imageInfo.dither = 0;
   (void) strcpy( imageInfo.filename , filename );
   GetExceptionInfo( &exception );
   imageData = ReadImage ( &imageInfo, &exception );
   if ( imageData == (Image*) NULL )
     {
	(void) fprintf ( stderr , "Error reading image %s\n", imageInfo.filename );
	return MagickFileInvalid;
     }

   /* setup X-image */
   xlockimage.width = imageData->columns;
   xlockimage.height = imageData->rows;
   xlockimage.depth = imageData->depth;
   if ( MI_IS_VERBOSE( mi ) )
     {
	(void) printf ( "Image dimensions %d x %d, depth = %d\n",
		       xlockimage.width, xlockimage.height, xlockimage.depth );
     }

   depth = MI_DEPTH(mi);
   if ( depth < 9 )
     xlockimage.data = (unsigned char *) malloc((int) ( xlockimage.width *
							xlockimage.height));
   else
     xlockimage.data = (unsigned char *) malloc((int) ( xlockimage.width *
							xlockimage.height *
							4 ));
   if (!xlockimage.data)
     {
	(void) fprintf(stderr, "out of memory for Image file\n");
	return MagickNoMemory;
     }

   format=(depth == 1) ? XYBitmap : ZPixmap;
   if ( MI_IS_VERBOSE( mi ) )
	(void) printf ( "XCreateImage depth = %d\n", depth );
   if ( depth < 9 )
     *image = XCreateImage( MI_DISPLAY(mi), MI_VISUAL(mi), depth, format, 0,
			 (char *) xlockimage.data, (int) xlockimage.width,
			 (int) xlockimage.height, 16, (int) xlockimage.width );
   else
     *image = XCreateImage( MI_DISPLAY(mi), MI_VISUAL(mi), depth, format, 0,
			 (char *) xlockimage.data, (int) xlockimage.width,
			 (int) xlockimage.height, 16, (int) xlockimage.width*4 );
     
   if (!*image)
     {
	(void) fprintf(stderr, "could not create image from file\n");
	return MagickColorError;
     }

   if ( imageData->colors == 0 )
     {
	PixelPacket* pixtmp;

/* PURIFY reports a memory leak on the next line */
	AllocateImageColormap ( imageData , COLORMAP_SIZE );
	pixdata = GetImagePixels( imageData , 0 , 0 , imageData->columns ,
					     imageData->rows );
	imageData->colors = imageData->columns*imageData->rows;
	tmpdata0 = (IndexPacket *) malloc((int) ( xlockimage.width *
						  xlockimage.height *
						  sizeof(IndexPacket)));

#if (QuantumDepth == 8)
	for ( fuzz=0; imageData->colors > COLORMAP_SIZE; fuzz++ )
#elif (QuantumDepth == 16)
	for ( fuzz=0; imageData->colors > COLORMAP_SIZE; fuzz+=256 )
#elif (QuantumDepth == 32)
	for ( fuzz=0; imageData->colors > COLORMAP_SIZE; fuzz+=65536 )
#elif (QuantumDepth == 64)
	for ( fuzz=0; imageData->colors > COLORMAP_SIZE; fuzz+=4294967296 )
#else
#error "ImageMagick configuration error"
#endif
	  {
	     int num_col , j , x , y;

	     pixtmp = pixdata;
	     num_col = 0;
	     for ( i=0; i<imageData->columns*imageData->rows; i++)
	       {
		  PixelPacket* pixtmp2;

		  pixtmp2 = imageData->colormap;
		  for ( j=0; j<num_col; j++ )
		    {
		       if ( ABS( pixtmp->red - pixtmp2->red ) +
			 ABS( pixtmp->green - pixtmp2->green ) +
			 ABS( pixtmp->blue - pixtmp2->blue ) < fuzz )
			 break;
		       pixtmp2++;
		    }
		  if ( j == num_col )
		    {
		       pixtmp2->red = pixtmp->red;
		       pixtmp2->green = pixtmp->green;
		       pixtmp2->blue = pixtmp->blue;
		       num_col++;
		       if ( num_col == COLORMAP_SIZE )
			 {
			    num_col++;
			    break;
			 }
		    }
		  tmpdata0[ i ] = j;
		  pixtmp++;
	       }
	     imageData->colors = num_col;
	  }
     }
   else
     {
	if ( imageData->colors > COLORMAP_SIZE )
	  {
	     imageData->colors = COLORMAP_SIZE;
	     (void) fprintf( stderr ,
			    "wrong colour reducing algorithm used\n" );
	     (void) fprintf( stderr ,
	     "Please notify the maintainer that this statement is reached\n" );
	  }
   /* get pixel information */
   if ( !( pixdata = GetImagePixels( imageData , 0 , 0 , imageData->columns ,
				    imageData->rows ) ) )
     {
	(void) fprintf( stderr , "Error getting pixels\n" );
	return MagickFileInvalid;
     }
	tmpdata0 = GetIndexes ( imageData );
     }

   /*set up colourmap */
	black = MI_BLACK_PIXEL(mi);
	white = MI_WHITE_PIXEL(mi);
	fgpix = MI_FG_PIXEL(mi);
	bgpix = MI_BG_PIXEL(mi);
	fgcol.pixel = fgpix;
	bgcol.pixel = bgpix;
	XQueryColor(MI_DISPLAY(mi), MI_COLORMAP(mi), &fgcol);
	XQueryColor(MI_DISPLAY(mi), MI_COLORMAP(mi), &bgcol);

	/* Set these, if Image does not overwrite some, so much the better. */
	if (fgpix < COLORMAP_SIZE) {
		xlockimage.red[fgpix] = fgcol.red >> 8;
		xlockimage.green[fgpix] = fgcol.green >> 8;
		xlockimage.blue[fgpix] = fgcol.blue >> 8;
	}
	if (bgpix < COLORMAP_SIZE) {
		xlockimage.red[bgpix] = bgcol.red >> 8;
		xlockimage.green[bgpix] = bgcol.green >> 8;
		xlockimage.blue[bgpix] = bgcol.blue >> 8;
	}
	if (white < COLORMAP_SIZE) {
		xlockimage.red[white] = 0xFF;
		xlockimage.green[white] = 0xFF;
		xlockimage.blue[white] = 0xFF;
	}
	if (black < COLORMAP_SIZE) {
		xlockimage.red[black] = 0;
		xlockimage.green[black] = 0;
		xlockimage.blue[black] = 0;
	}
   /* supply data and colours*/
	xlockimage.colors = imageData->colors;
	pixel_num = malloc( imageData->colors * sizeof( unsigned long ) );
	for ( i=0 ; i<xlockimage.colors ; i++ )
	  {
	     XColor Xcol;
	     
#if (QuantumDepth == 8)
	     xlockimage.red[ i ] =  imageData->colormap->red * 257;
	     xlockimage.green[ i ] = imageData->colormap->green *257;
	     xlockimage.blue[ i ] = imageData->colormap->blue * 257;
#elif (QuantumDepth == 16)
	     xlockimage.red[ i ] =  imageData->colormap->red;
	     xlockimage.green[ i ] = imageData->colormap->green;
	     xlockimage.blue[ i ] = imageData->colormap->blue;
#elif (QuantumDepth == 32)
	     xlockimage.red[ i ] =  imageData->colormap->red / 256;
	     xlockimage.green[ i ] = imageData->colormap->green / 256;
	     xlockimage.blue[ i ] = imageData->colormap->blue/ 256;
#elif (QuantumDepth == 64)
	     xlockimage.red[ i ] =  imageData->colormap->red / 65537;
	     xlockimage.green[ i ] = imageData->colormap->green / 65537;
	     xlockimage.blue[ i ] = imageData->colormap->blue/ 65537;
#else
#error "ImageMagick configuration error"
#endif
	     Xcol.red = xlockimage.red[ i ];
	     Xcol.green = xlockimage.green[ i ];
	     Xcol.blue = xlockimage.blue[ i ];
	     XAllocColor( MI_DISPLAY(mi) , m_cmap , &Xcol);
	     pixel_num[i] = Xcol.pixel;
	     imageData->colormap++;
	  }

   if ( MI_IS_VERBOSE( mi ) )
     {
	(void) printf ( "xlockimage.colors %d\n", xlockimage.colors );
     }
   /* Make sure there is a black ... */
   if (xlockimage.colors <= 0xff)
     xlockimage.red[0xff] = xlockimage.green[0xff] = xlockimage.blue[0xff] = 0;

	  {
	     IndexPacket* tmpdata;
	     int x , y;

	     tmpdata = tmpdata0;
	     for ( y=0; y<imageData->rows; y++ )
	       {
		  for ( x=0; x<imageData->columns; x++)
	       {
		  XPutPixel( *image , x , y , pixel_num[ tmpdata[ 0 ] ] );
		  tmpdata++;
	       }
	       }
	     free( tmpdata0 );
	     free( pixel_num );
	  }

   /* clean up */
   DestroyImage( imageData );

   return MagickSuccess;
}

#endif /* USE_MAGICK */