Camera tracking: libmv distortion API now also uses camera intrinsics
[blender.git] / source / blender / avi / intern / avi_endian.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  *
27  */
28
29 /** \file blender/avi/intern/avi_endian.c
30  *  \ingroup avi
31  *
32  * This is external code. Streams bytes to output depending on the
33  * endianness of the system.
34  */
35
36
37 #include <stdlib.h>
38 #include <string.h>
39 #include <stdio.h>
40
41 #include "AVI_avi.h"
42 #include "avi_endian.h"
43 #include "avi_intern.h"
44
45 #ifdef __BIG_ENDIAN__
46 #include "MEM_guardedalloc.h"
47 #endif
48
49 #ifdef __BIG_ENDIAN__
50
51 /* copied from BLI_endian_switch_inline.h */
52 static void invert(int *val)
53 {
54         int tval = *val;
55         *val = ((tval >> 24))             |
56                ((tval << 8) & 0x00ff0000) |
57                ((tval >> 8) & 0x0000ff00) |
58                ((tval << 24));
59 }
60
61 static void sinvert(short int *val)
62 {
63         short tval = *val;
64         *val = (tval >> 8) |
65                (tval << 8);
66 }
67
68 static void Ichunk(AviChunk *chunk)
69 {
70         invert(&chunk->fcc);
71         invert(&chunk->size);
72 }
73 #endif
74
75 #ifdef __BIG_ENDIAN__
76 static void Ilist(AviList *list)
77 {
78         invert(&list->fcc);
79         invert(&list->size);
80         invert(&list->ids);
81 }
82
83 static void Imainh(AviMainHeader *mainh)
84 {
85         invert(&mainh->fcc);
86         invert(&mainh->size);
87         invert(&mainh->MicroSecPerFrame);
88         invert(&mainh->MaxBytesPerSec);
89         invert(&mainh->PaddingGranularity);
90         invert(&mainh->Flags);
91         invert(&mainh->TotalFrames);
92         invert(&mainh->InitialFrames);
93         invert(&mainh->Streams);
94         invert(&mainh->SuggestedBufferSize);
95         invert(&mainh->Width);
96         invert(&mainh->Height);
97         invert(&mainh->Reserved[0]);
98         invert(&mainh->Reserved[1]);
99         invert(&mainh->Reserved[2]);
100         invert(&mainh->Reserved[3]);
101 }
102
103 static void Istreamh(AviStreamHeader *streamh)
104 {
105         invert(&streamh->fcc);
106         invert(&streamh->size);
107         invert(&streamh->Type);
108         invert(&streamh->Handler);
109         invert(&streamh->Flags);
110         sinvert(&streamh->Priority);
111         sinvert(&streamh->Language);
112         invert(&streamh->InitialFrames);
113         invert(&streamh->Scale);
114         invert(&streamh->Rate);
115         invert(&streamh->Start);
116         invert(&streamh->Length);
117         invert(&streamh->SuggestedBufferSize);
118         invert(&streamh->Quality);
119         invert(&streamh->SampleSize);
120         sinvert(&streamh->left);
121         sinvert(&streamh->right);
122         sinvert(&streamh->top);
123         sinvert(&streamh->bottom);
124 }
125
126 static void Ibitmaph(AviBitmapInfoHeader *bitmaph)
127 {
128         invert(&bitmaph->fcc);
129         invert(&bitmaph->size);
130         invert(&bitmaph->Size);
131         invert(&bitmaph->Width);
132         invert(&bitmaph->Height);
133         sinvert(&bitmaph->Planes);
134         sinvert(&bitmaph->BitCount);
135         invert(&bitmaph->Compression);
136         invert(&bitmaph->SizeImage);
137         invert(&bitmaph->XPelsPerMeter);
138         invert(&bitmaph->YPelsPerMeter);
139         invert(&bitmaph->ClrUsed);
140         invert(&bitmaph->ClrImportant);
141 }
142
143 static void Imjpegu(AviMJPEGUnknown *mjpgu)
144 {
145         invert(&mjpgu->a);
146         invert(&mjpgu->b);
147         invert(&mjpgu->c);
148         invert(&mjpgu->d);
149         invert(&mjpgu->e);
150         invert(&mjpgu->f);
151         invert(&mjpgu->g);
152 }
153
154 static void Iindexe(AviIndexEntry *indexe)
155 {
156         invert(&indexe->ChunkId);
157         invert(&indexe->Flags);
158         invert(&indexe->Offset);
159         invert(&indexe->Size);
160 }
161 #endif /* __BIG_ENDIAN__ */
162
163 void awrite(AviMovie *movie, void *datain, int block, int size, FILE *fp, int type)
164 {
165 #ifdef __BIG_ENDIAN__
166         void *data;
167
168         data = MEM_mallocN(size, "avi endian");
169
170         memcpy(data, datain, size);
171
172         switch (type) {
173                 case AVI_RAW:
174                         fwrite(data, block, size, fp);
175                         break;
176                 case AVI_CHUNK:
177                         Ichunk((AviChunk *) data);
178                         fwrite(data, block, size, fp);
179                         break;
180                 case AVI_LIST:
181                         Ilist((AviList *) data);
182                         fwrite(data, block, size, fp);
183                         break;
184                 case AVI_MAINH:
185                         Imainh((AviMainHeader *) data);
186                         fwrite(data, block, size, fp);
187                         break;
188                 case AVI_STREAMH:
189                         Istreamh((AviStreamHeader *) data);
190                         fwrite(data, block, size, fp);
191                         break;
192                 case AVI_BITMAPH:
193                         Ibitmaph((AviBitmapInfoHeader *) data);
194                         if (size == sizeof(AviBitmapInfoHeader) + sizeof(AviMJPEGUnknown)) {
195                                 Imjpegu((AviMJPEGUnknown *)((char *)data + sizeof(AviBitmapInfoHeader)));
196                         }
197                         fwrite(data, block, size, fp);
198                         break;
199                 case AVI_MJPEGU:
200                         Imjpegu((AviMJPEGUnknown *) data);
201                         fwrite(data, block, size, fp);
202                         break;
203                 case AVI_INDEXE:
204                         Iindexe((AviIndexEntry *) data);
205                         fwrite(data, block, size, fp);
206                         break;
207                 default:
208                         break;
209         }
210
211         MEM_freeN(data);
212 #else /* __BIG_ENDIAN__ */
213         (void)movie; /* unused */
214         (void)type; /* unused */
215         fwrite(datain, block, size, fp);
216 #endif /* __BIG_ENDIAN__ */
217 }