1 /*
2     Copyright (c) 2005-2023 Intel Corporation
3 
4     Licensed under the Apache License, Version 2.0 (the "License");
5     you may not use this file except in compliance with the License.
6     You may obtain a copy of the License at
7 
8         http://www.apache.org/licenses/LICENSE-2.0
9 
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15 */
16 
17 /*
18     The original source for this example is
19     Copyright (c) 1994-2008 John E. Stone
20     All rights reserved.
21 
22     Redistribution and use in source and binary forms, with or without
23     modification, are permitted provided that the following conditions
24     are met:
25     1. Redistributions of source code must retain the above copyright
26        notice, this list of conditions and the following disclaimer.
27     2. Redistributions in binary form must reproduce the above copyright
28        notice, this list of conditions and the following disclaimer in the
29        documentation and/or other materials provided with the distribution.
30     3. The name of the author may not be used to endorse or promote products
31        derived from this software without specific prior written permission.
32 
33     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
34     OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
35     WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36     ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
37     DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38     DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39     OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41     LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42     OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43     SUCH DAMAGE.
44 */
45 
46 /*
47  *  imageio.cpp - This file deals with reading/writing image files
48  */
49 
50 /* For our purposes, we're interested only in the 3 byte per pixel 24 bit
51  * truecolor sort of file..
52  */
53 
54 #include <cstdio>
55 
56 #include "machine.hpp"
57 #include "types.hpp"
58 #include "util.hpp"
59 #include "imageio.hpp"
60 #include "ppm.hpp" /* PPM files */
61 #include "tgafile.hpp" /* Truevision Targa files */
62 #include "jpeg.hpp" /* JPEG files */
63 
64 static int fakeimage(char *name, int *xres, int *yres, unsigned char **imgdata) {
65     int i, imgsize;
66 
67     fprintf(stderr, "Error loading image %s.  Faking it.\n", name);
68 
69     *xres = 2;
70     *yres = 2;
71     imgsize = 3 * (*xres) * (*yres);
72     *imgdata = (unsigned char *)rt_getmem(imgsize);
73     for (i = 0; i < imgsize; i++) {
74         (*imgdata)[i] = 255;
75     }
76 
77     return IMAGENOERR;
78 }
79 
80 int readimage(rawimage *img) {
81     int rc;
82     int xres, yres;
83     unsigned char *imgdata = nullptr;
84     char *name = img->name;
85 
86     if (strstr(name, ".ppm")) {
87         rc = readppm(name, &xres, &yres, &imgdata);
88     }
89     else if (strstr(name, ".tga")) {
90         rc = readtga(name, &xres, &yres, &imgdata);
91     }
92     else if (strstr(name, ".jpg")) {
93         rc = readjpeg(name, &xres, &yres, &imgdata);
94     }
95     else if (strstr(name, ".gif")) {
96         rc = IMAGEUNSUP;
97     }
98     else if (strstr(name, ".png")) {
99         rc = IMAGEUNSUP;
100     }
101     else if (strstr(name, ".tiff")) {
102         rc = IMAGEUNSUP;
103     }
104     else if (strstr(name, ".rgb")) {
105         rc = IMAGEUNSUP;
106     }
107     else if (strstr(name, ".xpm")) {
108         rc = IMAGEUNSUP;
109     }
110     else {
111         rc = readppm(name, &xres, &yres, &imgdata);
112     }
113 
114     switch (rc) {
115         case IMAGEREADERR:
116             fprintf(stderr, "Short read encountered while loading image %s\n", name);
117             rc = IMAGENOERR; /* remap to non-fatal error */
118             break;
119 
120         case IMAGEUNSUP:
121             fprintf(stderr, "Cannot read unsupported image format for image %s\n", name);
122             break;
123     }
124 
125     /* If the image load failed, create a tiny white colored image to fake it */
126     /* this allows a scene to render even when a file can't be loaded */
127     if (rc != IMAGENOERR) {
128         rc = fakeimage(name, &xres, &yres, &imgdata);
129     }
130 
131     /* If we succeeded in loading the image, return it. */
132     if (rc == IMAGENOERR) {
133         img->xres = xres;
134         img->yres = yres;
135         img->bpp = 3;
136         img->data = imgdata;
137     }
138 
139     return rc;
140 }
141