forked from aseprite/aseprite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpalette_io.cpp
57 lines (43 loc) · 1.22 KB
/
palette_io.cpp
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
// Aseprite Document Library
// Copyright (c) 2001-2018 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "doc/palette_io.h"
#include "base/serialization.h"
#include "doc/palette.h"
#include <iostream>
#include <memory>
namespace doc {
using namespace base::serialization;
using namespace base::serialization::little_endian;
// Serialized Palette data:
//
// WORD Frame
// WORD Number of colors
// for each color ("ncolors" times)
// DWORD _rgba color
void write_palette(std::ostream& os, const Palette* palette)
{
write16(os, palette->frame()); // Frame
write16(os, palette->size()); // Number of colors
for (int c=0; c<palette->size(); c++) {
uint32_t color = palette->getEntry(c);
write32(os, color);
}
}
Palette* read_palette(std::istream& is)
{
frame_t frame(read16(is)); // Frame
int ncolors = read16(is); // Number of colors
std::unique_ptr<Palette> palette(new Palette(frame, ncolors));
for (int c=0; c<ncolors; ++c) {
uint32_t color = read32(is);
palette->setEntry(c, color);
}
return palette.release();
}
}