zenilib: zenilib/jni/external/lib3ds/src/lib3ds_background.c Source File

zenilib 0.5.3.0

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
lib3ds_background.c
Go to the documentation of this file.
1 /*
2 Copyright (C) 1996-2008 by Jan Eric Kyprianidis <www.kyprianidis.com>
3 All rights reserved.
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published
7 by the Free Software Foundation, either version 2.1 of the License, or
8 (at your option) any later version.
9
10 Thisprogram is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program; If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include "lib3ds_impl.h"
19
20
21 static void
22 solid_bgnd_read(Lib3dsBackground *background, Lib3dsIo *io) {
23 Lib3dsChunk c;
24 uint16_t chunk;
25 int have_lin = FALSE;
26
27 lib3ds_chunk_read_start(&c, CHK_SOLID_BGND, io);
28
29 while ((chunk = lib3ds_chunk_read_next(&c, io)) != 0) {
30 switch (chunk) {
31 case CHK_LIN_COLOR_F:
32 lib3ds_io_read_rgb(io, background->solid_color);
33 have_lin = TRUE;
34 break;
35
36 case CHK_COLOR_F:
37 lib3ds_io_read_rgb(io, background->solid_color);
38 break;
39
40 default:
41 lib3ds_chunk_unknown(chunk, io);
42 }
43 }
44
45 lib3ds_chunk_read_end(&c, io);
46 }
47
48
49 static void
50 v_gradient_read(Lib3dsBackground *background, Lib3dsIo *io) {
51 Lib3dsChunk c;
52 uint16_t chunk;
53 int index[2];
54 float col[2][3][3];
55 int have_lin = 0;
56
57 lib3ds_chunk_read_start(&c, CHK_V_GRADIENT, io);
58
59 background->gradient_percent = lib3ds_io_read_float(io);
60 lib3ds_chunk_read_tell(&c, io);
61
62 index[0] = index[1] = 0;
63 while ((chunk = lib3ds_chunk_read_next(&c, io)) != 0) {
64 switch (chunk) {
65 case CHK_COLOR_F:
66 lib3ds_io_read_rgb(io, col[0][index[0]]);
67 index[0]++;
68 break;
69
70 case CHK_LIN_COLOR_F:
71 lib3ds_io_read_rgb(io, col[1][index[1]]);
72 index[1]++;
73 have_lin = 1;
74 break;
75
76 default:
77 lib3ds_chunk_unknown(chunk, io);
78 }
79 }
80 {
81 int i;
82 for (i = 0; i < 3; ++i) {
83 background->gradient_top[i] = col[have_lin][0][i];
84 background->gradient_middle[i] = col[have_lin][1][i];
85 background->gradient_bottom[i] = col[have_lin][2][i];
86 }
87 }
88 lib3ds_chunk_read_end(&c, io);
89 }
90
91
92 void
93 lib3ds_background_read(Lib3dsBackground *background, Lib3dsIo *io) {
94 Lib3dsChunk c;
95
96 lib3ds_chunk_read(&c, io);
97 switch (c.chunk) {
98 case CHK_BIT_MAP: {
99 lib3ds_io_read_string(io, background->bitmap_name, 64);
100 break;
101 }
102
103 case CHK_SOLID_BGND: {
104 lib3ds_chunk_read_reset(&c, io);
105 solid_bgnd_read(background, io);
106 break;
107 }
108
109 case CHK_V_GRADIENT: {
110 lib3ds_chunk_read_reset(&c, io);
111 v_gradient_read(background, io);
112 break;
113 }
114
115 case CHK_USE_BIT_MAP: {
116 background->use_bitmap = TRUE;
117 break;
118 }
119
120 case CHK_USE_SOLID_BGND: {
121 background->use_solid = TRUE;
122 break;
123 }
124
125 case CHK_USE_V_GRADIENT: {
126 background->use_gradient = TRUE;
127 break;
128 }
129 }
130 }
131
132
133 static void
134 colorf_write(float rgb[3], Lib3dsIo *io) {
135 Lib3dsChunk c;
136
137 c.chunk = CHK_COLOR_F;
138 c.size = 18;
139 lib3ds_chunk_write(&c, io);
140 lib3ds_io_write_rgb(io, rgb);
141
142 c.chunk = CHK_LIN_COLOR_F;
143 c.size = 18;
144 lib3ds_chunk_write(&c, io);
145 lib3ds_io_write_rgb(io, rgb);
146 }
147
148
149 static int
150 colorf_defined(float rgb[3]) {
151 int i;
152 for (i = 0; i < 3; ++i) {
153 if (fabs(rgb[i]) > LIB3DS_EPSILON) {
154 break;
155 }
156 }
157 return(i < 3);
158 }
159
160
161 void
162 lib3ds_background_write(Lib3dsBackground *background, Lib3dsIo *io) {
163 if (strlen(background->bitmap_name)) { /*---- LIB3DS_BIT_MAP ----*/
164 Lib3dsChunk c;
165 c.chunk = CHK_BIT_MAP;
166 c.size = 6 + 1 + (uint32_t)strlen(background->bitmap_name);
167 lib3ds_chunk_write(&c, io);
168 lib3ds_io_write_string(io, background->bitmap_name);
169 }
170
171 if (colorf_defined(background->solid_color)) { /*---- LIB3DS_SOLID_BGND ----*/
172 Lib3dsChunk c;
173 c.chunk = CHK_SOLID_BGND;
174 c.size = 42;
175 lib3ds_chunk_write(&c, io);
176 colorf_write(background->solid_color, io);
177 }
178
179 if (colorf_defined(background->gradient_top) ||
180 colorf_defined(background->gradient_middle) ||
181 colorf_defined(background->gradient_bottom)) { /*---- LIB3DS_V_GRADIENT ----*/
182 Lib3dsChunk c;
183 c.chunk = CHK_V_GRADIENT;
184 c.size = 118;
185 lib3ds_chunk_write(&c, io);
186 lib3ds_io_write_float(io, background->gradient_percent);
187 colorf_write(background->gradient_top, io);
188 colorf_write(background->gradient_middle, io);
189 colorf_write(background->gradient_bottom, io);
190 }
191
192 if (background->use_bitmap) { /*---- LIB3DS_USE_BIT_MAP ----*/
193 Lib3dsChunk c;
194 c.chunk = CHK_USE_BIT_MAP;
195 c.size = 6;
196 lib3ds_chunk_write(&c, io);
197 }
198
199 if (background->use_solid) { /*---- LIB3DS_USE_SOLID_BGND ----*/
200 Lib3dsChunk c;
201 c.chunk = CHK_USE_SOLID_BGND;
202 c.size = 6;
203 lib3ds_chunk_write(&c, io);
204 }
205
206 if (background->use_gradient) { /*---- LIB3DS_USE_V_GRADIENT ----*/
207 Lib3dsChunk c;
208 c.chunk = CHK_USE_V_GRADIENT;
209 c.size = 6;
210 lib3ds_chunk_write(&c, io);
211 }
212 }
213
CHK_SOLID_BGND
Definition: lib3ds_impl.h:97
chunk
else Out of place iCCP chunk
Definition: pngrutil.c:1260
Lib3dsBackground::gradient_percent
float gradient_percent
Definition: lib3ds.h:96
lib3ds_background_write
void lib3ds_background_write(Lib3dsBackground *background, Lib3dsIo *io)
Definition: lib3ds_background.c:162
solid_bgnd_read
static void solid_bgnd_read(Lib3dsBackground *background, Lib3dsIo *io)
Definition: lib3ds_background.c:22
Lib3dsChunk
Definition: lib3ds_impl.h:295
uint16_t
unsigned short uint16_t
Definition: SDL_config_windows.h:60
lib3ds_chunk_unknown
void lib3ds_chunk_unknown(uint16_t chunk, Lib3dsIo *io)
Definition: lib3ds_chunk.c:140
lib3ds_chunk_read
void lib3ds_chunk_read(Lib3dsChunk *c, Lib3dsIo *io)
Definition: lib3ds_chunk.c:34
Lib3dsBackground::gradient_bottom
float gradient_bottom[3]
Definition: lib3ds.h:99
Lib3dsChunk::size
uint32_t size
Definition: lib3ds_impl.h:297
Lib3dsBackground::bitmap_name
char bitmap_name[64]
Definition: lib3ds.h:92
lib3ds_chunk_read_next
uint16_t lib3ds_chunk_read_next(Lib3dsChunk *c, Lib3dsIo *io)
Definition: lib3ds_chunk.c:68
CHK_LIN_COLOR_F
Definition: lib3ds_impl.h:80
Lib3dsBackground::gradient_top
float gradient_top[3]
Definition: lib3ds.h:97
Lib3dsBackground::solid_color
float solid_color[3]
Definition: lib3ds.h:94
CHK_USE_BIT_MAP
Definition: lib3ds_impl.h:99
CHK_V_GRADIENT
Definition: lib3ds_impl.h:98
lib3ds_io_write_string
void lib3ds_io_write_string(Lib3dsIo *io, const char *s)
Definition: lib3ds_io.c:443
Lib3dsBackground::gradient_middle
float gradient_middle[3]
Definition: lib3ds.h:98
lib3ds_io_write_float
void lib3ds_io_write_float(Lib3dsIo *io, float l)
Definition: lib3ds_io.c:402
v_gradient_read
static void v_gradient_read(Lib3dsBackground *background, Lib3dsIo *io)
Definition: lib3ds_background.c:50
lib3ds_chunk_read_reset
void lib3ds_chunk_read_reset(Lib3dsChunk *c, Lib3dsIo *io)
Definition: lib3ds_chunk.c:90
c
const GLfloat * c
Definition: glew.h:14913
index
GLuint index
Definition: glew.h:1800
lib3ds_io_read_float
float lib3ds_io_read_float(Lib3dsIo *io)
Definition: lib3ds_io.c:237
FALSE
#define FALSE
Definition: ftobjs.h:57
CHK_BIT_MAP
Definition: lib3ds_impl.h:96
Lib3dsBackground::use_gradient
int use_gradient
Definition: lib3ds.h:95
lib3ds_chunk_read_end
void lib3ds_chunk_read_end(Lib3dsChunk *c, Lib3dsIo *io)
Definition: lib3ds_chunk.c:96
CHK_COLOR_F
Definition: lib3ds_impl.h:77
lib3ds_chunk_read_tell
void lib3ds_chunk_read_tell(Lib3dsChunk *c, Lib3dsIo *io)
Definition: lib3ds_chunk.c:62
LIB3DS_EPSILON
#define LIB3DS_EPSILON
Definition: lib3ds_impl.h:55
uint32_t
unsigned int uint32_t
Definition: SDL_config_windows.h:62
Lib3dsIo
Definition: lib3ds.h:56
lib3ds_io_read_string
void lib3ds_io_read_string(Lib3dsIo *io, char *s, int buflen)
Definition: lib3ds_io.c:285
CHK_USE_V_GRADIENT
Definition: lib3ds_impl.h:101
lib3ds_io_read_rgb
void lib3ds_io_read_rgb(Lib3dsIo *io, float rgb[3])
Definition: lib3ds_io.c:267
colorf_defined
static int colorf_defined(float rgb[3])
Definition: lib3ds_background.c:150
lib3ds_chunk_write
void lib3ds_chunk_write(Lib3dsChunk *c, Lib3dsIo *io)
Definition: lib3ds_chunk.c:111
lib3ds_chunk_read_start
void lib3ds_chunk_read_start(Lib3dsChunk *c, uint16_t chunk, Lib3dsIo *io)
Definition: lib3ds_chunk.c:50
Lib3dsBackground
Definition: lib3ds.h:90
lib3ds_background_read
void lib3ds_background_read(Lib3dsBackground *background, Lib3dsIo *io)
Definition: lib3ds_background.c:93
colorf_write
static void colorf_write(float rgb[3], Lib3dsIo *io)
Definition: lib3ds_background.c:134
Lib3dsChunk::chunk
uint16_t chunk
Definition: lib3ds_impl.h:296
i
int i
Definition: pngrutil.c:1377
fabs
double fabs(double x)
Definition: s_fabs.c:29
Lib3dsBackground::use_bitmap
int use_bitmap
Definition: lib3ds.h:91
TRUE
#define TRUE
Definition: ftobjs.h:53
lib3ds_impl.h
lib3ds_io_write_rgb
void lib3ds_io_write_rgb(Lib3dsIo *io, float rgb[3])
Definition: lib3ds_io.c:431
CHK_USE_SOLID_BGND
Definition: lib3ds_impl.h:100
Lib3dsBackground::use_solid
int use_solid
Definition: lib3ds.h:93