Blender V2.61 - r43446

endian.c

Go to the documentation of this file.
00001 /*
00002  *
00003  * This is external code. Streams bytes to output depending on the
00004  * endianness of the system.
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 <stdlib.h>
00039 #include <string.h>
00040 #include <stdio.h> 
00041 #include "AVI_avi.h"
00042 #include "endian.h"
00043 #include "avi_intern.h"
00044 
00045 #ifdef __BIG_ENDIAN__
00046 static void invert (int *num)
00047 {
00048     int new=0,i,j;
00049 
00050     for (j=0; j < 4; j++) {
00051         for (i=0; i<8; i++) {
00052             new |= ((*num>>(j*8+i))&1)<<((3-j)*8+i);
00053         }
00054     }
00055     
00056     *num = new;
00057 }
00058 
00059 static void sinvert (short int *num)
00060 {
00061     short int new=0;
00062     int i,j;
00063 
00064     for (j=0; j < 2; j++) {
00065         for (i=0; i<8; i++) {
00066             new |= ((*num>>(j*8+i))&1)<<((1-j)*8+i);
00067         }
00068     }
00069 
00070     *num = new;
00071 }
00072 
00073 static void Ichunk (AviChunk *chunk)
00074 {
00075     invert (&chunk->fcc);
00076     invert (&chunk->size);
00077 }
00078 #endif
00079 
00080 #ifdef __BIG_ENDIAN__
00081 static void Ilist (AviList *list)
00082 {
00083     invert (&list->fcc);
00084     invert (&list->size);
00085     invert (&list->ids);
00086 }
00087 
00088 static void Imainh (AviMainHeader *mainh)
00089 {
00090     invert (&mainh->fcc);
00091     invert (&mainh->size);
00092     invert (&mainh->MicroSecPerFrame);
00093     invert (&mainh->MaxBytesPerSec);
00094     invert (&mainh->PaddingGranularity);
00095     invert (&mainh->Flags);
00096     invert (&mainh->TotalFrames);
00097     invert (&mainh->InitialFrames);
00098     invert (&mainh->Streams);
00099     invert (&mainh->SuggestedBufferSize);
00100     invert (&mainh->Width);
00101     invert (&mainh->Height);
00102     invert (&mainh->Reserved[0]);
00103     invert (&mainh->Reserved[1]);
00104     invert (&mainh->Reserved[2]);
00105     invert (&mainh->Reserved[3]);
00106 }
00107 
00108 static void Istreamh (AviStreamHeader *streamh)
00109 {
00110     invert (&streamh->fcc);
00111     invert (&streamh->size);
00112     invert (&streamh->Type);
00113     invert (&streamh->Handler);
00114     invert (&streamh->Flags);
00115     sinvert (&streamh->Priority);
00116     sinvert (&streamh->Language);
00117     invert (&streamh->InitialFrames);
00118     invert (&streamh->Scale);
00119     invert (&streamh->Rate);
00120     invert (&streamh->Start);
00121     invert (&streamh->Length);
00122     invert (&streamh->SuggestedBufferSize);
00123     invert (&streamh->Quality);
00124     invert (&streamh->SampleSize);
00125     sinvert (&streamh->left);
00126     sinvert (&streamh->right);
00127     sinvert (&streamh->top);
00128     sinvert (&streamh->bottom);
00129 }
00130 
00131 static void Ibitmaph (AviBitmapInfoHeader *bitmaph)
00132 {
00133     invert (&bitmaph->fcc);
00134     invert (&bitmaph->size);
00135     invert (&bitmaph->Size);
00136     invert (&bitmaph->Width);
00137     invert (&bitmaph->Height);
00138     sinvert (&bitmaph->Planes);
00139     sinvert (&bitmaph->BitCount);
00140     invert (&bitmaph->Compression);
00141     invert (&bitmaph->SizeImage);
00142     invert (&bitmaph->XPelsPerMeter);
00143     invert (&bitmaph->YPelsPerMeter);
00144     invert (&bitmaph->ClrUsed);
00145     invert (&bitmaph->ClrImportant);
00146 }
00147 
00148 static void Imjpegu (AviMJPEGUnknown *mjpgu)
00149 {
00150     invert (&mjpgu->a);
00151     invert (&mjpgu->b);
00152     invert (&mjpgu->c);
00153     invert (&mjpgu->d);
00154     invert (&mjpgu->e);
00155     invert (&mjpgu->f);
00156     invert (&mjpgu->g);
00157 }
00158 
00159 static void Iindexe (AviIndexEntry *indexe)
00160 {
00161     invert (&indexe->ChunkId);
00162     invert (&indexe->Flags);
00163     invert (&indexe->Offset);
00164     invert (&indexe->Size);
00165 }
00166 #endif /* __BIG_ENDIAN__ */
00167 
00168 void awrite (AviMovie *movie, void *datain, int block, int size, FILE *fp, int type)
00169 {
00170 #ifdef __BIG_ENDIAN__
00171     void *data;
00172 
00173     data = MEM_mallocN (size, "avi endian");
00174 
00175     memcpy (data, datain, size);
00176 
00177     switch (type) {
00178     case AVI_RAW:
00179         fwrite (data, block, size, fp);
00180         break;
00181     case AVI_CHUNK:
00182         Ichunk ((AviChunk *) data);
00183         fwrite (data, block, size, fp);
00184         break;
00185     case AVI_LIST:
00186         Ilist ((AviList *) data);
00187         fwrite (data, block, size, fp);
00188         break;
00189     case AVI_MAINH:
00190         Imainh ((AviMainHeader *) data);
00191         fwrite (data, block, size, fp);
00192         break;
00193     case AVI_STREAMH:
00194         Istreamh ((AviStreamHeader *) data);
00195         fwrite (data, block, size, fp);
00196         break;
00197     case AVI_BITMAPH:
00198         Ibitmaph ((AviBitmapInfoHeader *) data);
00199         if (size==sizeof(AviBitmapInfoHeader) + sizeof(AviMJPEGUnknown)) {
00200             Imjpegu((AviMJPEGUnknown*)((char*)data+sizeof(AviBitmapInfoHeader)));
00201         }
00202         fwrite (data, block, size, fp);
00203         break;
00204     case AVI_MJPEGU:
00205         Imjpegu ((AviMJPEGUnknown *) data);
00206         fwrite (data, block, size, fp);
00207         break;
00208     case AVI_INDEXE:
00209         Iindexe ((AviIndexEntry *) data);
00210         fwrite (data, block, size, fp);
00211         break;
00212     default:
00213         break;
00214     }
00215 
00216     MEM_freeN (data);
00217 #else /* __BIG_ENDIAN__ */
00218     (void)movie; /* unused */
00219     (void)type; /* unused */
00220     fwrite (datain, block, size, fp);
00221 #endif /* __BIG_ENDIAN__ */
00222 }