1 #if 0
2 static const char sccsid[] = "@(#)ras.c 4.00 97/01/01 xlockmore";
3
4 #endif
5
6 /*-
7 * Utilities for Sun rasterfile processing
8 *
9 * Copyright (c) 1995 by Tobias Gloth
10 *
11 * See xlock.c for copying information.
12 *
13 * Revision History:
14 * 5-Apr-06: Correction for TrueColor.
15 * 3-Mar-96: Added random image selection.
16 * 12-Dec-95: Modified to be a used in more than one mode
17 * <joukj AT hrem.nano.tudelft.nl>
18 * 22-May-95: Written.
19 */
20
21 #include "xlock.h"
22 #include "iostuff.h"
23 #include "ras.h"
24 #include <time.h>
25
26 XLockImage xlockimage;
27
28 static unsigned long get_long(int n);
29
30 static void
analyze_header(void)31 analyze_header(void)
32 {
33 xlockimage.sign = get_long(0);
34 xlockimage.width = get_long(1);
35 xlockimage.height = get_long(2);
36 xlockimage.depth = get_long(3);
37 xlockimage.colors = get_long(7) / 3;
38 }
39
40 static unsigned long
get_long(int n)41 get_long(int n)
42 {
43 return
44 (((unsigned long) xlockimage.header[4 * n + 0]) << 24) +
45 (((unsigned long) xlockimage.header[4 * n + 1]) << 16) +
46 (((unsigned long) xlockimage.header[4 * n + 2]) << 8) +
47 (((unsigned long) xlockimage.header[4 * n + 3]) << 0);
48 }
49
50 int
RasterFileToImage(ModeInfo * mi,char * filename,XImage ** image,Colormap m_cmap)51 RasterFileToImage(ModeInfo * mi, char *filename, XImage ** image ,
52 Colormap m_cmap )
53 {
54 int read_width , depth , format , i;
55 FILE *file;
56 unsigned char* rasdata;
57 unsigned char* tmpdata;
58 unsigned long black, white, fgpix, bgpix;
59 XColor fgcol, bgcol;
60 unsigned long* pixel_num;
61 int x , y;
62
63 if ((file = my_fopen(filename, "r")) == NULL) {
64 /*(void) fprintf(stderr, "could not read file \"%s\"\n", filename); */
65 return RasterOpenFailed;
66 }
67 (void) fread((void *) xlockimage.header, 8, 4, file);
68 analyze_header();
69 if (xlockimage.sign != 0x59a66a95) {
70 /* not a raster file */
71 (void) fclose(file);
72 return RasterFileInvalid;
73 }
74 if (xlockimage.depth != 8) {
75 (void) fclose(file);
76 (void) fprintf(stderr, "only 8-bit Raster files are supported\n");
77 return RasterColorFailed;
78 }
79 read_width = (int) xlockimage.width;
80 if ((xlockimage.width & 1) != 0)
81 read_width++;
82
83 depth = MI_DEPTH(mi);
84 rasdata = (unsigned char *) malloc((int) (read_width * xlockimage.height));
85 if ( depth < 9 )
86 xlockimage.data = (unsigned char *) malloc((int) ( read_width *
87 xlockimage.height));
88 else
89 xlockimage.data = (unsigned char *) malloc((int) ( read_width *
90 xlockimage.height *
91 4 ));
92 if (!xlockimage.data) {
93 (void) fclose(file);
94 (void) fprintf(stderr, "out of memory for Raster file\n");
95 free( rasdata );
96 return RasterNoMemory;
97 }
98 format=(depth == 1) ? XYBitmap : ZPixmap;
99 if ( MI_IS_VERBOSE( mi ) )
100 (void) printf ( "XCreateImage depth = %d\n", depth );
101 if ( depth < 9 )
102 *image = XCreateImage( MI_DISPLAY(mi), MI_VISUAL(mi), depth, format, 0,
103 (char *) xlockimage.data, (int) xlockimage.width,
104 (int) xlockimage.height, 16, (int) xlockimage.width );
105 else
106 *image = XCreateImage( MI_DISPLAY(mi), MI_VISUAL(mi), depth, format, 0,
107 (char *) xlockimage.data, (int) xlockimage.width,
108 (int) xlockimage.height, 16, (int) xlockimage.width*4 );
109 if (!*image) {
110 (void) fclose(file);
111 (void) fprintf(stderr, "could not create image from Raster file\n");
112 free( rasdata );
113 return RasterColorError;
114 }
115 (void) fread((void *) xlockimage.color, (int) xlockimage.colors, 3, file);
116 (void) fread((void *) rasdata, read_width, (int) xlockimage.height, file);
117 (void) fclose(file);
118
119 /*set up colourmap */
120 black = MI_BLACK_PIXEL(mi);
121 white = MI_WHITE_PIXEL(mi);
122 fgpix = MI_FG_PIXEL(mi);
123 bgpix = MI_BG_PIXEL(mi);
124 fgcol.pixel = fgpix;
125 bgcol.pixel = bgpix;
126 XQueryColor(MI_DISPLAY(mi), MI_COLORMAP(mi), &fgcol);
127 XQueryColor(MI_DISPLAY(mi), MI_COLORMAP(mi), &bgcol);
128
129 /* Set these, if Image does not overwrite some, so much the better. */
130 if (fgpix < COLORMAP_SIZE) {
131 xlockimage.color[fgpix] = fgcol.red >> 8;
132 xlockimage.color[fgpix+xlockimage.colors] = fgcol.green >> 8;
133 xlockimage.color[fgpix+2*xlockimage.colors] = fgcol.blue >> 8;
134 }
135 if (bgpix < COLORMAP_SIZE) {
136 xlockimage.color[bgpix] = bgcol.red >> 8;
137 xlockimage.color[bgpix+xlockimage.colors] = bgcol.green >> 8;
138 xlockimage.color[bgpix+2*xlockimage.colors] = bgcol.blue >> 8;
139 }
140 if (white < COLORMAP_SIZE) {
141 xlockimage.color[white] = 0xFF;
142 xlockimage.color[white+xlockimage.colors] = 0xFF;
143 xlockimage.color[white+2*xlockimage.colors] = 0xFF;
144 }
145 if (black < COLORMAP_SIZE) {
146 xlockimage.color[black] = 0;
147 xlockimage.color[black+xlockimage.colors] = 0;
148 xlockimage.color[black+2*xlockimage.colors] = 0;
149 }
150 /* supply data and colours*/
151 pixel_num = (unsigned long *) malloc((int) (xlockimage.colors * sizeof( unsigned long )) );
152 for ( i=0 ; i<xlockimage.colors ; i++ )
153 {
154 XColor Xcol;
155 Xcol.red = xlockimage.color[ i ]*257;
156 Xcol.green = xlockimage.color[ i + xlockimage.colors ]*257;
157 Xcol.blue = xlockimage.color[ i + 2 * xlockimage.colors ]*257;
158 XAllocColor( MI_DISPLAY(mi) , m_cmap , &Xcol);
159 pixel_num[i] = Xcol.pixel;
160 }
161
162 tmpdata = rasdata;
163 for ( y=0; y<xlockimage.height; y++ )
164 {
165 for ( x=0; x<xlockimage.width; x++)
166 {
167 XPutPixel( *image , x , y , pixel_num[ tmpdata[ 0 ] ] );
168 tmpdata++;
169 }
170 }
171 free( rasdata );
172 free( pixel_num );
173
174 return RasterSuccess;
175 }
176