Object tracking: configurable scale for object solution
[blender-staging.git] / source / blender / blenloader / intern / readblenentry.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  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  * .blend file reading entry point
27  */
28
29 /** \file blender/blenloader/intern/readblenentry.c
30  *  \ingroup blenloader
31  */
32
33
34 #include <stddef.h>
35
36 #include <stdlib.h>
37 #include <string.h>
38 #include <stdio.h>
39 #include <math.h>
40
41 #include "MEM_guardedalloc.h"
42
43 #include "BLI_fileops.h"
44 #include "BLI_ghash.h"
45 #include "BLI_linklist.h"
46 #include "BLI_listbase.h"
47 #include "BLI_string.h"
48 #include "BLI_utildefines.h"
49
50 #include "DNA_genfile.h"
51 #include "DNA_sdna_types.h"
52
53
54 #include "BKE_main.h"
55 #include "BKE_library.h" // for free_main
56 #include "BKE_idcode.h"
57 #include "BKE_report.h"
58 #include "BKE_utildefines.h"
59
60 #include "BLO_readfile.h"
61 #include "BLO_undofile.h"
62
63 #include "readfile.h"
64
65 #include "BLO_sys_types.h" // needed for intptr_t
66
67 #ifdef _WIN32
68 #include "BLI_winstuff.h"
69 #endif
70
71 /* local prototypes --------------------- */
72 void BLO_blendhandle_print_sizes(BlendHandle *, void *); 
73
74         /* Access routines used by filesel. */
75          
76 BlendHandle *BLO_blendhandle_from_file(char *file, ReportList *reports)
77 {
78         BlendHandle *bh;
79
80         bh= (BlendHandle*)blo_openblenderfile(file, reports);
81
82         return bh;
83 }
84
85 BlendHandle *BLO_blendhandle_from_memory(void *mem, int memsize)
86 {
87         BlendHandle *bh;
88
89         bh= (BlendHandle*)blo_openblendermemory(mem, memsize, NULL);
90
91         return bh;
92 }
93
94 void BLO_blendhandle_print_sizes(BlendHandle *bh, void *fp) 
95 {
96         FileData *fd= (FileData*) bh;
97         BHead *bhead;
98
99         fprintf(fp, "[\n");
100         for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
101                 if (bhead->code==ENDB)
102                         break;
103                 else {
104                         short *sp= fd->filesdna->structs[bhead->SDNAnr];
105                         char *name= fd->filesdna->types[ sp[0] ];
106                         char buf[4];
107                         
108                         buf[0]= (bhead->code>>24)&0xFF;
109                         buf[1]= (bhead->code>>16)&0xFF;
110                         buf[2]= (bhead->code>>8)&0xFF;
111                         buf[3]= (bhead->code>>0)&0xFF;
112                         
113                         buf[0]= buf[0]?buf[0]:' ';
114                         buf[1]= buf[1]?buf[1]:' ';
115                         buf[2]= buf[2]?buf[2]:' ';
116                         buf[3]= buf[3]?buf[3]:' ';
117                         
118                         fprintf(fp, "['%.4s', '%s', %d, %ld ], \n", buf, name, bhead->nr, (long int)(bhead->len+sizeof(BHead)));
119                 }
120         }
121         fprintf(fp, "]\n");
122 }
123
124 LinkNode *BLO_blendhandle_get_datablock_names(BlendHandle *bh, int ofblocktype, int *tot_names)
125 {
126         FileData *fd= (FileData*) bh;
127         LinkNode *names= NULL;
128         BHead *bhead;
129         int tot= 0;
130
131         for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
132                 if (bhead->code==ofblocktype) {
133                         char *idname= bhead_id_name(fd, bhead);
134
135                         BLI_linklist_prepend(&names, strdup(idname+2));
136                         tot++;
137                 } else if (bhead->code==ENDB)
138                         break;
139         }
140
141         *tot_names= tot;
142         return names;
143 }
144
145 LinkNode *BLO_blendhandle_get_previews(BlendHandle *bh, int ofblocktype, int *tot_prev)
146 {
147         FileData *fd= (FileData*) bh;
148         LinkNode *previews= NULL;
149         BHead *bhead;
150         int looking=0;
151         PreviewImage* prv = NULL;
152         PreviewImage* new_prv = NULL;
153         int tot= 0;
154         
155         for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
156                 if (bhead->code==ofblocktype) {
157                         char *idname= bhead_id_name(fd, bhead);
158                         switch(GS(idname))
159                         {
160                                 case ID_MA: /* fall through */
161                                 case ID_TE: /* fall through */
162                                 case ID_IM: /* fall through */
163                                 case ID_WO: /* fall through */
164                                 case ID_LA: /* fall through */
165                                         new_prv = MEM_callocN(sizeof(PreviewImage), "newpreview");
166                                         BLI_linklist_prepend(&previews, new_prv);
167                                         tot++;
168                                         looking = 1;
169                                         break;
170                                 default:
171                                         break;
172                         }
173                 } else if (bhead->code==DATA) {
174                         if (looking) {
175                                 if (bhead->SDNAnr == DNA_struct_find_nr(fd->filesdna, "PreviewImage") ) {
176                                         prv = BLO_library_read_struct(fd, bhead, "PreviewImage");       
177                                         if (prv) {
178                                                 memcpy(new_prv, prv, sizeof(PreviewImage));
179                                                 if (prv->rect[0]) {
180                                                         unsigned int *rect = NULL;
181                                                         new_prv->rect[0] = MEM_callocN(new_prv->w[0]*new_prv->h[0]*sizeof(unsigned int), "prvrect");
182                                                         bhead= blo_nextbhead(fd, bhead);
183                                                         rect = (unsigned int*)(bhead+1);
184                                                         memcpy(new_prv->rect[0], rect, bhead->len);                                     
185                                                 } else {
186                                                         new_prv->rect[0] = NULL;
187                                                 }
188                                                 
189                                                 if (prv->rect[1]) {
190                                                         unsigned int *rect = NULL;
191                                                         new_prv->rect[1] = MEM_callocN(new_prv->w[1]*new_prv->h[1]*sizeof(unsigned int), "prvrect");
192                                                         bhead= blo_nextbhead(fd, bhead);
193                                                         rect = (unsigned int*)(bhead+1);
194                                                         memcpy(new_prv->rect[1], rect, bhead->len);                                                     
195                                                 } else {
196                                                         new_prv->rect[1] = NULL;
197                                                 }
198                                                 MEM_freeN(prv);
199                                         }
200                                 }
201                         }
202                 } else if (bhead->code==ENDB) {
203                         break;
204                 } else {
205                         looking = 0;
206                         new_prv = NULL;
207                         prv = NULL;
208                 }
209                 
210         }
211
212         *tot_prev= tot;
213         return previews;
214 }
215
216 LinkNode *BLO_blendhandle_get_linkable_groups(BlendHandle *bh) 
217 {
218         FileData *fd= (FileData*) bh;
219         GHash *gathered= BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, "linkable_groups gh");
220         LinkNode *names= NULL;
221         BHead *bhead;
222         
223         for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
224                 if (bhead->code==ENDB) {
225                         break;
226                 } else if (BKE_idcode_is_valid(bhead->code)) {
227                         if (BKE_idcode_is_linkable(bhead->code)) {
228                                 const char *str= BKE_idcode_to_name(bhead->code);
229                                 
230                                 if (!BLI_ghash_haskey(gathered, (void *)str)) {
231                                         BLI_linklist_prepend(&names, strdup(str));
232                                         BLI_ghash_insert(gathered, (void *)str, NULL);
233                                 }
234                         }
235                 }
236         }
237         
238         BLI_ghash_free(gathered, NULL, NULL);
239         
240         return names;
241 }               
242
243 void BLO_blendhandle_close(BlendHandle *bh)
244 {
245         FileData *fd= (FileData*) bh;
246         
247         blo_freefiledata(fd);
248 }
249
250         /**********/
251
252 BlendFileData *BLO_read_from_file(const char *filepath, ReportList *reports)
253 {
254         BlendFileData *bfd = NULL;
255         FileData *fd;
256                 
257         fd = blo_openblenderfile(filepath, reports);
258         if (fd) {
259                 fd->reports= reports;
260                 bfd= blo_read_file_internal(fd, filepath);
261                 blo_freefiledata(fd);                   
262         }
263
264         return bfd;     
265 }
266
267 BlendFileData *BLO_read_from_memory(void *mem, int memsize, ReportList *reports)
268 {
269         BlendFileData *bfd = NULL;
270         FileData *fd;
271                 
272         fd = blo_openblendermemory(mem, memsize,  reports);
273         if (fd) {
274                 fd->reports= reports;
275                 bfd= blo_read_file_internal(fd, "");
276                 blo_freefiledata(fd);                   
277         }
278
279         return bfd;     
280 }
281
282 BlendFileData *BLO_read_from_memfile(Main *oldmain, const char *filename, MemFile *memfile, ReportList *reports)
283 {
284         BlendFileData *bfd = NULL;
285         FileData *fd;
286         ListBase mainlist;
287         
288         fd = blo_openblendermemfile(memfile, reports);
289         if (fd) {
290                 fd->reports= reports;
291                 BLI_strncpy(fd->relabase, filename, sizeof(fd->relabase));
292                 
293                 /* clear ob->proxy_from pointers in old main */
294                 blo_clear_proxy_pointers_from_lib(oldmain);
295
296                 /* separate libraries from old main */
297                 blo_split_main(&mainlist, oldmain);
298                 /* add the library pointers in oldmap lookup */
299                 blo_add_library_pointer_map(&mainlist, fd);
300                 
301                 /* makes lookup of existing images in old main */
302                 blo_make_image_pointer_map(fd, oldmain);
303                 
304                 /* makes lookup of existing video clips in old main */
305                 blo_make_movieclip_pointer_map(fd, oldmain);
306                 
307                 bfd= blo_read_file_internal(fd, filename);
308                 
309                 /* ensures relinked images are not freed */
310                 blo_end_image_pointer_map(fd, oldmain);
311                 
312                 /* ensures relinked movie clips are not freed */
313                 blo_end_movieclip_pointer_map(fd, oldmain);
314                 
315                 /* move libraries from old main to new main */
316                 if(bfd && mainlist.first!=mainlist.last) {
317                         
318                         /* Library structs themselves */
319                         bfd->main->library= oldmain->library;
320                         oldmain->library.first= oldmain->library.last= NULL;
321                         
322                         /* add the Library mainlist to the new main */
323                         BLI_remlink(&mainlist, oldmain);
324                         BLI_addhead(&mainlist, bfd->main);
325                 }
326                 blo_join_main(&mainlist);
327                 
328                 blo_freefiledata(fd);                   
329         }
330
331         return bfd;     
332 }
333
334 void BLO_blendfiledata_free(BlendFileData *bfd)
335 {
336         if (bfd->main) {
337                 free_main(bfd->main);
338         }
339         
340         if (bfd->user) {
341                 MEM_freeN(bfd->user);
342         }
343
344         MEM_freeN(bfd);
345 }
346