doxygen: prevent GPL license block from being parsed as doxygen comment.
[blender.git] / source / blender / imbuf / intern / thumbs_blend.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * Contributor(s): Campbell Barton.
21  *
22  * ***** END GPL LICENSE BLOCK *****
23  */
24
25 #include <string.h>
26
27 #include "zlib.h"
28
29 #include "BLI_utildefines.h"
30
31 #include "BKE_utildefines.h"
32 #include "BKE_global.h"
33
34 #include "IMB_imbuf_types.h"
35 #include "IMB_imbuf.h"
36 #include "IMB_thumbs.h"
37
38 #include "MEM_guardedalloc.h"
39
40 /* extracts the thumbnail from between the 'REND' and the 'GLOB'
41  * chunks of the header, dont use typical blend loader because its too slow */
42
43 static ImBuf *loadblend_thumb(gzFile gzfile)
44 {
45         char buf[12];
46         int bhead[24/sizeof(int)]; /* max size on 64bit */
47         char endian, pointer_size;
48         char endian_switch;
49         int sizeof_bhead ;
50
51         /* read the blend file header */
52         if(gzread(gzfile, buf, 12) != 12)
53                 return NULL;
54         if(strncmp(buf, "BLENDER", 7))
55                 return NULL;
56
57         if(buf[7]=='-')
58                 pointer_size= 8;
59         else if(buf[7]=='_')
60                 pointer_size= 4;
61         else
62                 return NULL;
63
64          sizeof_bhead = 16 + pointer_size;
65
66         if(buf[8]=='V')
67                 endian= B_ENDIAN; /* big: PPC */
68         else if(buf[8]=='v')
69                 endian= L_ENDIAN; /* little: x86 */
70         else
71                 return NULL;
72
73         endian_switch = ((ENDIAN_ORDER != endian)) ? 1 : 0;
74
75         while(gzread(gzfile, bhead, sizeof_bhead) == sizeof_bhead) {
76                 if(endian_switch)
77                         SWITCH_INT(bhead[1]); /* length */
78
79                 if (bhead[0]==REND) {
80                         gzseek(gzfile, bhead[1], SEEK_CUR); /* skip to the next */
81                 }
82                 else {
83                         break;
84                 }
85         }
86
87         /* using 'TEST' since new names segfault when loading in old blenders */
88         if(bhead[0] == TEST) {
89                 ImBuf *img= NULL;
90                 int size[2];
91
92                 if(gzread(gzfile, size, sizeof(size)) != sizeof(size))
93                         return NULL;
94
95                 if(endian_switch) {
96                         SWITCH_INT(size[0]);
97                         SWITCH_INT(size[1]);
98                 }
99                 /* length */
100                 bhead[1] -= sizeof(int) * 2;
101
102                 /* inconsistent image size, quit early */
103                 if(bhead[1] != size[0] * size[1] * sizeof(int))
104                         return NULL;
105         
106                 /* finally malloc and read the data */
107                 img= IMB_allocImBuf(size[0], size[1], 32, IB_rect | IB_metadata);
108         
109                 if(gzread(gzfile, img->rect, bhead[1]) != bhead[1]) {
110                         IMB_freeImBuf(img);
111                         img= NULL;
112                 }
113         
114                 return img;
115         }
116         
117         return NULL;
118 }
119
120 ImBuf *IMB_loadblend_thumb(const char *path)
121 {
122         gzFile gzfile;
123
124         /* not necessarily a gzip */
125         gzfile = gzopen(path, "rb");
126
127         if (NULL == gzfile ) {
128                 return NULL;
129         }
130         else {
131                 ImBuf *img= loadblend_thumb(gzfile);
132
133                 /* read ok! */
134                 gzclose(gzfile);
135
136                 return img;
137         }
138 }
139
140 /* add a fake passepartout overlay to a byte buffer, use for blend file thumbnails */
141 #define MARGIN 2
142
143 void IMB_overlayblend_thumb(unsigned int *thumb, int width, int height, float aspect)
144 {
145         unsigned char *px= (unsigned char *)thumb;
146         int margin_l = MARGIN;
147         int margin_b = MARGIN;
148         int margin_r = width - MARGIN;
149         int margin_t = height - MARGIN;
150
151         if(aspect < 1.0f) {
152                 margin_l= (int)((width - ((float)width * aspect)) / 2.0f);
153                 margin_l += MARGIN;
154                 CLAMP(margin_l, MARGIN, (width/2));
155                 margin_r = width - margin_l;
156         }
157         else if (aspect > 1.0f) {
158                 margin_b= (int)((height - ((float)height / aspect)) / 2.0f);
159                 margin_b += MARGIN;
160                 CLAMP(margin_b, MARGIN, (height/2));
161                 margin_t = height - margin_b;
162         }
163
164         {       
165                 int x, y;
166                 int stride_x= (margin_r - margin_l) - 2;
167                 
168                 for(y=0; y < height; y++) {
169                         for(x=0; x < width; x++, px+=4) {
170                                 int hline= 0, vline= 0;
171                                 if((x > margin_l && x < margin_r) && (y > margin_b && y < margin_t)) {
172                                         /* interior. skip */
173                                         x  += stride_x;
174                                         px += stride_x * 4;
175                                 } else if(      (hline=(((x == margin_l || x == margin_r)) && y >= margin_b && y <= margin_t)) ||
176                                                         (vline=(((y == margin_b || y == margin_t)) && x >= margin_l && x <= margin_r))
177                                 ) {
178                                         /* dashed line */
179                                         if((hline && y % 2) || (vline && x % 2)) {
180                                                 px[0]= px[1]= px[2]= 0;
181                                                 px[3] = 255;
182                                         }
183                                 }
184                                 else {
185                                         /* outside, fill in alpha, like passepartout */
186                                         px[0] *= 0.5f;
187                                         px[1] *= 0.5f;
188                                         px[2] *= 0.5f;
189                                         px[3] = (px[3] * 0.5f) + 96;
190                                 }
191                         }
192                 }
193         }
194 }