summaryrefslogtreecommitdiffstats
path: root/kernel/kls_ras/fmt_codec_ras.cpp
blob: c932d9b67342290550e162f0930dd30433460054 (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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*  This file is part of ksquirrel-libs (http://ksquirrel.sf.net)

    Copyright (c) 2004 Dmitry Baryshev <[email protected]>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation;
    either version 2 of the License, or (at your option) any later
    version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    as32 with this library; see the file COPYING.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "ksquirrel-libs/fmt_types.h"
#include "ksquirrel-libs/fileio.h"
#include "ksquirrel-libs/error.h"
#include "ksquirrel-libs/fmt_utils.h"

#include "fmt_codec_ras_defs.h"
#include "fmt_codec_ras.h"

#include "../xpm/codec_ras.xpm"

/*
 *
 * The Sun Raster image file format is the native bitmap format of the Sun
 * Microsystems UNIX platforms using the SunOS operating system. This format is
 * capable of storing black-and-white, gray-scale, and color bitmapped data of any
 * pixel depth. The use of color maps and a simple Run-Length data compression
 * are also supported. Typically, most images found on a SunOS system are Sun
 * Raster images, and this format is supported by most UNIX imaging applications.
 *
 */

bool fmt_readdata(ifstreamK &, u8 *, u32, bool);

fmt_codec::fmt_codec() : fmt_codec_base()
{}

fmt_codec::~fmt_codec()
{}

void fmt_codec::options(codec_options *o)
{
    o->version = "0.6.3";
    o->name = "SUN Raster";
    o->filter = "*.ras ";
    o->config = "";
    o->mime = "\x0059\x00A6\x006A\x0095";
    o->mimetype = "image/x-ras";
    o->pixmap = codec_ras;
    o->readable = true;
    o->canbemultiple = false;
    o->writestatic = false;
    o->writeanimated = false;
    o->needtempfile = false;
}

s32 fmt_codec::read_init(const std::string &file)
{
    frs.open(file.c_str(), ios::binary | ios::in);

    if(!frs.good())
        return SQE_R_NOFILE;

    currentImage = -1;
    rle = false;
    isRGB = false;
    buf = NULL;

    finfo.animated = false;

    return SQE_OK;
}

s32 fmt_codec::read_next()
{
    currentImage++;
    
    if(currentImage)
	return SQE_NOTOK;
	    
    fmt_image image;

    if(!frs.be_getlong(&rfh.ras_magic)) return SQE_R_BADFILE;
    if(!frs.be_getlong(&rfh.ras_width)) return SQE_R_BADFILE;
    if(!frs.be_getlong(&rfh.ras_height)) return SQE_R_BADFILE;
    if(!frs.be_getlong(&rfh.ras_depth)) return SQE_R_BADFILE;
    if(!frs.be_getlong(&rfh.ras_length)) return SQE_R_BADFILE;
    if(!frs.be_getlong(&rfh.ras_type)) return SQE_R_BADFILE;
    if(!frs.be_getlong(&rfh.ras_maptype)) return SQE_R_BADFILE;
    if(!frs.be_getlong(&rfh.ras_maplength)) return SQE_R_BADFILE;

    if(rfh.ras_magic != RAS_MAGIC) return SQE_R_BADFILE;

    if(rfh.ras_type != RAS_OLD && rfh.ras_type != RAS_STANDARD && rfh.ras_type != RAS_BYTE_ENCODED && rfh.ras_type != RAS_RGB &&
	    rfh.ras_type != RAS_TIFF && rfh.ras_type != RAS_IFF &&  rfh.ras_type != RAS_EXPERIMENTAL)
	return SQE_R_BADFILE;
    else if(rfh.ras_type == RAS_EXPERIMENTAL)
	return SQE_R_NOTSUPPORTED;

    image.w = rfh.ras_width;
    image.h = rfh.ras_height;
    image.bpp = rfh.ras_depth;

    switch(rfh.ras_maptype)
    {
	case RMT_NONE :
	{
		if (rfh.ras_depth < 24)
		{
		    s32 numcolors = 1 << rfh.ras_depth, i;

		    for (i = 0; i < numcolors; i++)
		    {
			pal[i].r = (255 * i) / (numcolors - 1);
			pal[i].g = (255 * i) / (numcolors - 1);
			pal[i].b = (255 * i) / (numcolors - 1);
		    }
		}

	break;
	}

	case RMT_EQUAL_RGB:
	{
		s8 *g, *b;

		s32 numcolors = 1 << rfh.ras_depth;

		s8 r[3 * numcolors];

		g = r + numcolors;
		b = g + numcolors;

		if(!frs.readK(r, 3 * numcolors)) return SQE_R_BADFILE;

		for(s32 i = 0; i < numcolors; i++)
		{
			pal[i].r = r[i];
			pal[i].g = g[i];
			pal[i].b = b[i];
		}
	break;
	}

	case RMT_RAW:
	{
		s8 colormap[rfh.ras_maplength];

		if(!frs.readK(colormap, rfh.ras_maplength)) return SQE_R_BADFILE;
	break;
	}
    }

    switch(rfh.ras_type)
    {
	case RAS_OLD:
	case RAS_STANDARD:
	case RAS_TIFF:
	case RAS_IFF:
	break;

	case RAS_BYTE_ENCODED:
	    rle = true;
	break;

	case RAS_RGB:
	    isRGB = true;
	break;
    }
    
    if(rfh.ras_depth == 1)
	linelength = (short)((rfh.ras_width / 8) + (rfh.ras_width % 8 ? 1 : 0));
    else
	linelength = (short)rfh.ras_width;

    fill = (linelength % 2) ? 1 : 0;

    buf = new u8 [rfh.ras_width * sizeof(RGB)];

    if(!buf)
	return SQE_R_NOMEMORY;

    image.compression = ((isRGB) ? "-":"RLE");
    image.colorspace = "RGB";

    finfo.image.push_back(image);

    return SQE_OK;
}

s32 fmt_codec::read_next_pass()
{
    return SQE_OK;
}
    
s32 fmt_codec::read_scanline(RGBA *scan)
{
    u32 i;
    fmt_image *im = image(currentImage);
    fmt_utils::fillAlpha(scan, im->w);

    switch(im->bpp)
    {
    	case 1:
	break;

	case 8:
		if(!fmt_readdata(frs, buf, linelength, rle))
		    return SQE_R_BADFILE;

		for(i = 0;i < rfh.ras_width;i++)
		    memcpy(scan+i, &pal[i], sizeof(RGB));

		if(fill)
		{
		    if(!fmt_readdata(frs, &fillchar, fill, rle))
			return SQE_R_BADFILE;
		}
    	break;

	case 24:
	{
	    u8 *b = buf;
	    
	    if(!fmt_readdata(frs, buf, rfh.ras_width * 3, rle))
		return SQE_R_BADFILE;

	    if(isRGB)
	        for (i = 0; i < rfh.ras_width; i++)
	        {
	    	    scan[i].r = *b;
    	    	    scan[i].g = *(b+1);
		    scan[i].b = *(b+2);
		    b += 3;
		}
	    else
	        for (i = 0; i < rfh.ras_width; i++)
	        {
		    scan[i].r = *(b + 2);
		    scan[i].g = *(b + 1);
		    scan[i].b = *b;
		    b += 3;
		}
		
	    if(fill)
	    {
	        if(!fmt_readdata(frs, &fillchar, fill, rle))
		    return SQE_R_BADFILE;
	    }
	}	
	break;

	case 32:
	{
	    u8 *b = buf;
	    
	    if(!fmt_readdata(frs, buf, rfh.ras_width * 4, rle))
		return SQE_R_BADFILE;

	    if(isRGB)
	        for (i = 0; i < rfh.ras_width; i++)
	        {
	    	    scan[i].a = *b;
	    	    scan[i].r = *(b+1);
    	    	    scan[i].g = *(b+2);
		    scan[i].b = *(b+3);
		    b += 4;
		}
	    else
	        for (i = 0; i < rfh.ras_width; i++)
	        {
		    scan[i].r = *(b + 3);
		    scan[i].g = *(b + 2);
		    scan[i].b = *(b + 1);
		    scan[i].a = *b;
		    b += 4;
		}
		
	    if(fill)
	    {
	        if(!fmt_readdata(frs, &fillchar, fill, rle))
		    return SQE_R_BADFILE;
	    }

	}
	break;
    }

    return SQE_OK;
}

void fmt_codec::read_close()
{
    frs.close();

    delete [] buf;
    buf = NULL;

    finfo.meta.clear();
    finfo.image.clear();
}

bool fmt_readdata(ifstreamK &ff, u8 *_buf, u32 length, bool rle)
{
    u8 repchar, remaining = 0;

    if(rle)
    {
	while(length--)
	{
		if (remaining)
		{
    		    remaining--;
		    *(_buf++)= repchar;
		}
		else
		{
			if(!ff.readK(&repchar, 1)) return false;

			if(repchar == RESC)
			{
				if(!ff.readK(&remaining, 1)) return false;

				if (remaining == 0)
					*(_buf++) = RESC;
				else
				{
					if(!ff.readK(&repchar, 1)) return false;
					*(_buf++) = repchar;
				}
			}
			else
				*(_buf++) = repchar;
		}
	}
    }
    else
    {
	if(!ff.readK(_buf, length)) return false;
	
    }

    return true;
}

#include "fmt_codec_cd_func.h"