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