remove $Id: tags after discussion on the mailign list: http://markmail.org/message...
[blender.git] / source / blender / blenkernel / intern / customdata_file.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * ***** END GPL LICENSE BLOCK *****
19  */
20
21 /** \file blender/blenkernel/intern/customdata_file.c
22  *  \ingroup bke
23  */
24
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include "MEM_guardedalloc.h"
31
32 #include "BLI_fileops.h"
33 #include "BLI_string.h"
34 #include "BLI_utildefines.h"
35
36 #include "BKE_customdata_file.h"
37 #include "BKE_global.h"
38
39
40 /************************* File Format Definitions ***************************/
41
42 #define CDF_ENDIAN_LITTLE       0
43 #define CDF_ENDIAN_BIG          1
44
45 #define CDF_DATA_FLOAT  0
46
47 typedef struct CDataFileHeader {
48         char ID[4];                                     /* "BCDF" */
49         char endian;                            /* little, big */
50         char version;                           /* non-compatible versions */
51         char subversion;                        /* compatible sub versions */
52         char pad;                                       /* padding */
53
54         int structbytes;                        /* size of this struct in bytes */
55         int type;                                       /* image, mesh */
56         int totlayer;                           /* number of layers in the file */
57 } CDataFileHeader;
58
59 typedef struct CDataFileImageHeader {
60         int structbytes;                        /* size of this struct in bytes */
61         int width;                                      /* image width */
62         int height;                                     /* image height */
63         int tile_size;                          /* tile size (required power of 2) */
64 } CDataFileImageHeader;
65
66 typedef struct CDataFileMeshHeader {
67         int structbytes;                        /* size of this struct in bytes */
68 } CDataFileMeshHeader;
69
70 struct CDataFileLayer {
71         int structbytes;                                /* size of this struct in bytes */
72         int datatype;                                   /* only float for now */
73         uint64_t datasize;                              /* size of data in layer */
74         int type;                                               /* layer type */
75         char name[CDF_LAYER_NAME_MAX];  /* layer name */
76 };
77
78 /**************************** Other Definitions ******************************/
79
80 #define CDF_VERSION                     0
81 #define CDF_SUBVERSION          0
82 #define CDF_TILE_SIZE           64
83
84 struct CDataFile {
85         int type;
86
87         CDataFileHeader header;
88         union {
89                 CDataFileImageHeader image;
90                 CDataFileMeshHeader mesh;
91         } btype;
92
93         CDataFileLayer *layer;
94         int totlayer;
95
96         FILE *readf;
97         FILE *writef;
98         int switchendian;
99         size_t dataoffset;
100 };
101
102 /********************************* Create/Free *******************************/
103
104 static int cdf_endian(void)
105 {
106         if(ENDIAN_ORDER == L_ENDIAN)
107                 return CDF_ENDIAN_LITTLE;
108         else
109                 return CDF_ENDIAN_BIG;
110 }
111
112 /*static int cdf_data_type_size(int datatype)
113 {
114         if(datatype == CDF_DATA_FLOAT)
115                 return sizeof(float);
116         
117         return 0;
118 }*/
119
120 CDataFile *cdf_create(int type)
121 {
122         CDataFile *cdf= MEM_callocN(sizeof(CDataFile), "CDataFile");
123
124         cdf->type= type;
125
126         return cdf;
127 }
128
129 void cdf_free(CDataFile *cdf)
130 {
131         cdf_read_close(cdf);
132         cdf_write_close(cdf);
133
134         if(cdf->layer)
135                 MEM_freeN(cdf->layer);
136
137         MEM_freeN(cdf);
138 }
139
140 /********************************* Read/Write ********************************/
141
142 static int cdf_read_header(CDataFile *cdf)
143 {
144         CDataFileHeader *header;
145         CDataFileImageHeader *image;
146         CDataFileMeshHeader *mesh;
147         CDataFileLayer *layer;
148         FILE *f= cdf->readf;
149         size_t offset = 0;
150         int a;
151
152         header= &cdf->header;
153
154         if(!fread(header, sizeof(CDataFileHeader), 1, cdf->readf))
155                 return 0;
156         
157         if(memcmp(header->ID, "BCDF", sizeof(header->ID)) != 0)
158                 return 0;
159         if(header->version > CDF_VERSION)
160                 return 0;
161
162         cdf->switchendian= header->endian != cdf_endian();
163         header->endian= cdf_endian();
164
165         if(cdf->switchendian) {
166                 SWITCH_INT(header->type);
167                 SWITCH_INT(header->totlayer);
168                 SWITCH_INT(header->structbytes);
169         }
170
171         if(!ELEM(header->type, CDF_TYPE_IMAGE, CDF_TYPE_MESH))
172                 return 0;
173
174         offset += header->structbytes;
175         header->structbytes= sizeof(CDataFileHeader);
176
177         if(fseek(f, offset, SEEK_SET) != 0)
178                 return 0;
179         
180         if(header->type == CDF_TYPE_IMAGE) {
181                 image= &cdf->btype.image;
182                 if(!fread(image, sizeof(CDataFileImageHeader), 1, f))
183                         return 0;
184
185                 if(cdf->switchendian) {
186                         SWITCH_INT(image->width);
187                         SWITCH_INT(image->height);
188                         SWITCH_INT(image->tile_size);
189                         SWITCH_INT(image->structbytes);
190                 }
191
192                 offset += image->structbytes;
193                 image->structbytes= sizeof(CDataFileImageHeader);
194         }
195         else if(header->type == CDF_TYPE_MESH) {
196                 mesh= &cdf->btype.mesh;
197                 if(!fread(mesh, sizeof(CDataFileMeshHeader), 1, f))
198                         return 0;
199
200                 if(cdf->switchendian)
201                         SWITCH_INT(mesh->structbytes);
202
203                 offset += mesh->structbytes;
204                 mesh->structbytes= sizeof(CDataFileMeshHeader);
205         }
206
207         if(fseek(f, offset, SEEK_SET) != 0)
208                 return 0;
209
210         cdf->layer= MEM_callocN(sizeof(CDataFileLayer)*header->totlayer, "CDataFileLayer");
211         cdf->totlayer= header->totlayer;
212
213         for(a=0; a<header->totlayer; a++) {
214                 layer= &cdf->layer[a];
215
216                 if(!fread(layer, sizeof(CDataFileLayer), 1, f))
217                         return 0;
218
219                 if(cdf->switchendian) {
220                         SWITCH_INT(layer->type);
221                         SWITCH_INT(layer->datatype);
222                         SWITCH_INT64(layer->datasize);
223                         SWITCH_INT(layer->structbytes);
224                 }
225
226                 if(layer->datatype != CDF_DATA_FLOAT)
227                         return 0;
228
229                 offset += layer->structbytes;
230                 layer->structbytes= sizeof(CDataFileLayer);
231
232                 if(fseek(f, offset, SEEK_SET) != 0)
233                         return 0;
234         }
235
236         cdf->dataoffset= offset;
237
238         return 1;
239 }
240
241 static int cdf_write_header(CDataFile *cdf)
242 {
243         CDataFileHeader *header;
244         CDataFileImageHeader *image;
245         CDataFileMeshHeader *mesh;
246         CDataFileLayer *layer;
247         FILE *f= cdf->writef;
248         int a;
249
250         header= &cdf->header;
251
252         if(!fwrite(header, sizeof(CDataFileHeader), 1, f))
253                 return 0;
254         
255         if(header->type == CDF_TYPE_IMAGE) {
256                 image= &cdf->btype.image;
257                 if(!fwrite(image, sizeof(CDataFileImageHeader), 1, f))
258                         return 0;
259         }
260         else if(header->type == CDF_TYPE_MESH) {
261                 mesh= &cdf->btype.mesh;
262                 if(!fwrite(mesh, sizeof(CDataFileMeshHeader), 1, f))
263                         return 0;
264         }
265
266         for(a=0; a<header->totlayer; a++) {
267                 layer= &cdf->layer[a];
268
269                 if(!fwrite(layer, sizeof(CDataFileLayer), 1, f))
270                         return 0;
271         }
272
273         return 1;
274 }
275
276 int cdf_read_open(CDataFile *cdf, char *filename)
277 {
278         FILE *f;
279
280         f= fopen(filename, "rb");
281         if(!f)
282                 return 0;
283         
284         cdf->readf= f;
285
286         if(!cdf_read_header(cdf)) {
287                 cdf_read_close(cdf);
288                 return 0;
289         }
290
291         if(cdf->header.type != cdf->type) {
292                 cdf_read_close(cdf);
293                 return 0;
294         }
295
296         return 1;
297 }
298
299 int cdf_read_layer(CDataFile *cdf, CDataFileLayer *blay)
300 {
301         size_t offset;
302         int a;
303
304         /* seek to right location in file */
305         offset= cdf->dataoffset;
306         for(a=0; a<cdf->totlayer; a++) {
307                 if(&cdf->layer[a] == blay)
308                         break;
309                 else
310                         offset += cdf->layer[a].datasize;
311         }
312
313         return (fseek(cdf->readf, offset, SEEK_SET) == 0);
314 }
315
316 int cdf_read_data(CDataFile *cdf, unsigned int size, void *data)
317 {
318         float *fdata;
319         unsigned int a;
320
321         /* read data */
322         if(!fread(data, size, 1, cdf->readf))
323                 return 0;
324
325         /* switch endian if necessary */
326         if(cdf->switchendian) {
327                 fdata= data;
328
329                 for(a=0; a<size/sizeof(float); a++)
330                         SWITCH_INT(fdata[a])
331         }
332
333         return 1;
334 }
335
336 void cdf_read_close(CDataFile *cdf)
337 {
338         if(cdf->readf) {
339                 fclose(cdf->readf);
340                 cdf->readf= NULL;
341         }
342 }
343
344 int cdf_write_open(CDataFile *cdf, char *filename)
345 {
346         CDataFileHeader *header;
347         CDataFileImageHeader *image;
348         CDataFileMeshHeader *mesh;
349         FILE *f;
350
351         f= fopen(filename, "wb");
352         if(!f)
353                 return 0;
354         
355         cdf->writef= f;
356
357         /* fill header */
358         header= &cdf->header;
359         /* strcpy(, "BCDF"); // terminator out of range */
360         header->ID[0]= 'B'; header->ID[1]= 'C'; header->ID[2]= 'D'; header->ID[3]= 'F';
361         header->endian= cdf_endian();
362         header->version= CDF_VERSION;
363         header->subversion= CDF_SUBVERSION;
364
365         header->structbytes= sizeof(CDataFileHeader);
366         header->type= cdf->type;
367         header->totlayer= cdf->totlayer;
368
369         if(cdf->type == CDF_TYPE_IMAGE) {
370                 /* fill image header */
371                 image= &cdf->btype.image;
372                 image->structbytes= sizeof(CDataFileImageHeader);
373                 image->tile_size= CDF_TILE_SIZE;
374         }
375         else if(cdf->type == CDF_TYPE_MESH) {
376                 /* fill mesh header */
377                 mesh= &cdf->btype.mesh;
378                 mesh->structbytes= sizeof(CDataFileMeshHeader);
379         }
380
381         cdf_write_header(cdf);
382
383         return 1;
384 }
385
386 int cdf_write_layer(CDataFile *UNUSED(cdf), CDataFileLayer *UNUSED(blay))
387 {
388         return 1;
389 }
390
391 int cdf_write_data(CDataFile *cdf, unsigned int size, void *data)
392 {
393         /* write data */
394         if(!fwrite(data, size, 1, cdf->writef))
395                 return 0;
396
397         return 1;
398 }
399
400 void cdf_write_close(CDataFile *cdf)
401 {
402         if(cdf->writef) {
403                 fclose(cdf->writef);
404                 cdf->writef= NULL;
405         }
406 }
407
408 void cdf_remove(char *filename)
409 {
410         BLI_delete(filename, 0, 0);
411 }
412
413 /********************************** Layers ***********************************/
414
415 CDataFileLayer *cdf_layer_find(CDataFile *cdf, int type, char *name)
416 {
417         CDataFileLayer *layer;
418         int a;
419
420         for(a=0; a<cdf->totlayer; a++) {
421                 layer= &cdf->layer[a];
422
423                 if(layer->type == type && strcmp(layer->name, name) == 0)
424                         return layer;
425         }
426         
427         return NULL;
428 }
429
430 CDataFileLayer *cdf_layer_add(CDataFile *cdf, int type, char *name, size_t datasize)
431 {
432         CDataFileLayer *newlayer, *layer;
433
434         /* expand array */
435         newlayer= MEM_callocN(sizeof(CDataFileLayer)*(cdf->totlayer+1), "CDataFileLayer");
436         memcpy(newlayer, cdf->layer, sizeof(CDataFileLayer)*cdf->totlayer);
437         cdf->layer= newlayer;
438
439         cdf->totlayer++;
440
441         /* fill in new layer */
442         layer= &cdf->layer[cdf->totlayer-1];
443         layer->structbytes= sizeof(CDataFileLayer);
444         layer->datatype= CDF_DATA_FLOAT;
445         layer->datasize= datasize;
446         layer->type= type;
447         BLI_strncpy(layer->name, name, CDF_LAYER_NAME_MAX);
448
449         return layer;
450 }
451