1// Aseprite Document Library
2// Copyright (c) 2001-2018 David Capello
3//
4// This file is released under the terms of the MIT license.
5// Read LICENSE.txt for more information.
6
7#ifdef HAVE_CONFIG_H
8#include "config.h"
9#endif
10
11#include "doc/palette_io.h"
12
13#include "base/serialization.h"
14#include "doc/palette.h"
15
16#include <iostream>
17#include <memory>
18
19namespace doc {
20
21using namespace base::serialization;
22using namespace base::serialization::little_endian;
23
24// Serialized Palette data:
25//
26// WORD Frame
27// WORD Number of colors
28// for each color ("ncolors" times)
29// DWORD _rgba color
30
31void write_palette(std::ostream& os, const Palette* palette)
32{
33 write16(os, palette->frame()); // Frame
34 write16(os, palette->size()); // Number of colors
35
36 for (int c=0; c<palette->size(); c++) {
37 uint32_t color = palette->getEntry(c);
38 write32(os, color);
39 }
40}
41
42Palette* read_palette(std::istream& is)
43{
44 frame_t frame(read16(is)); // Frame
45 int ncolors = read16(is); // Number of colors
46
47 std::unique_ptr<Palette> palette(new Palette(frame, ncolors));
48
49 for (int c=0; c<ncolors; ++c) {
50 uint32_t color = read32(is);
51 palette->setEntry(c, color);
52 }
53
54 return palette.release();
55}
56
57}
58