source: trunk/src/testing/app/bitplay/libimg/rasdisp.c @ 4

Revision 4, 5.9 KB checked in by ajaworski, 13 years ago (diff)

Added modified SAGE sources

  • Property svn:executable set to *
Line 
1/******************************************************************************
2 * SAGE - Scalable Adaptive Graphics Environment
3 *
4 * Copyright (C) 2004 Electronic Visualization Laboratory,
5 * University of Illinois at Chicago
6 *
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 *
12 *  * Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 *  * Redistributions in binary form must reproduce the above
15 *    copyright notice, this list of conditions and the following disclaimer
16 *    in the documentation and/or other materials provided with the distribution.
17 *  * Neither the name of the University of Illinois at Chicago nor
18 *    the names of its contributors may be used to endorse or promote
19 *    products derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
25 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 * Direct questions, comments etc about SAGE to http://www.evl.uic.edu/cavern/forum/
34 *
35 *****************************************************************************/
36
37/*
38 * Given a Sun raster file, return an IMG containing its raster data.
39 */
40
41#include <stdio.h>
42#include <stdlib.h>
43#include "rasterfile.h"
44#include "imginfo.h"
45
46IMG *
47rasmakedisp(filename)
48    char *filename;
49{
50    FILE *inf;
51    struct rasterfile ras;
52    IMG *im = NULL;
53    int xsize, ysize;
54    register long *rp;
55    unsigned char *rowbuf = NULL;
56    long cmap[256];
57
58    inf = fopen(filename, "r");
59    if(inf == NULL) {
60        fprintf(stderr, "%s: cannot open: ", filename);
61        perror("");
62        return NULL;
63    }
64
65    if(fread(&ras, sizeof(ras), 1, inf) <= 0 || ras.ras_magic != RAS_MAGIC)
66        goto fail;
67
68    xsize = ras.ras_width;
69    ysize = ras.ras_height;
70
71    im = (IMG *)malloc(sizeof(IMG));
72    im->rowbytes = xsize * sizeof(long);
73    im->type = IT_LONG;
74    im->xsize = xsize;
75    im->ysize = ysize;
76    im->data = (unsigned char *)malloc(im->rowbytes * ysize);
77    if(im->data == NULL) {
78        fprintf(stderr, "Can't malloc %ld bytes of memory for image\n",
79            sizeof(long) * xsize * ysize);
80        exit(2);
81    }
82    rp = (long *)im->data;
83
84    switch(ras.ras_maptype) {
85        unsigned char map[768];
86        register int i;
87
88    case RMT_EQUAL_RGB:
89        if(ras.ras_maplength != 3*256) {
90            fprintf(stderr, "%s: can't handle colormap with %d != 256 entries",
91                filename, ras.ras_maplength / 3);
92            goto fail;
93        }
94        if(fread(map, sizeof(map), 1, inf) <= 0) {
95            fprintf(stderr, "%s: premature EOF reading colormap", filename);
96            goto fail;
97        }
98        for(i = 0; i < 256; i++)
99            cmap[i] = map[i] | (map[i+256] << 8) | (map[i+512] << 16);
100        break;
101
102    case RMT_NONE:
103        if(ras.ras_depth == 8) {
104            for(i = 0; i < 256; i++)
105                cmap[i] = i * 0x010101;
106        }
107        break;
108
109    default:
110        fprintf(stderr, "%s: can't handle Sun colormap type %d\n",
111                filename, ras.ras_maptype);
112        goto fail;
113    }
114
115    if(ras.ras_type == RT_BYTE_ENCODED) {
116        if(ras.ras_depth != 8) {
117            fprintf(stderr, "%s: can't handle byte-encoded %d-bit images\n",
118                filename, ras.ras_depth);
119            goto fail;
120        }
121        rowbuf = (unsigned char *)malloc(ras.ras_width*sizeof(unsigned char *));
122    }
123
124    if(ras.ras_width > 0) {
125        register int i, c;
126        int row;
127
128        for(row = 0; row < ras.ras_height; row++) {
129            rp = (long *)(im->data + im->rowbytes*(ysize-1 - row));
130            i = xsize;
131            switch(ras.ras_depth) {
132            case 1:
133                while((c = getc(inf)) != EOF && i >= 8) {
134                    *rp++ = (c&0x80) ? 0xFFFFFF : 0;
135                    *rp++ = (c&0x40) ? 0xFFFFFF : 0;
136                    *rp++ = (c&0x20) ? 0xFFFFFF : 0;
137                    *rp++ = (c&0x10) ? 0xFFFFFF : 0;
138                    *rp++ = (c&0x08) ? 0xFFFFFF : 0;
139                    *rp++ = (c&0x04) ? 0xFFFFFF : 0;
140                    *rp++ = (c&0x02) ? 0xFFFFFF : 0;
141                    *rp++ = (c&0x01) ? 0xFFFFFF : 0;
142                }
143                if(c != EOF) {
144                    while(--i >= 0) {
145                        *rp++ = (c&0x80) ? 0xFFFFFF : 0;
146                        c <<= 1;
147                    }
148                }
149                break;
150
151            case 24:
152                do {
153                    c = getc(inf);
154                    c |= getc(inf) << 8;
155                    *rp++ = c | getc(inf) << 16;
156                } while(--i > 0);
157                break;
158
159            case 8:
160                if(ras.ras_type == RT_BYTE_ENCODED) {
161                    register unsigned char *rowp = rowbuf;
162                    int nleft = ras.ras_width;
163                    int seen = 0;
164
165                    do {
166                        seen++;
167                        c = getc(inf);
168                        if(c == 128) {
169                            seen++;
170                            c = getc(inf);
171                            if(c == 0) {
172                                *rowp++ = 128;
173                            } else {
174                                seen++;
175                                nleft -= c;
176                                i = getc(inf);
177                                do { *rowp++ = i; } while(--c >= 0);
178                            }
179                        } else {
180                            *rowp++ = c;
181                        }
182                    } while(--nleft > 0);
183                    if(seen&1)
184                        (void) getc(inf);
185                    /*
186                     * Copy uncompressed data.
187                     */
188                    i = xsize;
189                    rowp = rowbuf;
190                    do { *rp++ = cmap[*rowp++]; } while(--i > 0);
191
192                } else {
193
194                    do {
195                        *rp++ = cmap[getc(inf)];
196                    } while(--i > 0);
197                }
198                break;
199            }
200
201            if(feof(inf)) {
202                fprintf(stderr, "%s: premature EOF reading row %d of %d\n",
203                    filename, row, ras.ras_height);
204                break;
205            }
206
207            if(ras.ras_type != RT_BYTE_ENCODED) {
208                i = (((ras.ras_width*ras.ras_depth + 15) & ~15) >> 3) - xsize;
209                while(--i >= 0)
210                    (void) getc(inf);
211            }
212        }
213    }
214    if(rowbuf != NULL)
215        free(rowbuf);
216    if(inf != NULL)
217        fclose(inf);
218    return im;
219
220  fail:
221    if(im) {
222        if(im->data) free(im->data);
223        free(im);
224    }
225    if(inf != NULL)
226        fclose(inf);
227    return NULL;
228}
Note: See TracBrowser for help on using the repository browser.