comparison utils/frmconv/frm.c @ 0:4a0efb7baf70

* Datasets becomes the new trunk and retires after that :-)
author mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
date Sun, 29 Jun 2008 18:44:17 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4a0efb7baf70
1 // test
2 #ifdef _WIN32
3 #define ssize_t int
4 #include <io.h>
5 #include <sys/types.h>
6 #include <Winsock2.h>
7 #else
8 #include <netinet/in.h>
9 #include <unistd.h>
10 #endif
11
12 #include <string.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15
16 #include "frm.h"
17 #include "pal.h"
18
19 #define ntoh32(BASE, OFF) ntohl( ( ((uint32_t)(BASE[(OFF+3)])) << 24 ) | ( ((uint32_t)(BASE[(OFF) + 2])) << 16 ) | ( ((uint32_t)(BASE[(OFF) + 1])) << 8 ) | ((uint32_t)(BASE[(OFF)])))
20 #define ntoh16(BASE, OFF) ntohs( (((uint16_t)(BASE[(OFF)+1])) << 8) | ((uint16_t)(BASE[(OFF)])) )
21
22
23 void freefrmanim(frm_anim *anim) {
24 frm_frame *frp, *frp2;
25
26 if (!anim)
27 return;
28
29 if ( (frp2 = (frp = anim->frames)) ) {
30 do {
31 frp2 = frp->next_frame;
32 free(frp);
33 frp = frp2;
34 } while (frp);
35 }
36
37 free(anim);
38 }
39
40 frm_frame *fetchFRMFrame(unsigned char *buf, uint32_t buflen, uint8_t direct) {
41 int i = 0;
42 frm_frame *frame = malloc(sizeof(frm_frame));
43
44 if (!frame || buflen < 8)
45 return NULL;
46
47 frame->orient = direct;
48 frame->width = ntoh16(buf, FRMFRAME_OFFSET_WIDTH);
49 frame->height = ntoh16(buf, FRMFRAME_OFFSET_HEIGHT);
50 frame->framesize = ntoh32(buf, FRMFRAME_OFFSET_SIZE);
51
52 if (frame->framesize != frame->width * frame->height) {
53 printf("ERR: Framesize different: %d %d\n", frame->framesize, frame->width * frame->height);
54 return NULL;
55 }
56
57 if (buflen < (12 + frame->width * frame->height) ) {
58 return NULL;
59 }
60
61 frame->rowptr = malloc( sizeof(unsigned char *) * frame->height);
62 if (!frame->rowptr) {
63 printf("ERROR: no memory\n");
64 free(frame);
65 return NULL;
66 }
67
68 for(i=0; i < frame->height; ++i) {
69 frame->rowptr[i] = malloc(sizeof(unsigned char) * frame->width);
70 if (!frame->rowptr[i]) {
71 printf("ERROR: no memory\n");
72 free(frame);
73 return NULL;
74 }
75 memcpy(frame->rowptr[i], &buf[12]+i*frame->width, frame->width);
76 }
77
78 frame->next_frame = NULL;
79 return frame;
80 }
81
82
83
84 /*
85 * frm_anim *loadFRMAnim(FILE *frmfile)
86 *
87 * Paramter: frmfile is an already opened FILE handle.
88 * Return: a pointer to frm_anim structure for the frm animation; NULL in case of an error
89 *
90 * Note: 1) loadFRMAnim calls rewind and fseek on frmfile.
91 * 2) loadFRMAnim does not close frmfile
92 *
93 * Overview:
94 * loadFRMAnim tries to load a frm file from frmfile and creates a frm_anim structure that has
95 * to be freed later with freefrmanim(frmanim *). Please look at the frm_anim for an explanation of
96 * the structure.
97 *
98 */
99
100 frm_anim *loadFRMAnim(FILE *frmfile) {
101 uint32_t frmfilesize, offset=0;
102 uint8_t direct=0;
103 unsigned char *filebuf;
104 frm_anim *frmp;
105 frm_frame *frame;
106
107 // Get the length of the file frmfile:
108 rewind(frmfile);
109 fseek(frmfile, 0l, SEEK_END);
110 frmfilesize = ftell(frmfile);
111 rewind(frmfile);
112
113 if (frmfilesize < 0x3e) {
114 printf("Error: loadFRMAnim - File length to short. This cannout be a frm file.\n");
115 return NULL;
116 }
117
118 filebuf = malloc(frmfilesize);
119 if (!filebuf) {
120 printf("Error: loadFRMAnim - Couldn't allocate %d Byte memory for the file.\n", frmfilesize);
121 return NULL;
122 }
123
124 if (frmfilesize != fread(filebuf, 1, frmfilesize, frmfile)) {
125 printf("Error: loadFRMAnim - Reading the whole file failed.\n");
126 free(filebuf);
127 return NULL;
128 }
129
130 if (NULL == (frmp = malloc(sizeof(frm_anim)))) {
131 printf("Error: loadFRMAnim - Couldn't allocate %d Byte memory for the frm_anim struct.\n", sizeof(frm_anim));
132 free(filebuf);
133 return NULL;
134 }
135
136 frmp->nrofframes = 0;
137 frmp->palette = pal;
138 frmp->frames = NULL;
139 frmp->frameperorient = ntoh16(filebuf, FRM_OFFSET_FRAMESPERORIENT);
140 frmp->frameareasize = ntoh32(filebuf, FRM_OFFSET_FRAMEAREASIZE);
141
142 frame = (frmp->frames = fetchFRMFrame(&filebuf[FRM_OFFSET_FRAMEDATA], frmfilesize-FRM_OFFSET_FRAMEDATA, direct));
143
144 if (frame) {
145 offset = FRM_OFFSET_FRAMEDATA + FRMFRAME_OFFSET_DATA + frame->framesize;
146 frmp->nrofframes = 1;
147 } else {
148 printf("Warn: loadFRMAnim - Haven't found one frame.\n");
149 }
150
151 while (frame && (frame->next_frame = fetchFRMFrame(&filebuf[offset], frmfilesize-offset, direct) )) {
152 frame = frame->next_frame;
153 offset += FRMFRAME_OFFSET_DATA + frame->framesize;
154 ++frmp->nrofframes;
155
156 if (offset > filebuf[FRM_OFFSET_FIRSTFRAME_ORIENT5]) {
157 direct=5;
158 } else if (offset > filebuf[FRM_OFFSET_FIRSTFRAME_ORIENT4]) {
159 direct=4;
160 } else if (offset > filebuf[FRM_OFFSET_FIRSTFRAME_ORIENT3]) {
161 direct=3;
162 } else if (offset > filebuf[FRM_OFFSET_FIRSTFRAME_ORIENT2]) {
163 direct=2;
164 } else if (offset > filebuf[FRM_OFFSET_FIRSTFRAME_ORIENT1]) {
165 direct=1;
166 } else if (offset > filebuf[FRM_OFFSET_FIRSTFRAME_ORIENT0]) {
167 direct=0;
168 } else {
169 printf("Error: loadFRMAnim - wrong offset.\n");
170 freefrmanim(frmp);
171 free(filebuf);
172 return 0;
173 }
174 }
175 free(filebuf);
176 return frmp;
177 }