Blender  V2.59
avirgb.c
Go to the documentation of this file.
00001 /*
00002  * $Id: avirgb.c 35249 2011-02-27 20:43:42Z jesterking $
00003  *
00004  * This is external code. Converts rgb-type avi-s.
00005  *
00006  * ***** BEGIN GPL LICENSE BLOCK *****
00007  *
00008  * This program is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU General Public License
00010  * as published by the Free Software Foundation; either version 2
00011  * of the License, or (at your option) any later version.
00012  *
00013  * This program is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software Foundation,
00020  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00021  *
00022  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00023  * All rights reserved.
00024  *
00025  * The Original Code is: all of this file.
00026  *
00027  * Contributor(s): none yet.
00028  *
00029  * ***** END GPL LICENSE BLOCK *****
00030  *
00031  */
00032 
00038 #include "AVI_avi.h"
00039 #include <stdlib.h>
00040 #include <string.h>
00041 
00042 #include "MEM_guardedalloc.h"
00043 #include "avirgb.h"
00044 
00045 #if defined(__sgi) || defined (__sparc) || defined (__sparc__) || defined (__PPC__) || defined (__ppc__) || defined (__hppa__) || defined (__BIG_ENDIAN__)
00046 #define WORDS_BIGENDIAN
00047 #endif
00048 
00049 
00050 /* implementation */
00051 
00052 void *avi_converter_from_avi_rgb (AviMovie *movie, int stream, unsigned char *buffer, int *size) {
00053         int x, y,i, rowstride;
00054         unsigned char *buf;
00055         AviBitmapInfoHeader *bi;
00056         short bits= 32;
00057         
00058         (void)size; /* unused */
00059 
00060         bi= (AviBitmapInfoHeader *) movie->streams[stream].sf;
00061         if (bi) bits= bi->BitCount;
00062 
00063         if (bits==16) {
00064                 unsigned short *pxl;
00065                 unsigned char *to;
00066                 #ifdef WORDS_BIGENDIAN
00067                 unsigned char  *pxla;
00068                 #endif            
00069                 
00070                 buf = MEM_mallocN (movie->header->Height * movie->header->Width * 3, "fromavirgbbuf");
00071 
00072                 y= movie->header->Height;
00073                 to= buf;
00074                                 
00075                 while (y--) {
00076                         pxl= (unsigned short *) (buffer + y * movie->header->Width * 2);
00077                         
00078                         #ifdef WORDS_BIGENDIAN
00079                         pxla= (unsigned char *)pxl;
00080                         #endif
00081 
00082                         x= movie->header->Width;
00083                         while (x--) {
00084                                 #ifdef WORDS_BIGENDIAN
00085                                 i= pxla[0];
00086                                 pxla[0]= pxla[1];
00087                                 pxla[1]= i;
00088         
00089                                 pxla+=2;
00090                                 #endif
00091                         
00092                                 *(to++)= ((*pxl>>10)&0x1f)*8;
00093                                 *(to++)= ((*pxl>>5)&0x1f)*8;
00094                                 *(to++)= (*pxl&0x1f)*8;
00095                                 pxl++;  
00096                         }
00097                 }
00098 
00099                 MEM_freeN (buffer);
00100                 
00101                 return buf;
00102         } else {
00103                 buf = MEM_mallocN (movie->header->Height * movie->header->Width * 3, "fromavirgbbuf");
00104         
00105                 rowstride = movie->header->Width*3;
00106                 if (bits!=16) if (movie->header->Width%2) rowstride++;
00107         
00108                 for (y=0; y < movie->header->Height; y++) {
00109                         memcpy (&buf[y*movie->header->Width*3], &buffer[((movie->header->Height-1)-y)*rowstride], movie->header->Width*3);
00110                 }
00111         
00112                 for (y=0; y < movie->header->Height*movie->header->Width*3; y+=3) {
00113                         i = buf[y];
00114                         buf[y] = buf[y+2];
00115                         buf[y+2] = i;
00116                 }
00117         
00118                 MEM_freeN (buffer);
00119         
00120                 return buf;
00121         }
00122 }
00123 
00124 void *avi_converter_to_avi_rgb (AviMovie *movie, int stream, unsigned char *buffer, int *size) {
00125         int y, x, i, rowstride;
00126         unsigned char *buf;
00127 
00128         (void)stream; /* unused */
00129 
00130         *size= movie->header->Height * movie->header->Width * 3;
00131         if (movie->header->Width%2) *size+= movie->header->Height;
00132         
00133         buf = MEM_mallocN (*size,"toavirgbbuf");
00134 
00135         rowstride = movie->header->Width*3;
00136         if (movie->header->Width%2) rowstride++;
00137 
00138         for (y=0; y < movie->header->Height; y++) {
00139                 memcpy (&buf[y*rowstride], &buffer[((movie->header->Height-1)-y)*movie->header->Width*3], movie->header->Width*3);
00140         }
00141 
00142         for (y=0; y < movie->header->Height; y++) {
00143                 for (x=0; x < movie->header->Width*3; x+=3) {
00144                         i = buf[y*rowstride+x];
00145                         buf[y*rowstride+x] = buf[y*rowstride+x+2];
00146                         buf[y*rowstride+x+2] = i;
00147                 }
00148         }
00149 
00150         MEM_freeN (buffer);
00151 
00152         return buf;
00153 }