Clean up headers a bit more.
[blender.git] / source / blender / avi / intern / avirgb.c
1 /*
2  * $Id$
3  *
4  * This is external code. Converts rgb-type avi-s.
5  *
6  * ***** BEGIN GPL LICENSE BLOCK *****
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23  * All rights reserved.
24  *
25  * The Original Code is: all of this file.
26  *
27  * Contributor(s): none yet.
28  *
29  * ***** END GPL LICENSE BLOCK *****
30  *
31  */
32
33 #include "AVI_avi.h"
34 #include <stdlib.h>
35 #include <string.h>
36
37 #include "MEM_guardedalloc.h"
38 #include "avirgb.h"
39
40 #if defined(__sgi) || defined (__sparc) || defined (__sparc__) || defined (__PPC__) || defined (__ppc__) || defined (__hppa__) || defined (__BIG_ENDIAN__)
41 #define WORDS_BIGENDIAN
42 #endif
43
44
45 /* implementation */
46
47 void *avi_converter_from_avi_rgb (AviMovie *movie, int stream, unsigned char *buffer, int *size) {
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 WORDS_BIGENDIAN
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 WORDS_BIGENDIAN
74                         pxla= (unsigned char *)pxl;
75                         #endif
76
77                         x= movie->header->Width;
78                         while (x--) {
79                                 #ifdef WORDS_BIGENDIAN
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         } else {
98                 buf = MEM_mallocN (movie->header->Height * movie->header->Width * 3, "fromavirgbbuf");
99         
100                 rowstride = movie->header->Width*3;
101                 if (bits!=16) if (movie->header->Width%2) rowstride++;
102         
103                 for (y=0; y < movie->header->Height; y++) {
104                         memcpy (&buf[y*movie->header->Width*3], &buffer[((movie->header->Height-1)-y)*rowstride], movie->header->Width*3);
105                 }
106         
107                 for (y=0; y < movie->header->Height*movie->header->Width*3; y+=3) {
108                         i = buf[y];
109                         buf[y] = buf[y+2];
110                         buf[y+2] = i;
111                 }
112         
113                 MEM_freeN (buffer);
114         
115                 return buf;
116         }
117 }
118
119 void *avi_converter_to_avi_rgb (AviMovie *movie, int stream, unsigned char *buffer, int *size) {
120         int y, x, i, rowstride;
121         unsigned char *buf;
122
123         (void)stream; /* unused */
124
125         *size= movie->header->Height * movie->header->Width * 3;
126         if (movie->header->Width%2) *size+= movie->header->Height;
127         
128         buf = MEM_mallocN (*size,"toavirgbbuf");
129
130         rowstride = movie->header->Width*3;
131         if (movie->header->Width%2) rowstride++;
132
133         for (y=0; y < movie->header->Height; y++) {
134                 memcpy (&buf[y*rowstride], &buffer[((movie->header->Height-1)-y)*movie->header->Width*3], movie->header->Width*3);
135         }
136
137         for (y=0; y < movie->header->Height; y++) {
138                 for (x=0; x < movie->header->Width*3; x+=3) {
139                         i = buf[y*rowstride+x];
140                         buf[y*rowstride+x] = buf[y*rowstride+x+2];
141                         buf[y*rowstride+x+2] = i;
142                 }
143         }
144
145         MEM_freeN (buffer);
146
147         return buf;
148 }