9e40b00adfcb7b2367fad1d21af0d0ff7b8c7368
[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 /** \file blender/avi/intern/avirgb.c
34  *  \ingroup avi
35  */
36
37
38 #include "AVI_avi.h"
39 #include <stdlib.h>
40 #include <string.h>
41
42 #include "MEM_guardedalloc.h"
43 #include "avirgb.h"
44
45 #ifdef __BIG_ENDIAN__
46 #  define WORDS_BIGENDIAN
47 #endif
48
49
50 /* implementation */
51
52 void *avi_converter_from_avi_rgb (AviMovie *movie, int stream, unsigned char *buffer, int *size) {
53         int x, y,i, rowstride;
54         unsigned char *buf;
55         AviBitmapInfoHeader *bi;
56         short bits= 32;
57         
58         (void)size; /* unused */
59
60         bi= (AviBitmapInfoHeader *) movie->streams[stream].sf;
61         if (bi) bits= bi->BitCount;
62
63         if (bits==16) {
64                 unsigned short *pxl;
65                 unsigned char *to;
66                 #ifdef WORDS_BIGENDIAN
67                 unsigned char  *pxla;
68                 #endif            
69                 
70                 buf = MEM_mallocN (movie->header->Height * movie->header->Width * 3, "fromavirgbbuf");
71
72                 y= movie->header->Height;
73                 to= buf;
74                                 
75                 while (y--) {
76                         pxl= (unsigned short *) (buffer + y * movie->header->Width * 2);
77                         
78                         #ifdef WORDS_BIGENDIAN
79                         pxla= (unsigned char *)pxl;
80                         #endif
81
82                         x= movie->header->Width;
83                         while (x--) {
84                                 #ifdef WORDS_BIGENDIAN
85                                 i= pxla[0];
86                                 pxla[0]= pxla[1];
87                                 pxla[1]= i;
88         
89                                 pxla+=2;
90                                 #endif
91                         
92                                 *(to++)= ((*pxl>>10)&0x1f)*8;
93                                 *(to++)= ((*pxl>>5)&0x1f)*8;
94                                 *(to++)= (*pxl&0x1f)*8;
95                                 pxl++;  
96                         }
97                 }
98
99                 MEM_freeN (buffer);
100                 
101                 return buf;
102         } else {
103                 buf = MEM_mallocN (movie->header->Height * movie->header->Width * 3, "fromavirgbbuf");
104         
105                 rowstride = movie->header->Width*3;
106                 if (bits!=16) if (movie->header->Width%2) rowstride++;
107         
108                 for (y=0; y < movie->header->Height; y++) {
109                         memcpy (&buf[y*movie->header->Width*3], &buffer[((movie->header->Height-1)-y)*rowstride], movie->header->Width*3);
110                 }
111         
112                 for (y=0; y < movie->header->Height*movie->header->Width*3; y+=3) {
113                         i = buf[y];
114                         buf[y] = buf[y+2];
115                         buf[y+2] = i;
116                 }
117         
118                 MEM_freeN (buffer);
119         
120                 return buf;
121         }
122 }
123
124 void *avi_converter_to_avi_rgb (AviMovie *movie, int stream, unsigned char *buffer, int *size) {
125         int y, x, i, rowstride;
126         unsigned char *buf;
127
128         (void)stream; /* unused */
129
130         *size= movie->header->Height * movie->header->Width * 3;
131         if (movie->header->Width%2) *size+= movie->header->Height;
132         
133         buf = MEM_mallocN (*size,"toavirgbbuf");
134
135         rowstride = movie->header->Width*3;
136         if (movie->header->Width%2) rowstride++;
137
138         for (y=0; y < movie->header->Height; y++) {
139                 memcpy (&buf[y*rowstride], &buffer[((movie->header->Height-1)-y)*movie->header->Width*3], movie->header->Width*3);
140         }
141
142         for (y=0; y < movie->header->Height; y++) {
143                 for (x=0; x < movie->header->Width*3; x+=3) {
144                         i = buf[y*rowstride+x];
145                         buf[y*rowstride+x] = buf[y*rowstride+x+2];
146                         buf[y*rowstride+x+2] = i;
147                 }
148         }
149
150         MEM_freeN (buffer);
151
152         return buf;
153 }