Allow specifying ROM type in file load dialog
[lsnes.git] / src / emulation / sky / image.cpp
blobf0a2e90374abab24ed90d3c812e5fc34c28c9957
1 #include "image.hpp"
2 #include "util.hpp"
4 namespace sky
6 struct vector_output_stream : public output_stream
8 vector_output_stream(std::vector<uint8_t>& _data);
9 ~vector_output_stream();
10 void put(uint8_t byte);
11 private:
12 std::vector<uint8_t>& data;
15 vector_output_stream::vector_output_stream(std::vector<uint8_t>& _data)
16 : data(_data)
20 vector_output_stream::~vector_output_stream()
24 void vector_output_stream::put(uint8_t byte)
26 data.push_back(byte);
29 image::image()
31 memset(palette, 0, 256 * sizeof(uint32_t));
32 memset(unknown2, 0, 512);
33 unknown1 = 0;
34 width = 0;
35 height = 0;
38 image::image(const std::vector<char>& data)
40 memset(palette, 0, 256 * sizeof(uint32_t));
41 memset(unknown2, 0, 512);
42 if(data.size() < 5)
43 throw std::runtime_error("Expected CMAP magic, got EOF");
44 if(data[0] != 'C' || data[1] != 'M' || data[2] != 'A' || data[3] != 'P')
45 throw std::runtime_error("Bad CMAP magic");
46 colors = data[4];
47 if(data.size() < 5 * colors + 5)
48 throw std::runtime_error("Expected CMAP, got EOF");
49 for(unsigned i = 0; i < colors; i++) {
50 palette[i] = ((uint32_t)expand_color(data[3 * i + 5]) << 16) |
51 ((uint32_t)expand_color(data[3 * i + 6]) << 8) |
52 ((uint32_t)expand_color(data[3 * i + 7]));
53 unknown2[2 * i + 0] = data[3 * colors + 2 * i + 5];
54 unknown2[2 * i + 1] = data[3 * colors + 2 * i + 6];
56 if(data.size() < 5 * colors + 9)
57 throw std::runtime_error("Bad PICT magic");
58 size_t x = 5 * colors + 5;
59 if(data[x + 0] != 'P' || data[x + 1] != 'I' || data[x + 2] != 'C' || data[x + 3] != 'T')
60 throw std::runtime_error("Bad PICT magic");
61 if(data.size() < 5 * colors + 15)
62 throw std::runtime_error("Expected PICT header, got EOF");
63 unknown1 = combine(data[x + 4], data[x + 5]);
64 height = combine(data[x + 6], data[x + 7]);
65 width = combine(data[x + 8], data[x + 9]);
66 vector_input_stream in(data, x + 10);
67 vector_output_stream out(decode);
68 lzs_decompress(in, out, (uint32_t)width * height);