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