| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | #define LIB3DS_EXPORT |
|---|
| 23 | #include "background.h" |
|---|
| 24 | #include "chunk.h" |
|---|
| 25 | #include "readwrite.h" |
|---|
| 26 | #include <string.h> |
|---|
| 27 | #include <math.h> |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | static Lib3dsBool |
|---|
| 38 | solid_bgnd_read(Lib3dsBackground *background, iostream *strm) |
|---|
| 39 | { |
|---|
| 40 | Lib3dsChunk c; |
|---|
| 41 | Lib3dsWord chunk; |
|---|
| 42 | |
|---|
| 43 | if (!lib3ds_chunk_read_start(&c, LIB3DS_SOLID_BGND, strm)) { |
|---|
| 44 | return(LIB3DS_FALSE); |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | while ((chunk=lib3ds_chunk_read_next(&c, strm))!=0) { |
|---|
| 48 | switch (chunk) { |
|---|
| 49 | case LIB3DS_LIN_COLOR_F: |
|---|
| 50 | lib3ds_rgb_read(background->solid.col, strm); |
|---|
| 51 | break; |
|---|
| 52 | case LIB3DS_COLOR_F: |
|---|
| 53 | lib3ds_rgb_read(background->solid.col, strm); |
|---|
| 54 | break; |
|---|
| 55 | default: |
|---|
| 56 | lib3ds_chunk_unknown(chunk); |
|---|
| 57 | } |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | lib3ds_chunk_read_end(&c, strm); |
|---|
| 61 | return(LIB3DS_TRUE); |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | static Lib3dsBool |
|---|
| 66 | v_gradient_read(Lib3dsBackground *background, iostream *strm) |
|---|
| 67 | { |
|---|
| 68 | Lib3dsChunk c; |
|---|
| 69 | Lib3dsWord chunk; |
|---|
| 70 | int index[2]; |
|---|
| 71 | Lib3dsRgb col[2][3]; |
|---|
| 72 | int have_lin=0; |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | if (!lib3ds_chunk_read_start(&c, LIB3DS_V_GRADIENT, strm)) { |
|---|
| 76 | return(LIB3DS_FALSE); |
|---|
| 77 | } |
|---|
| 78 | background->gradient.percent=lib3ds_float_read(strm); |
|---|
| 79 | lib3ds_chunk_read_tell(&c, strm); |
|---|
| 80 | |
|---|
| 81 | index[0]=index[1]=0; |
|---|
| 82 | while ((chunk=lib3ds_chunk_read_next(&c, strm))!=0) { |
|---|
| 83 | switch (chunk) { |
|---|
| 84 | case LIB3DS_COLOR_F: |
|---|
| 85 | lib3ds_rgb_read(col[0][index[0]],strm); |
|---|
| 86 | index[0]++; |
|---|
| 87 | break; |
|---|
| 88 | case LIB3DS_LIN_COLOR_F: |
|---|
| 89 | lib3ds_rgb_read(col[1][index[1]],strm); |
|---|
| 90 | index[1]++; |
|---|
| 91 | have_lin=1; |
|---|
| 92 | break; |
|---|
| 93 | default: |
|---|
| 94 | lib3ds_chunk_unknown(chunk); |
|---|
| 95 | } |
|---|
| 96 | } |
|---|
| 97 | { |
|---|
| 98 | int i; |
|---|
| 99 | for (i=0; i<3; ++i) { |
|---|
| 100 | background->gradient.top[i]=col[have_lin][0][i]; |
|---|
| 101 | background->gradient.middle[i]=col[have_lin][1][i]; |
|---|
| 102 | background->gradient.bottom[i]=col[have_lin][2][i]; |
|---|
| 103 | } |
|---|
| 104 | } |
|---|
| 105 | lib3ds_chunk_read_end(&c, strm); |
|---|
| 106 | return(LIB3DS_TRUE); |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | Lib3dsBool |
|---|
| 114 | lib3ds_background_read(Lib3dsBackground *background, iostream *strm) |
|---|
| 115 | { |
|---|
| 116 | Lib3dsChunk c; |
|---|
| 117 | |
|---|
| 118 | if (!lib3ds_chunk_read(&c, strm)) { |
|---|
| 119 | return(LIB3DS_FALSE); |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | switch (c.chunk) { |
|---|
| 123 | case LIB3DS_BIT_MAP: |
|---|
| 124 | { |
|---|
| 125 | if (!lib3ds_string_read(background->bitmap.name, 64, strm)) { |
|---|
| 126 | return(LIB3DS_FALSE); |
|---|
| 127 | } |
|---|
| 128 | } |
|---|
| 129 | break; |
|---|
| 130 | case LIB3DS_SOLID_BGND: |
|---|
| 131 | { |
|---|
| 132 | lib3ds_chunk_read_reset(&c, strm); |
|---|
| 133 | if (!solid_bgnd_read(background, strm)) { |
|---|
| 134 | return(LIB3DS_FALSE); |
|---|
| 135 | } |
|---|
| 136 | } |
|---|
| 137 | break; |
|---|
| 138 | case LIB3DS_V_GRADIENT: |
|---|
| 139 | { |
|---|
| 140 | lib3ds_chunk_read_reset(&c, strm); |
|---|
| 141 | if (!v_gradient_read(background, strm)) { |
|---|
| 142 | return(LIB3DS_FALSE); |
|---|
| 143 | } |
|---|
| 144 | } |
|---|
| 145 | break; |
|---|
| 146 | case LIB3DS_USE_BIT_MAP: |
|---|
| 147 | { |
|---|
| 148 | background->bitmap.use=LIB3DS_TRUE; |
|---|
| 149 | } |
|---|
| 150 | break; |
|---|
| 151 | case LIB3DS_USE_SOLID_BGND: |
|---|
| 152 | { |
|---|
| 153 | background->solid.use=LIB3DS_TRUE; |
|---|
| 154 | } |
|---|
| 155 | break; |
|---|
| 156 | case LIB3DS_USE_V_GRADIENT: |
|---|
| 157 | { |
|---|
| 158 | background->gradient.use=LIB3DS_TRUE; |
|---|
| 159 | } |
|---|
| 160 | break; |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | return(LIB3DS_TRUE); |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | static Lib3dsBool |
|---|
| 168 | colorf_write(Lib3dsRgba rgb, iostream *strm) |
|---|
| 169 | { |
|---|
| 170 | Lib3dsChunk c; |
|---|
| 171 | |
|---|
| 172 | c.chunk=LIB3DS_COLOR_F; |
|---|
| 173 | c.size=18; |
|---|
| 174 | lib3ds_chunk_write(&c,strm); |
|---|
| 175 | lib3ds_rgb_write(rgb,strm); |
|---|
| 176 | |
|---|
| 177 | c.chunk=LIB3DS_LIN_COLOR_F; |
|---|
| 178 | c.size=18; |
|---|
| 179 | lib3ds_chunk_write(&c,strm); |
|---|
| 180 | lib3ds_rgb_write(rgb,strm); |
|---|
| 181 | return(LIB3DS_TRUE); |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | |
|---|
| 185 | static Lib3dsBool |
|---|
| 186 | colorf_defined(Lib3dsRgba rgb) |
|---|
| 187 | { |
|---|
| 188 | int i; |
|---|
| 189 | for (i=0; i<3; ++i) { |
|---|
| 190 | if (fabs(rgb[i])>LIB3DS_EPSILON) { |
|---|
| 191 | break; |
|---|
| 192 | } |
|---|
| 193 | } |
|---|
| 194 | return(i<3); |
|---|
| 195 | } |
|---|
| 196 | |
|---|
| 197 | |
|---|
| 198 | |
|---|
| 199 | |
|---|
| 200 | |
|---|
| 201 | Lib3dsBool |
|---|
| 202 | lib3ds_background_write(Lib3dsBackground *background, iostream *strm) |
|---|
| 203 | { |
|---|
| 204 | if (strlen(background->bitmap.name)) { |
|---|
| 205 | Lib3dsChunk c; |
|---|
| 206 | c.chunk=LIB3DS_BIT_MAP; |
|---|
| 207 | c.size=6+1+strlen(background->bitmap.name); |
|---|
| 208 | lib3ds_chunk_write(&c,strm); |
|---|
| 209 | lib3ds_string_write(background->bitmap.name, strm); |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | if (colorf_defined(background->solid.col)) { |
|---|
| 213 | Lib3dsChunk c; |
|---|
| 214 | c.chunk=LIB3DS_SOLID_BGND; |
|---|
| 215 | c.size=42; |
|---|
| 216 | lib3ds_chunk_write(&c,strm); |
|---|
| 217 | colorf_write(background->solid.col,strm); |
|---|
| 218 | } |
|---|
| 219 | |
|---|
| 220 | if (colorf_defined(background->gradient.top) || |
|---|
| 221 | colorf_defined(background->gradient.middle) || |
|---|
| 222 | colorf_defined(background->gradient.bottom)) { |
|---|
| 223 | Lib3dsChunk c; |
|---|
| 224 | c.chunk=LIB3DS_V_GRADIENT; |
|---|
| 225 | c.size=118; |
|---|
| 226 | lib3ds_chunk_write(&c,strm); |
|---|
| 227 | lib3ds_float_write(background->gradient.percent,strm); |
|---|
| 228 | colorf_write(background->gradient.top,strm); |
|---|
| 229 | colorf_write(background->gradient.middle,strm); |
|---|
| 230 | colorf_write(background->gradient.bottom,strm); |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | if (background->bitmap.use) { |
|---|
| 234 | Lib3dsChunk c; |
|---|
| 235 | c.chunk=LIB3DS_USE_BIT_MAP; |
|---|
| 236 | c.size=6; |
|---|
| 237 | lib3ds_chunk_write(&c,strm); |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | if (background->solid.use) { |
|---|
| 241 | Lib3dsChunk c; |
|---|
| 242 | c.chunk=LIB3DS_USE_SOLID_BGND; |
|---|
| 243 | c.size=6; |
|---|
| 244 | lib3ds_chunk_write(&c,strm); |
|---|
| 245 | } |
|---|
| 246 | |
|---|
| 247 | if (background->gradient.use) { |
|---|
| 248 | Lib3dsChunk c; |
|---|
| 249 | c.chunk=LIB3DS_USE_V_GRADIENT; |
|---|
| 250 | c.size=6; |
|---|
| 251 | lib3ds_chunk_write(&c,strm); |
|---|
| 252 | } |
|---|
| 253 | |
|---|
| 254 | return(LIB3DS_TRUE); |
|---|
| 255 | } |
|---|
| 256 | |
|---|
| 257 | |
|---|
| 258 | |
|---|
| 259 | |
|---|
| 260 | |
|---|
| 261 | |
|---|
| 262 | |
|---|
| 263 | |
|---|
| 264 | |
|---|