// spr format analysis demo
// 2008-2-14 by b
#include <stdio.h>
#include <windows.h>
#define SIMPLE
typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned int u32;
typedef unsigned __int64 u64;
#define SprHeaderSize 8
typedef struct _SprHeader {
u32 type; // 0x02015053
u32 numFrame;
} SprHeader;
#define SprFrameSize 6
typedef struct _SprFrame {
u16 frameX;
u16 frameY;
u16 length;
} SprFrame;
void printSprHeader (SprHeader* header) {
printf("SprHeader-----------------------\n");
printf("\ttype: 0x%08x \n", header->type);
printf("\tframe number: %d \n", header->numFrame);
printf("--------------------------------\n");
}
void printSprFrame (SprFrame* frame) {
printf("SprFrame------------------------\n");
printf("\tframe x: %d \n", frame->frameX);
printf("\tframe y: %d \n", frame->frameY);
printf("\tframe length: %d \n", frame->length);
printf("--------------------------------\n");
}
#define LINE_LENGTH 20
void printPixelData (FILE* fp, int length) {
int n = 0;
printf("<<< pixel data <<<<<<<<<<<<<<<<<<<<<<<<<<\n");
for (int i = 0; i < length; i++) {
u8 a = 0;
fread(&a, sizeof(a), 1, fp);
printf("%02x ", a);
n++;
if (n >= LINE_LENGTH) {
printf("\n");
n = 0;
}
}
printf("\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
}
void printSprFrameLoop (FILE* fp, int numLoop) {
int i = 0;
do {
printf("frame %d:::::::::::::::::::::::::::\n", i+1);
// print a frame
SprFrame frame;
memset(&frame, 0, sizeof(frame));
fread(&frame, SprFrameSize, 1, fp);
printSprFrame(&frame);
#ifdef SIMPLE
// seek file
fseek(fp, frame.length, SEEK_CUR);
if (feof(fp)) {
printf("file length error!");
break; // if end of file exit loop
}
#else
// print pixel data
printPixelData(fp, frame.length);
#endif
i++;
} while (i < numLoop);
printf("::::::::::::::::::::::::::::::::::\n");
}
void printSprPal (FILE* fp) {
printf("=== spr pal ========================\n");
int length = 0;
// print 32bit-RGB color in pal
while (!feof(fp)) {
// print 4byte
int tmp = 0xFFFFFFFF;
int n = fread(&tmp, 4, 1, fp);
if (n == 0) {
break;
} else if (n != 1) {
printf("number of read file is: %d\n", n);
break;
}
#ifdef SIMPLE
printf("%08x\t", tmp);
if ((length + 1) % 4 == 0) printf("\n");
#else
// red
int r = tmp & 255;
printf("R: %8d \t", r);
// green
int g = tmp & 65280;
g = g >> 8;
printf("G: %8d \t", g);
// blue
int b = tmp & 16711680;
b = b >> 16;
#ifndef _DEBUG
printf("B: %8d \n", g);
#else
printf("B: %8d \t", g);
// alpha
int a = tmp & 4278190080;
a = a >> 24;
printf("A: %8d \n", a);
#endif
#endif
length++;
}
if (length != 256) {
printf("pal length error! length is: %d\n", length);
} else {
printf("======================================= finish\n");
}
}
int main (int argc, char* argv[]) {
// open file
FILE* fp;
fp = fopen("spr\\VPHelm20051002.spr", "rb");
if (fp == NULL) {
printf("can not open file");
return 0;
}
// read spr header
SprHeader header;
memset(&header, 0, sizeof(header));
fread(&header, SprHeaderSize, 1, fp);
printSprHeader(&header);
/**//* // read spr frame
SprFrame frame;
memset(&frame, 0, sizeof(frame));
//#ifdef _DEBUG
// printf("sizeof(frame) is: %d\n", sizeof(frame));
//#endif
fread(&frame, SprFrameSize, 1, fp);
printSprFrame(&frame);*/
// read spr frame
printSprFrameLoop(fp, header.numFrame);
// read spr pal
printSprPal(fp);
// exit
//l_exit:
fclose(fp);
system("pause");
return 0;
}
参考文章 http://mist.in/gratia/ro/spr/SprFileFormat.html 写的