summaryrefslogtreecommitdiff
path: root/app/xlockmore/xlock/ras.c
blob: 87b080ac3f874f8b9c5fb2030be5acc794c4429a (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
#if !defined( lint ) && !defined( SABER )
static const char sccsid[] = "@(#)ras.c	4.00 97/01/01 xlockmore";

#endif

/*-
 * Utilities for Sun rasterfile processing
 *
 * Copyright (c) 1995 by Tobias Gloth
 *
 * See xlock.c for copying information.
 *
 * Revision History:
 *  5-Apr-06: Correction for TrueColor.
 *  3-Mar-96: Added random image selection.
 * 12-Dec-95: Modified to be a used in more than one mode
 *            <joukj@hrem.stm.tudelft.nl>
 * 22-May-95: Written.
 */

#include "xlock.h"
#include "iostuff.h"
#include "ras.h"
#include <time.h>

XLockImage xlockimage;

static unsigned long get_long(int n);

static void
analyze_header(void)
{
	xlockimage.sign = get_long(0);
	xlockimage.width = get_long(1);
	xlockimage.height = get_long(2);
	xlockimage.depth = get_long(3);
	xlockimage.colors = get_long(7) / 3;
}

static unsigned long
get_long(int n)
{
	return
		(((unsigned long) xlockimage.header[4 * n + 0]) << 24) +
		(((unsigned long) xlockimage.header[4 * n + 1]) << 16) +
		(((unsigned long) xlockimage.header[4 * n + 2]) << 8) +
		(((unsigned long) xlockimage.header[4 * n + 3]) << 0);
}

int
RasterFileToImage(ModeInfo * mi, char *filename, XImage ** image ,
		  Colormap m_cmap )
{
	int         read_width , depth , format , i;
	FILE       *file;
   unsigned char* rasdata;
   unsigned char* tmpdata;
   unsigned long black, white, fgpix, bgpix;
   XColor      fgcol, bgcol;
   unsigned long* pixel_num;
   int x , y;

	if ((file = my_fopen(filename, "r")) == NULL) {
		/*(void) fprintf(stderr, "could not read file \"%s\"\n", filename); */
		return RasterOpenFailed;
	}
	(void) fread((void *) xlockimage.header, 8, 4, file);
	analyze_header();
	if (xlockimage.sign != 0x59a66a95) {
		/* not a raster file */
		(void) fclose(file);
		return RasterFileInvalid;
	}
	if (xlockimage.depth != 8) {
		(void) fclose(file);
		(void) fprintf(stderr, "only 8-bit Raster files are supported\n");
		return RasterColorFailed;
	}
	read_width = (int) xlockimage.width;
	if ((xlockimage.width & 1) != 0)
		read_width++;
   
   depth = MI_DEPTH(mi);
   rasdata = (unsigned char *) malloc((int) (read_width * xlockimage.height));
   if ( depth < 9 )
     xlockimage.data = (unsigned char *) malloc((int) ( read_width *
							xlockimage.height));
   else
     xlockimage.data = (unsigned char *) malloc((int) ( read_width *
							xlockimage.height *
							4 ));
	if (!xlockimage.data) {
		(void) fclose(file);
		(void) fprintf(stderr, "out of memory for Raster file\n");
		return RasterNoMemory;
	}
   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) fclose(file);
		(void) fprintf(stderr, "could not create image from Raster file\n");
		return RasterColorError;
	}
	(void) fread((void *) xlockimage.color, (int) xlockimage.colors, 3, file);
	(void) fread((void *) rasdata, read_width, (int) xlockimage.height, file);
	(void) fclose(file);

   /*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.color[fgpix] = fgcol.red >> 8;
		xlockimage.color[fgpix+xlockimage.colors] = fgcol.green >> 8;
		xlockimage.color[fgpix+2*xlockimage.colors] = fgcol.blue >> 8;
	}
	if (bgpix < COLORMAP_SIZE) {
		xlockimage.color[bgpix] = bgcol.red >> 8;
		xlockimage.color[bgpix+xlockimage.colors] = bgcol.green >> 8;
		xlockimage.color[bgpix+2*xlockimage.colors] = bgcol.blue >> 8;
	}
	if (white < COLORMAP_SIZE) {
		xlockimage.color[white] = 0xFF;
		xlockimage.color[white+xlockimage.colors] = 0xFF;
		xlockimage.color[white+2*xlockimage.colors] = 0xFF;
	}
	if (black < COLORMAP_SIZE) {
		xlockimage.color[black] = 0;
		xlockimage.color[black+xlockimage.colors] = 0;
		xlockimage.color[black+2*xlockimage.colors] = 0;
	}
   /* supply data and colours*/
	pixel_num = malloc( xlockimage.colors * sizeof( unsigned long ) );
	for ( i=0 ; i<xlockimage.colors ; i++ )
	  {
	     XColor Xcol;
	     Xcol.red = xlockimage.color[ i ]*257;
	     Xcol.green = xlockimage.color[ i + xlockimage.colors ]*257;
	     Xcol.blue = xlockimage.color[ i + 2 * xlockimage.colors ]*257;
	     XAllocColor( MI_DISPLAY(mi) , m_cmap , &Xcol);
	     pixel_num[i] = Xcol.pixel;
	  }

   tmpdata = rasdata;
   for ( y=0; y<xlockimage.height; y++ )
     {
	for ( x=0; x<xlockimage.width; x++)
	  {
	     XPutPixel( *image , x , y , pixel_num[ tmpdata[ 0 ] ] );
	     tmpdata++;
	  }
     }
   free( rasdata );
   free( pixel_num );
   
   return RasterSuccess;
}