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 JPEG file, return a DISPIMAGE |
---|
39 | * containing its raster data and with size <= (xsize, ysize). |
---|
40 | */ |
---|
41 | |
---|
42 | #include <stdio.h> |
---|
43 | #include <stdlib.h> |
---|
44 | #include "jpeglib.h" |
---|
45 | #include "imginfo.h" |
---|
46 | |
---|
47 | #ifndef alloca |
---|
48 | # include <alloca.h> |
---|
49 | #endif |
---|
50 | |
---|
51 | IMG * |
---|
52 | jpegmakedisp(char *filename) |
---|
53 | { |
---|
54 | FILE *f; |
---|
55 | IMG *di = NULL; |
---|
56 | int xsize, ysize, zsize; |
---|
57 | int x, y; |
---|
58 | struct jpeg_error_mgr jerr[1]; |
---|
59 | struct jpeg_decompress_struct jp[1]; |
---|
60 | JSAMPARRAY yx; |
---|
61 | JSAMPROW trow; |
---|
62 | |
---|
63 | f = fopen(filename, "r"); |
---|
64 | if(f == NULL) { |
---|
65 | fprintf(stderr, "%s: cannot open: ", filename); |
---|
66 | perror(""); |
---|
67 | return NULL; |
---|
68 | } |
---|
69 | |
---|
70 | jp->err = jpeg_std_error( jerr ); |
---|
71 | jpeg_create_decompress( jp ); |
---|
72 | jpeg_stdio_src( jp, f ); |
---|
73 | if(jpeg_read_header( jp, TRUE ) != JPEG_HEADER_OK) |
---|
74 | goto fail; |
---|
75 | |
---|
76 | jpeg_start_decompress( jp ); |
---|
77 | |
---|
78 | |
---|
79 | di = (IMG *)malloc(sizeof(IMG)); |
---|
80 | di->xsize = xsize = jp->output_width; |
---|
81 | di->ysize = ysize = jp->output_height; |
---|
82 | zsize = jp->output_components; |
---|
83 | di->data = (unsigned char *)malloc( xsize * ysize * 4 * sizeof(char) ); |
---|
84 | di->type = IT_LONG; |
---|
85 | if(di->data == NULL) { |
---|
86 | fprintf(stderr, "Not enough memory for %dx%dx%d image %s!\n", |
---|
87 | xsize, ysize, zsize, filename); |
---|
88 | goto fail; |
---|
89 | } |
---|
90 | |
---|
91 | yx = (JSAMPARRAY) alloca( jp->rec_outbuf_height * sizeof(JSAMPROW) ); |
---|
92 | trow = (JSAMPROW) alloca( zsize * xsize * jp->rec_outbuf_height * sizeof(JSAMPLE) ); |
---|
93 | |
---|
94 | for(y = 0; y < jp->rec_outbuf_height; y++) |
---|
95 | yx[y] = &trow[ y * xsize * zsize ]; |
---|
96 | |
---|
97 | while(jp->output_scanline < ysize) { |
---|
98 | int y, got, nleft; |
---|
99 | JSAMPLE *ip = trow; |
---|
100 | y = jp->output_scanline; |
---|
101 | nleft = ysize - y; |
---|
102 | if(nleft > jp->rec_outbuf_height) nleft = jp->rec_outbuf_height; |
---|
103 | got = jpeg_read_scanlines( jp, yx, nleft ); |
---|
104 | if(got <= 0) { |
---|
105 | fprintf(stderr, "%s: trouble reading JPEG row %d of 0..%d (nleft %d)\n", filename, y, ysize-1, nleft); |
---|
106 | break; |
---|
107 | } |
---|
108 | while(--got >= 0) { |
---|
109 | unsigned int *op = (unsigned int *)&di->data[ (ysize-1 - y) * xsize * 4 ]; |
---|
110 | x = xsize; |
---|
111 | switch(zsize) { |
---|
112 | case 1: |
---|
113 | do { |
---|
114 | *op++ = (0xFF<<24) | (*ip++ * 0x010101); |
---|
115 | } while(--x > 0); |
---|
116 | break; |
---|
117 | case 3: |
---|
118 | do { |
---|
119 | *op++ = (0xFF<<24) | (ip[2]<<16) | (ip[1]<<8) | (ip[0]); |
---|
120 | ip += 3; |
---|
121 | } while(--x > 0); |
---|
122 | break; |
---|
123 | } |
---|
124 | y++; |
---|
125 | } |
---|
126 | } |
---|
127 | jpeg_finish_decompress( jp ); |
---|
128 | jpeg_destroy( (j_common_ptr) jp ); |
---|
129 | fclose(f); |
---|
130 | return di; |
---|
131 | |
---|
132 | fail: |
---|
133 | jpeg_destroy( (j_common_ptr) jp ); |
---|
134 | if(di) { |
---|
135 | if(di->data) free(di->data); |
---|
136 | free(di); |
---|
137 | } |
---|
138 | if(f != NULL) |
---|
139 | fclose(f); |
---|
140 | return NULL; |
---|
141 | } |
---|