ccdevnet / openc2e

openc2e

openc2e / blkImage.cpp
100644 84 lines (70 sloc) 2.83 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
* blkImage.cpp
* openc2e
*
* Created by Alyssa Milburn on Tue May 25 2004.
* Copyright (c) 2004 Alyssa Milburn. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
*/
 
#include "openc2e.h"
#include "blkImage.h"
#include <iostream>
 
void blkImage::readHeader(std::istream &in) {
uint32 flags; uint16 width, height, spritecount;
in.read((char *)&flags, 4); flags = swapEndianLong(flags);
is_565 = (flags & 0x01);
in.read((char *)&width, 2); width = swapEndianShort(width);
in.read((char *)&height, 2); height = swapEndianShort(height);
totalwidth = width * 128; totalheight = height * 128;
in.read((char *)&spritecount, 2); m_numframes = swapEndianShort(spritecount);
 
assert(m_numframes == (unsigned int) (width * height));
 
widths = new uint16[m_numframes];
heights = new uint16[m_numframes];
offsets = new uint32[m_numframes];
 
for (unsigned int i = 0; i < m_numframes; i++) {
in.read((char *)&offsets[i], 4); offsets[i] = swapEndianLong(offsets[i]) + 4;
in.read((char *)&widths[i], 2); widths[i] = swapEndianShort(widths[i]); assert(widths[i] == 128);
in.read((char *)&heights[i], 2); heights[i] = swapEndianShort(heights[i]); assert(heights[i] == 128);
}
}
 
void blkImage::writeHeader(std::ostream &s) {
unsigned int dw; unsigned short w;
 
dw = (is_565 ? 1 : 0);
dw = swapEndianLong(dw); s.write((char *)&dw, 4);
w = totalwidth / 128; assert((unsigned int)(w * 128) == totalwidth);
w = swapEndianShort(w); s.write((char *)&w, 2);
w = totalheight / 128; assert((unsigned int)(w * 128) == totalheight);
w = swapEndianShort(w); s.write((char *)&w, 2);
w = m_numframes; assert(m_numframes == (unsigned int) ((totalwidth / 128) * (totalheight / 128)));
w = swapEndianShort(w); s.write((char *)&w, 2);
 
for (unsigned int i = 0; i < m_numframes; i++) {
dw = offsets[i] - 4;
dw = swapEndianLong(dw); s.write((char *)&dw, 4);
w = widths[i]; assert(w == 128);
w = swapEndianShort(w); s.write((char *)&w, 2);
w = heights[i]; assert(w == 128);
w = swapEndianShort(w); s.write((char *)&w, 2);
}
}
 
blkImage::blkImage(mmapifstream *in) {
stream = in;
 
  readHeader(*in);
 
buffers = new void *[m_numframes];
 
for (unsigned int i = 0; i < m_numframes; i++)
buffers[i] = in->map + offsets[i];
}
 
blkImage::~blkImage() {
delete[] buffers;
delete[] offsets;
}
/* vim: set noet: */