Merging r44227 through r45619 from trunk into soc-2011-tomato
[blender.git] / source / blender / avi / intern / avirgb.c
1 /*
2  *
3  * This is external code. Converts rgb-type avi-s.
4  *
5  * ***** BEGIN GPL LICENSE BLOCK *****
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
22  * All rights reserved.
23  *
24  * The Original Code is: all of this file.
25  *
26  * Contributor(s): none yet.
27  *
28  * ***** END GPL LICENSE BLOCK *****
29  *
30  */
31
32 /** \file blender/avi/intern/avirgb.c
33  *  \ingroup avi
34  */
35
36
37 #include "AVI_avi.h"
38 #include <stdlib.h>
39 #include <string.h>
40
41 #include "MEM_guardedalloc.h"
42 #include "avirgb.h"
43
44 /* implementation */
45
46 void *avi_converter_from_avi_rgb (AviMovie *movie, int stream, unsigned char *buffer, int *size)
47 {
48         int x, y,i, rowstride;
49         unsigned char *buf;
50         AviBitmapInfoHeader *bi;
51         short bits= 32;
52         
53         (void)size; /* unused */
54
55         bi= (AviBitmapInfoHeader *) movie->streams[stream].sf;
56         if (bi) bits= bi->BitCount;
57
58         if (bits==16) {
59                 unsigned short *pxl;
60                 unsigned char *to;
61 #ifdef __BIG_ENDIAN__
62                 unsigned char  *pxla;
63 #endif            
64                 
65                 buf = MEM_mallocN (movie->header->Height * movie->header->Width * 3, "fromavirgbbuf");
66
67                 y= movie->header->Height;
68                 to= buf;
69                                 
70                 while (y--) {
71                         pxl= (unsigned short *) (buffer + y * movie->header->Width * 2);
72                         
73 #ifdef __BIG_ENDIAN__
74                         pxla= (unsigned char *)pxl;
75 #endif
76
77                         x= movie->header->Width;
78                         while (x--) {
79 #ifdef __BIG_ENDIAN__
80                                 i= pxla[0];
81                                 pxla[0]= pxla[1];
82                                 pxla[1]= i;
83         
84                                 pxla+=2;
85 #endif
86                         
87                                 *(to++)= ((*pxl>>10)&0x1f)*8;
88                                 *(to++)= ((*pxl>>5)&0x1f)*8;
89                                 *(to++)= (*pxl&0x1f)*8;
90                                 pxl++;  
91                         }
92                 }
93
94                 MEM_freeN (buffer);
95                 
96                 return buf;
97         }
98         else {
99                 buf = MEM_mallocN (movie->header->Height * movie->header->Width * 3, "fromavirgbbuf");
100         
101                 rowstride = movie->header->Width*3;
102                 if (bits!=16) if (movie->header->Width%2) rowstride++;
103         
104                 for (y=0; y < movie->header->Height; y++) {
105                         memcpy (&buf[y*movie->header->Width*3], &buffer[((movie->header->Height-1)-y)*rowstride], movie->header->Width*3);
106                 }
107         
108                 for (y=0; y < movie->header->Height*movie->header->Width*3; y+=3) {
109                         i = buf[y];
110                         buf[y] = buf[y+2];
111                         buf[y+2] = i;
112                 }
113         
114                 MEM_freeN (buffer);
115         
116                 return buf;
117         }
118 }
119
120 void *avi_converter_to_avi_rgb (AviMovie *movie, int stream, unsigned char *buffer, int *size)
121 {
122         int y, x, i, rowstride;
123         unsigned char *buf;
124
125         (void)stream; /* unused */
126
127         *size= movie->header->Height * movie->header->Width * 3;
128         if (movie->header->Width%2) *size+= movie->header->Height;
129         
130         buf = MEM_mallocN (*size,"toavirgbbuf");
131
132         rowstride = movie->header->Width*3;
133         if (movie->header->Width%2) rowstride++;
134
135         for (y=0; y < movie->header->Height; y++) {
136                 memcpy (&buf[y*rowstride], &buffer[((movie->header->Height-1)-y)*movie->header->Width*3], movie->header->Width*3);
137         }
138
139         for (y=0; y < movie->header->Height; y++) {
140                 for (x=0; x < movie->header->Width*3; x+=3) {
141                         i = buf[y*rowstride+x];
142                         buf[y*rowstride+x] = buf[y*rowstride+x+2];
143                         buf[y*rowstride+x+2] = i;
144                 }
145         }
146
147         MEM_freeN (buffer);
148
149         return buf;
150 }