goat3d

annotate libs/openctm/compressRAW.c @ 22:44a20d72f3a6

merged
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 27 Sep 2013 07:14:49 +0300
parents
children
rev   line source
nuclear@14 1 //-----------------------------------------------------------------------------
nuclear@14 2 // Product: OpenCTM
nuclear@14 3 // File: compressRAW.c
nuclear@14 4 // Description: Implementation of the RAW compression method.
nuclear@14 5 //-----------------------------------------------------------------------------
nuclear@14 6 // Copyright (c) 2009-2010 Marcus Geelnard
nuclear@14 7 //
nuclear@14 8 // This software is provided 'as-is', without any express or implied
nuclear@14 9 // warranty. In no event will the authors be held liable for any damages
nuclear@14 10 // arising from the use of this software.
nuclear@14 11 //
nuclear@14 12 // Permission is granted to anyone to use this software for any purpose,
nuclear@14 13 // including commercial applications, and to alter it and redistribute it
nuclear@14 14 // freely, subject to the following restrictions:
nuclear@14 15 //
nuclear@14 16 // 1. The origin of this software must not be misrepresented; you must not
nuclear@14 17 // claim that you wrote the original software. If you use this software
nuclear@14 18 // in a product, an acknowledgment in the product documentation would be
nuclear@14 19 // appreciated but is not required.
nuclear@14 20 //
nuclear@14 21 // 2. Altered source versions must be plainly marked as such, and must not
nuclear@14 22 // be misrepresented as being the original software.
nuclear@14 23 //
nuclear@14 24 // 3. This notice may not be removed or altered from any source
nuclear@14 25 // distribution.
nuclear@14 26 //-----------------------------------------------------------------------------
nuclear@14 27
nuclear@14 28 #include "openctm.h"
nuclear@14 29 #include "internal.h"
nuclear@14 30
nuclear@14 31 #ifdef __DEBUG_
nuclear@14 32 #include <stdio.h>
nuclear@14 33 #endif
nuclear@14 34
nuclear@14 35
nuclear@14 36 //-----------------------------------------------------------------------------
nuclear@14 37 // _ctmCompressMesh_RAW() - Compress the mesh that is stored in the CTM
nuclear@14 38 // context using the RAW method, and write it the the output stream in the CTM
nuclear@14 39 // context.
nuclear@14 40 //-----------------------------------------------------------------------------
nuclear@14 41 int _ctmCompressMesh_RAW(_CTMcontext * self)
nuclear@14 42 {
nuclear@14 43 CTMuint i;
nuclear@14 44 _CTMfloatmap * map;
nuclear@14 45
nuclear@14 46 #ifdef __DEBUG_
nuclear@14 47 printf("COMPRESSION METHOD: RAW\n");
nuclear@14 48 #endif
nuclear@14 49
nuclear@14 50 // Write triangle indices
nuclear@14 51 #ifdef __DEBUG_
nuclear@14 52 printf("Inidices: %d bytes\n", (CTMuint)(self->mTriangleCount * 3 * sizeof(CTMuint)));
nuclear@14 53 #endif
nuclear@14 54 _ctmStreamWrite(self, (void *) "INDX", 4);
nuclear@14 55 for(i = 0; i < self->mTriangleCount * 3; ++ i)
nuclear@14 56 _ctmStreamWriteUINT(self, self->mIndices[i]);
nuclear@14 57
nuclear@14 58 // Write vertices
nuclear@14 59 #ifdef __DEBUG_
nuclear@14 60 printf("Vertices: %d bytes\n", (CTMuint)(self->mVertexCount * 3 * sizeof(CTMfloat)));
nuclear@14 61 #endif
nuclear@14 62 _ctmStreamWrite(self, (void *) "VERT", 4);
nuclear@14 63 for(i = 0; i < self->mVertexCount * 3; ++ i)
nuclear@14 64 _ctmStreamWriteFLOAT(self, self->mVertices[i]);
nuclear@14 65
nuclear@14 66 // Write normals
nuclear@14 67 if(self->mNormals)
nuclear@14 68 {
nuclear@14 69 #ifdef __DEBUG_
nuclear@14 70 printf("Normals: %d bytes\n", (CTMuint)(self->mVertexCount * 3 * sizeof(CTMfloat)));
nuclear@14 71 #endif
nuclear@14 72 _ctmStreamWrite(self, (void *) "NORM", 4);
nuclear@14 73 for(i = 0; i < self->mVertexCount * 3; ++ i)
nuclear@14 74 _ctmStreamWriteFLOAT(self, self->mNormals[i]);
nuclear@14 75 }
nuclear@14 76
nuclear@14 77 // Write UV maps
nuclear@14 78 map = self->mUVMaps;
nuclear@14 79 while(map)
nuclear@14 80 {
nuclear@14 81 #ifdef __DEBUG_
nuclear@14 82 printf("UV coordinates (%s): %d bytes\n", map->mName ? map->mName : "no name", (CTMuint)(self->mVertexCount * 2 * sizeof(CTMfloat)));
nuclear@14 83 #endif
nuclear@14 84 _ctmStreamWrite(self, (void *) "TEXC", 4);
nuclear@14 85 _ctmStreamWriteSTRING(self, map->mName);
nuclear@14 86 _ctmStreamWriteSTRING(self, map->mFileName);
nuclear@14 87 for(i = 0; i < self->mVertexCount * 2; ++ i)
nuclear@14 88 _ctmStreamWriteFLOAT(self, map->mValues[i]);
nuclear@14 89 map = map->mNext;
nuclear@14 90 }
nuclear@14 91
nuclear@14 92 // Write attribute maps
nuclear@14 93 map = self->mAttribMaps;
nuclear@14 94 while(map)
nuclear@14 95 {
nuclear@14 96 #ifdef __DEBUG_
nuclear@14 97 printf("Vertex attributes (%s): %d bytes\n", map->mName ? map->mName : "no name", (CTMuint)(self->mVertexCount * 4 * sizeof(CTMfloat)));
nuclear@14 98 #endif
nuclear@14 99 _ctmStreamWrite(self, (void *) "ATTR", 4);
nuclear@14 100 _ctmStreamWriteSTRING(self, map->mName);
nuclear@14 101 for(i = 0; i < self->mVertexCount * 4; ++ i)
nuclear@14 102 _ctmStreamWriteFLOAT(self, map->mValues[i]);
nuclear@14 103 map = map->mNext;
nuclear@14 104 }
nuclear@14 105
nuclear@14 106 return 1;
nuclear@14 107 }
nuclear@14 108
nuclear@14 109 //-----------------------------------------------------------------------------
nuclear@14 110 // _ctmUncompressMesh_RAW() - Uncmpress the mesh from the input stream in the
nuclear@14 111 // CTM context using the RAW method, and store the resulting mesh in the CTM
nuclear@14 112 // context.
nuclear@14 113 //-----------------------------------------------------------------------------
nuclear@14 114 int _ctmUncompressMesh_RAW(_CTMcontext * self)
nuclear@14 115 {
nuclear@14 116 CTMuint i;
nuclear@14 117 _CTMfloatmap * map;
nuclear@14 118
nuclear@14 119 // Read triangle indices
nuclear@14 120 if(_ctmStreamReadUINT(self) != FOURCC("INDX"))
nuclear@14 121 {
nuclear@14 122 self->mError = CTM_BAD_FORMAT;
nuclear@14 123 return 0;
nuclear@14 124 }
nuclear@14 125 for(i = 0; i < self->mTriangleCount * 3; ++ i)
nuclear@14 126 self->mIndices[i] = _ctmStreamReadUINT(self);
nuclear@14 127
nuclear@14 128 // Read vertices
nuclear@14 129 if(_ctmStreamReadUINT(self) != FOURCC("VERT"))
nuclear@14 130 {
nuclear@14 131 self->mError = CTM_BAD_FORMAT;
nuclear@14 132 return 0;
nuclear@14 133 }
nuclear@14 134 for(i = 0; i < self->mVertexCount * 3; ++ i)
nuclear@14 135 self->mVertices[i] = _ctmStreamReadFLOAT(self);
nuclear@14 136
nuclear@14 137 // Read normals
nuclear@14 138 if(self->mNormals)
nuclear@14 139 {
nuclear@14 140 if(_ctmStreamReadUINT(self) != FOURCC("NORM"))
nuclear@14 141 {
nuclear@14 142 self->mError = CTM_BAD_FORMAT;
nuclear@14 143 return 0;
nuclear@14 144 }
nuclear@14 145 for(i = 0; i < self->mVertexCount * 3; ++ i)
nuclear@14 146 self->mNormals[i] = _ctmStreamReadFLOAT(self);
nuclear@14 147 }
nuclear@14 148
nuclear@14 149 // Read UV maps
nuclear@14 150 map = self->mUVMaps;
nuclear@14 151 while(map)
nuclear@14 152 {
nuclear@14 153 if(_ctmStreamReadUINT(self) != FOURCC("TEXC"))
nuclear@14 154 {
nuclear@14 155 self->mError = CTM_BAD_FORMAT;
nuclear@14 156 return 0;
nuclear@14 157 }
nuclear@14 158 _ctmStreamReadSTRING(self, &map->mName);
nuclear@14 159 _ctmStreamReadSTRING(self, &map->mFileName);
nuclear@14 160 for(i = 0; i < self->mVertexCount * 2; ++ i)
nuclear@14 161 map->mValues[i] = _ctmStreamReadFLOAT(self);
nuclear@14 162 map = map->mNext;
nuclear@14 163 }
nuclear@14 164
nuclear@14 165 // Read attribute maps
nuclear@14 166 map = self->mAttribMaps;
nuclear@14 167 while(map)
nuclear@14 168 {
nuclear@14 169 if(_ctmStreamReadUINT(self) != FOURCC("ATTR"))
nuclear@14 170 {
nuclear@14 171 self->mError = CTM_BAD_FORMAT;
nuclear@14 172 return 0;
nuclear@14 173 }
nuclear@14 174 _ctmStreamReadSTRING(self, &map->mName);
nuclear@14 175 for(i = 0; i < self->mVertexCount * 4; ++ i)
nuclear@14 176 map->mValues[i] = _ctmStreamReadFLOAT(self);
nuclear@14 177 map = map->mNext;
nuclear@14 178 }
nuclear@14 179
nuclear@14 180 return 1;
nuclear@14 181 }