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

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

Added modified SAGE sources

  • Property svn:executable set to *
RevLine 
[4]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#define USEDISP 1
38
39#include <stdio.h>
40#include <stdlib.h>
41#include <sys/types.h>
42#include "imginfo.h"
43
44#ifdef sun
45typedef uint32_t u_int32_t;
46#endif
47
48/*
49 * Routine to read an image in simple raster form.
50 * Given a file name, may fill in an "imginfo" structure,
51 * and returns the type of image.
52 */
53#if USEDISP
54extern IMG *TIFFmakedisp(), *SGImakedisp(), *softmakedisp(),
55                 *rasmakedisp(), *gifmakedisp(), *pnmmakedisp(),
56                 *jpegmakedisp();
57
58static IMG *(*imgdatafunc[])() = {
59        NULL,
60        TIFFmakedisp,
61        SGImakedisp,
62        rasmakedisp,
63        softmakedisp,
64        gifmakedisp,
65        pnmmakedisp,
66        NULL/*rawmakedisp?*/,
67        jpegmakedisp
68};
69
70#else /*!USEDISP*/
71
72extern unsigned char *img_tiffdata();
73extern unsigned char *img_sgidata();
74extern unsigned char *img_rasdata();
75extern unsigned char *img_softdata();
76extern unsigned char *img_gifdata();
77extern unsigned char *img_pnmdata();
78extern unsigned char *img_rawdata();
79extern unsigned char *img_jpegdata();
80
81static unsigned char *(*imgdatafunc[])() = {
82        NULL,
83        img_tiffdata,
84        img_sgidata,
85        img_rasdata,
86        img_softdata,
87        img_gifdata,
88        img_pnmdata,
89        /*img_rawdata*/NULL,
90        img_jpegdata,
91};
92#endif /*!USEDISP*/
93
94
95unsigned char *
96getimgdata(
97    const char *fname,          /* file name */
98    const struct imginfo *knowninfo, /* image info if already got by getimginfo */
99    struct imginfo *wantinfo,   /* image info if wanted */
100                                /* Flags for desired image format */
101    int yup,                    /* Y increases up */
102    int bpp,                    /* bytes per pixel, or 0 for image's own */
103    int byteorder)              /* 0: RGBA, 1: ABGR */
104{
105    struct imginfo dummy;
106    struct imginfo *info;
107#if USEDISP
108    register unsigned char *p;
109    register int i, n;
110    int y;
111    IMG *im;
112#endif
113
114    if(knowninfo != NULL) {
115        info = (struct imginfo *)knowninfo;
116    } else {
117        info = wantinfo ? wantinfo : &dummy;
118        getimginfo((char *)fname, info);
119    }
120
121    if((unsigned)info->kind >= sizeof(imgdatafunc)/sizeof(imgdatafunc[0])
122                || imgdatafunc[info->kind] == NULL)
123        return NULL;
124
125#if USEDISP
126    /* just for now ... (ha!) */
127    if(bpp != 4 && bpp != 0) {
128        fprintf(stderr, "getimgdata: can only handle 4 byte/pixel requests\n");
129        return NULL;
130    }
131    im = (*imgdatafunc[info->kind])(fname);
132    if(im == NULL || im->data == NULL)
133        return NULL;
134    switch(im->type) {
135    case IT_LONG:
136        p = im->data;
137        if(!yup) {
138            register unsigned char *q = p + 4 * im->xsize * (im->ysize - 1);
139            if(byteorder) {
140                for(n = im->ysize / 2; --n >= 0; q -= 2*4*im->xsize) {
141                    register int k = im->xsize;
142                    do {
143                        i = *(u_int32_t *)p;  *(u_int32_t *)p = *(u_int32_t *)q;  *(u_int32_t *)q = i;
144                        p += sizeof(u_int32_t);  q += sizeof(u_int32_t);
145                    } while(--k);
146                }
147            } else {
148                for(n = im->ysize / 2; --n >= 0; q -= 2*4*im->xsize) {
149                    register int k = im->xsize;
150                    do {
151                        i = p[0]; p[0] = q[3]; q[3] = i;
152                        i = p[1]; p[1] = q[2]; q[2] = i;
153                        i = p[2]; p[2] = q[1]; q[1] = i;
154                        i = p[3]; p[3] = q[0]; q[0] = i;
155                        p += 4;
156                        q += 4;
157                    } while(--k);
158                }
159            }
160            if(im->ysize & 1) {
161                register int k = im->xsize;
162                do {
163                    i = p[0]; p[0] = p[3]; p[3] = i;
164                    i = p[1]; p[1] = p[2]; p[2] = i;
165                    p += 4;
166                } while(--k);
167            }
168        } else if(byteorder) {
169            i = im->xsize * im->ysize;
170            do {
171                *(u_int32_t *)p = p[0] | (p[1]<<8) | (p[2]<<16) | (p[3]<<24);
172                p += sizeof(u_int32_t);
173            } while(--i);
174        }
175        p = im->data;
176        break;
177
178    case IT_BIT:
179     {
180        unsigned char *obuf = (unsigned char *)malloc(sizeof(u_int32_t)*(im->xsize*im->ysize + 7));
181        register u_int32_t *q = (u_int32_t *)obuf;
182
183        for(y = im->ysize; --y >= 0; ) {
184            p = &im->data[y*im->rowbytes];
185            i = im->xsize >> 3;
186            do {
187                n = *p++;
188                *q++ = n&0x80 ? 0xFFFFFFFF : 0;
189                *q++ = n&0x40 ? 0xFFFFFFFF : 0;
190                *q++ = n&0x20 ? 0xFFFFFFFF : 0;
191                *q++ = n&0x10 ? 0xFFFFFFFF : 0;
192                *q++ = n&0x08 ? 0xFFFFFFFF : 0;
193                *q++ = n&0x04 ? 0xFFFFFFFF : 0;
194                *q++ = n&0x02 ? 0xFFFFFFFF : 0;
195                *q++ = n&0x01 ? 0xFFFFFFFF : 0;
196            } while(--i > 0);
197        }
198        free(im->data);
199        p = obuf;
200        break;
201     }
202
203    default:
204        p = NULL;
205        break;
206    }
207    free(im);
208    return p;
209#else /*!USEDISP*/
210    return (*imgdatafunc[info->kind])(fname, info->xsize, info->ysize,
211                                                yup, bpp, byteorder);
212#endif /*!USEDISP*/
213}
214
215/*
216 * Utility routine shared by some _data routines
217 * Selects/reorders pixels in a row, in place.
218 */
219void
220_img_rerow(row, xsize, hasbpp, wantbpp, reorder)
221    unsigned char *row;
222    int xsize;
223    int hasbpp;
224    int wantbpp;
225    int reorder;
226{
227    register unsigned char *p, *q;
228    register int j, k;
229
230    if(xsize <= 0)
231        return;
232
233    k = xsize;
234    p = row;
235    if(hasbpp == wantbpp) {
236        if(!reorder)
237            return;
238
239        switch(wantbpp) {
240        case 2:
241        case 3:
242            do {
243                j = *p;
244                *p = *q;
245                *q = j;
246                p = q;
247                p += wantbpp;
248                q += wantbpp;
249            } while(--k);
250            break;
251
252        case 4:
253            do {
254                j = *p;
255                *p = p[3];
256                p[3] = j;
257                j = p[1];
258                p[1] = p[2];
259                p[2] = j;
260                p += 4;
261            } while(--k);
262            break;
263        }
264    } else if(hasbpp > wantbpp) {
265        /* Shorten the row; we can copy scanning forward */
266        q = p;
267        if(reorder) {
268           /* ... */
269        }
270    } else {
271        /* ... */
272    }
273}
Note: See TracBrowser for help on using the repository browser.