2 Copyright (C) 2005 John Tsiombikas <nuclear@siggraph.org>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
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.
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
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include "byteorder.h"
22 int8_t read_int8(FILE *fp) {
28 int16_t read_int16(FILE *fp) {
34 int16_t read_int16_inv(FILE *fp) {
37 return v >> 8 | v << 8;
40 int32_t read_int32(FILE *fp) {
46 int32_t read_int32_inv(FILE *fp) {
49 return v >> 24 | (v & 0x00ff0000) >> 8 | (v & 0x0000ff00) << 8 | v << 24;
52 float read_float(FILE *fp) {
53 int32_t tmp = read_int32(fp);
54 return *((float*)&tmp);
57 float read_float_inv(FILE *fp) {
58 int32_t tmp = read_int32_inv(fp);
59 return *((float*)&tmp);
62 void write_int8(FILE *fp, int8_t v) {
66 void write_int16(FILE *fp, int16_t v) {
70 void write_int16_inv(FILE *fp, int16_t v) {
71 int16_t tmp = v >> 8 | v << 8;
72 fwrite(&tmp, 2, 1, fp);
75 void write_int32(FILE *fp, int32_t v) {
79 void write_int32_inv(FILE *fp, int32_t v) {
80 int32_t tmp = v >> 24 | (v & 0x00ff0000) >> 8 | (v & 0x0000ff00) << 8 | v << 24;
81 fwrite(&tmp, 4, 1, fp);
84 void write_float(FILE *fp, float v) {
85 write_int32(fp, *((int32_t*)&v));
87 void write_float_inv(FILE *fp, float v) {
88 write_int32_inv(fp, *((int32_t*)&v));