2.5
[blender-staging.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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
38
39 #include "AVI_avi.h"
40 #include <stdlib.h>
41 #include <string.h>
42
43 #include "MEM_guardedalloc.h"
44 #include "avirgb.h"
45
46 #if defined(__sgi) || defined (__sparc) || defined (__sparc__) || defined (__PPC__) || defined (__ppc__) || defined (__hppa__) || defined (__BIG_ENDIAN__)
47 #define WORDS_BIGENDIAN
48 #endif
49
50
51 /* implementation */
52
53 void *avi_converter_from_avi_rgb (AviMovie *movie, int stream, unsigned char *buffer, int *size) {
54         int x, y,i, rowstride;
55         unsigned char *buf;
56         AviBitmapInfoHeader *bi;
57         short bits= 32;
58
59         bi= (AviBitmapInfoHeader *) movie->streams[stream].sf;
60         if (bi) bits= bi->BitCount;
61
62         if (bits==16) {
63                 unsigned short *pxl;
64                 unsigned char *to;
65                 #ifdef WORDS_BIGENDIAN
66                 unsigned char  *pxla;
67                 #endif            
68                 
69                 buf = MEM_mallocN (movie->header->Height * movie->header->Width * 3, "fromavirgbbuf");
70
71                 y= movie->header->Height;
72                 to= buf;
73                                 
74                 while (y--) {
75                         pxl= (unsigned short *) (buffer + y * movie->header->Width * 2);
76                         
77                         #ifdef WORDS_BIGENDIAN
78                         pxla= (unsigned char *)pxl;
79                         #endif
80
81                         x= movie->header->Width;
82                         while (x--) {
83                                 #ifdef WORDS_BIGENDIAN
84                                 i= pxla[0];
85                                 pxla[0]= pxla[1];
86                                 pxla[1]= i;
87         
88                                 pxla+=2;
89                                 #endif
90                         
91                                 *(to++)= ((*pxl>>10)&0x1f)*8;
92                                 *(to++)= ((*pxl>>5)&0x1f)*8;
93                                 *(to++)= (*pxl&0x1f)*8;
94                                 pxl++;  
95                         }
96                 }
97
98                 MEM_freeN (buffer);
99                 
100                 return buf;
101         } else {
102                 buf = MEM_mallocN (movie->header->Height * movie->header->Width * 3, "fromavirgbbuf");
103         
104                 rowstride = movie->header->Width*3;
105                 if (bits!=16) if (movie->header->Width%2) rowstride++;
106         
107                 for (y=0; y < movie->header->Height; y++) {
108                         memcpy (&buf[y*movie->header->Width*3], &buffer[((movie->header->Height-1)-y)*rowstride], movie->header->Width*3);
109                 }
110         
111                 for (y=0; y < movie->header->Height*movie->header->Width*3; y+=3) {
112                         i = buf[y];
113                         buf[y] = buf[y+2];
114                         buf[y+2] = i;
115                 }
116         
117                 MEM_freeN (buffer);
118         
119                 return buf;
120         }
121 }
122
123 void *avi_converter_to_avi_rgb (AviMovie *movie, int stream, unsigned char *buffer, int *size) {
124         int y, x, i, rowstride;
125         unsigned char *buf;
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 }