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 | #include <stdio.h> |
---|
38 | #include <stdlib.h> |
---|
39 | #include <image.h> |
---|
40 | #include <stdlib.h> |
---|
41 | #include <memory.h> |
---|
42 | #include <sys/types.h> |
---|
43 | #include "image.h" |
---|
44 | #include "imginfo.h" |
---|
45 | |
---|
46 | IMG * |
---|
47 | SGImakedisp(fname) |
---|
48 | char *fname; |
---|
49 | { |
---|
50 | IMAGE *image; |
---|
51 | register IMG *im; |
---|
52 | unsigned short *trow; |
---|
53 | register int k; |
---|
54 | int width, height; |
---|
55 | int y, z; |
---|
56 | static int one = 1; |
---|
57 | |
---|
58 | int littleendian = (*(char *)&one == 1); |
---|
59 | |
---|
60 | image = iopen(fname, "r"); |
---|
61 | if(image == NULL) |
---|
62 | return NULL; |
---|
63 | |
---|
64 | width = image->xsize; |
---|
65 | height = image->ysize; |
---|
66 | im = (IMG *)malloc(sizeof(IMG)); |
---|
67 | im->rowbytes = sizeof(IMulong) * width; |
---|
68 | im->type = IT_LONG; |
---|
69 | im->xsize = width; |
---|
70 | im->ysize = height; |
---|
71 | im->data = (unsigned char *)malloc(im->rowbytes * height + (sizeof(short)*width)); |
---|
72 | if(im->data == NULL) { |
---|
73 | fprintf(stderr, "Can't malloc %d bytes of memory for image\n", |
---|
74 | im->rowbytes * height); |
---|
75 | exit(2); |
---|
76 | } |
---|
77 | trow = (unsigned short *)(im->data + im->rowbytes * height); |
---|
78 | if(image->zsize == 3) |
---|
79 | memset(im->data, 255, im->rowbytes * height); |
---|
80 | |
---|
81 | for(y = 0; y < image->ysize; y++) { |
---|
82 | for(z = 0; z < image->zsize; z++) { |
---|
83 | register unsigned char *op; |
---|
84 | register unsigned short *ip; |
---|
85 | |
---|
86 | |
---|
87 | getrow(image, trow, y, z); |
---|
88 | k = image->xsize; |
---|
89 | op = (unsigned char *) (im->data + im->rowbytes * y) |
---|
90 | + (littleendian ? z : 3 - z); |
---|
91 | ip = trow; |
---|
92 | do { |
---|
93 | *op = *ip++; |
---|
94 | op += sizeof(IMulong); |
---|
95 | } while(--k > 0); |
---|
96 | } |
---|
97 | } |
---|
98 | if(image->zsize == 1) { |
---|
99 | register IMulong *op; |
---|
100 | register int v; |
---|
101 | |
---|
102 | k = image->xsize * image->ysize; |
---|
103 | op = (IMulong *)im->data; |
---|
104 | do { |
---|
105 | v = (*op & 0xFF); /* XXX byte-order dependent */ |
---|
106 | *op++ = v | (v<<8) | (v<<16) | 0xFF000000; |
---|
107 | } while(--k > 0); |
---|
108 | } |
---|
109 | iclose(image); |
---|
110 | |
---|
111 | return im; |
---|
112 | } |
---|