qtgui: parent the agent injector/brain viewer correctly, and fix some onSelect functi...
[openc2e.git] / fileSwapper.cpp
blobdd17da6684fbd913550c7230289f4eb55a31c75e
1 /*
2 * fileSwapper.cpp
3 * openc2e
5 * Created by Alyssa Milburn on Fri Jul 23 2004.
6 * Copyright (c) 2004 Alyssa Milburn. All rights reserved.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
20 #include "fileSwapper.h"
21 #include <fstream>
22 #include "blkImage.h"
23 #include "c16Image.h"
24 #include "openc2e.h"
26 void fileSwapper::convertsprite(s16Image &img, std::string dest) {
27 std::ofstream out(dest.c_str());
28 assert(out.is_open());
30 img.offsets = new unsigned int[img.m_numframes];
32 // okay, now we get the exciting bit, we get to write it to disk!
33 // step one: calculate offsets
34 unsigned int offset = 6 + (8 * img.m_numframes);
35 for (unsigned int i = 0; i < img.m_numframes; i++) {
36 img.offsets[i] = offset;
37 offset += img.widths[i] * img.heights[i] * 2;
40 // step two: write data
41 img.writeHeader(out);
42 for (unsigned int i = 0; i < img.m_numframes; i++) {
43 out.write((char *)img.buffers[i], img.widths[i] * img.heights[i] * 2);
46 delete[] img.offsets;
49 void fileSwapper::convertc16(std::string src, std::string dest) {
50 mmapifstream *in = new mmapifstream(src.c_str()); // auto-freed by the c16Image below
51 if (!in->is_open()) { delete in; return; }
53 // okay. read the damn file.
54 c16Image img(in, src);
56 shared_ptr<creaturesImage> imgcopy = img.mutableCopy();
57 s16Image *i = dynamic_cast<s16Image *>(imgcopy.get());
58 assert(i);
60 convertsprite(*i, dest);
63 void fileSwapper::converts16(std::string src, std::string dest) {
64 std::ifstream in(src.c_str());
65 if (!in.is_open()) return;
67 // okay. read the damn file.
68 s16Image img;
69 img.readHeader(in);
71 img.buffers = new void *[img.m_numframes];
73 for (unsigned int i = 0; i < img.m_numframes; i++) {
74 img.buffers[i] = new unsigned short[img.widths[i] * img.heights[i]];
75 in.seekg(img.offsets[i], std::ios::beg);
76 in.read((char *)img.buffers[i], (img.widths[i] * img.heights[i] * 2));
77 for (unsigned int k = 0; k < (unsigned int) (img.widths[i] * img.heights[i]); k++) {
78 ((unsigned short *)img.buffers[i])[k] = swapEndianShort(((unsigned short *)img.buffers[i])[k]);
81 delete[] img.offsets;
83 convertsprite(img, dest);
86 void fileSwapper::convertblk(std::string src, std::string dest) {
87 std::ifstream in(src.c_str());
88 if (!in.is_open()) return;
89 std::ofstream out(dest.c_str());
90 assert(out.is_open());
92 // okay. read the damn file.
93 blkImage img;
94 img.readHeader(in);
96 img.buffers = new void *[img.m_numframes];
98 for (unsigned int i = 0; i < img.m_numframes; i++) {
99 in.seekg(img.offsets[i], std::ios::beg);
100 img.buffers[i] = new uint16[128 * 128];
101 in.read((char *)img.buffers[i], 128 * 128 * 2);
102 for (unsigned int k = 0; k < 128 * 128; k++) {
103 ((unsigned short *)img.buffers[i])[k] = swapEndianShort(((unsigned short *)img.buffers[i])[k]);
107 // okay, now we get the exciting bit, we get to write it to disk!
108 // step one: calculate offsets
109 unsigned int offset = 10 + (8 * img.m_numframes);
110 for (unsigned int i = 0; i < img.m_numframes; i++) {
111 img.offsets[i] = offset;
112 offset += (128 * 128 * 2);
115 // step two: write data
116 img.writeHeader(out);
117 for (unsigned int i = 0; i < img.m_numframes; i++) {
118 out.write((char *)img.buffers[i], 128 * 128 * 2);
121 for (unsigned int i = 0; i < img.m_numframes; i++) {
122 delete[] (uint16 *)img.buffers[i];
125 /* vim: set noet: */