1 /*
2 Copyright (c) 2005-2021 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 * imap.cpp - This file contains code for doing image map type things.
48 */
49
50 #include "machine.hpp"
51 #include "types.hpp"
52 #include "imap.hpp"
53 #include "util.hpp"
54 #include "imageio.hpp"
55
56 rawimage *imagelist[MAXIMGS];
57 int numimages;
58
ResetImages(void)59 void ResetImages(void) {
60 int i;
61 numimages = 0;
62 for (i = 0; i < MAXIMGS; i++) {
63 imagelist[i] = nullptr;
64 }
65 }
66
LoadImage(rawimage * image)67 void LoadImage(rawimage *image) {
68 if (!image->loaded) {
69 readimage(image);
70 image->loaded = 1;
71 }
72 }
73
ImageMap(rawimage * image,flt u,flt v)74 color ImageMap(rawimage *image, flt u, flt v) {
75 color col, colx, colx2;
76 flt x, y, px, py;
77 int x1, x2, y1, y2;
78 unsigned char *ptr;
79 unsigned char *ptr2;
80
81 if (!image->loaded) {
82 LoadImage(image);
83 image->loaded = 1;
84 }
85
86 if ((u <= 1.0) && (u >= 0.0) && (v <= 1.0) && (v >= 0.0)) {
87 x = (image->xres - 1.0) * u; /* floating point X location */
88 y = (image->yres - 1.0) * v; /* floating point Y location */
89
90 px = x - ((int)x);
91 py = y - ((int)y);
92
93 x1 = (int)x;
94 x2 = x1 + 1;
95
96 y1 = (int)y;
97 y2 = y1 + 1;
98
99 ptr = image->data + ((image->xres * y1) + x1) * 3;
100 ptr2 = image->data + ((image->xres * y1) + x2) * 3;
101
102 colx.r = (flt)((flt)ptr[0] + px * ((flt)ptr2[0] - (flt)ptr[0])) / 255.0;
103 colx.g = (flt)((flt)ptr[1] + px * ((flt)ptr2[1] - (flt)ptr[1])) / 255.0;
104 colx.b = (flt)((flt)ptr[2] + px * ((flt)ptr2[2] - (flt)ptr[2])) / 255.0;
105
106 ptr = image->data + ((image->xres * y2) + x1) * 3;
107 ptr2 = image->data + ((image->xres * y2) + x2) * 3;
108
109 colx2.r = ((flt)ptr[0] + px * ((flt)ptr2[0] - (flt)ptr[0])) / 255.0;
110 colx2.g = ((flt)ptr[1] + px * ((flt)ptr2[1] - (flt)ptr[1])) / 255.0;
111 colx2.b = ((flt)ptr[2] + px * ((flt)ptr2[2] - (flt)ptr[2])) / 255.0;
112
113 col.r = colx.r + py * (colx2.r - colx.r);
114 col.g = colx.g + py * (colx2.g - colx.g);
115 col.b = colx.b + py * (colx2.b - colx.b);
116 }
117 else {
118 col.r = 0.0;
119 col.g = 0.0;
120 col.b = 0.0;
121 }
122 return col;
123 }
124
AllocateImage(char * filename)125 rawimage *AllocateImage(char *filename) {
126 rawimage *newimage = nullptr;
127 int i, intable;
128 std::size_t len;
129
130 intable = 0;
131 if (numimages != 0) {
132 for (i = 0; i < numimages; i++) {
133 if (!strcmp(filename, imagelist[i]->name)) {
134 newimage = imagelist[i];
135 intable = 1;
136 }
137 }
138 }
139
140 if (!intable) {
141 newimage = (rawimage *)rt_getmem(sizeof(rawimage));
142 newimage->loaded = 0;
143 newimage->xres = 0;
144 newimage->yres = 0;
145 newimage->bpp = 0;
146 newimage->data = nullptr;
147 len = strlen(filename);
148 if (len > 80)
149 rtbomb("Filename too long in image map!!");
150 strcpy(newimage->name, filename);
151
152 imagelist[numimages] = newimage; /* add new one to the table */
153 numimages++; /* increment the number of images */
154 }
155
156 return newimage;
157 }
158
DeallocateImage(rawimage * image)159 void DeallocateImage(rawimage *image) {
160 image->loaded = 0;
161 rt_freemem(image->data);
162 }
163