forked from aseprite/aseprite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpixel_io.h
150 lines (138 loc) · 3.91 KB
/
pixel_io.h
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Aseprite Document IO Library
// Copyright (c) 2019 Igara Studio S.A.
// Copyright (c) 2017-2018 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#ifndef DIO_PIXEL_IO_H_INCLUDED
#define DIO_PIXEL_IO_H_INCLUDED
#pragma once
#include "dio/file_interface.h"
#include "doc/image_traits.h"
#include <cstring>
namespace dio {
template<typename ImageTraits>
class PixelIO {
public:
typename ImageTraits::pixel_t read_pixel(FileInterface* fi);
void write_pixel(FileInterface* fi, typename ImageTraits::pixel_t c);
void read_scanline(typename ImageTraits::address_t address,
int w, uint8_t* buffer);
void write_scanline(typename ImageTraits::address_t address,
int w, uint8_t* buffer);
};
template<>
class PixelIO<doc::RgbTraits> {
int r, g, b, a;
public:
doc::RgbTraits::pixel_t read_pixel(FileInterface* f) {
r = f->read8();
g = f->read8();
b = f->read8();
a = f->read8();
return doc::rgba(r, g, b, a);
}
void write_pixel(FileInterface* f, doc::RgbTraits::pixel_t c) {
f->write8(doc::rgba_getr(c));
f->write8(doc::rgba_getg(c));
f->write8(doc::rgba_getb(c));
f->write8(doc::rgba_geta(c));
}
void read_scanline(doc::RgbTraits::address_t address,
int w, uint8_t* buffer) {
for (int x=0; x<w; ++x, ++address) {
r = *(buffer++);
g = *(buffer++);
b = *(buffer++);
a = *(buffer++);
*address = doc::rgba(r, g, b, a);
}
}
void write_scanline(doc::RgbTraits::address_t address,
int w, uint8_t* buffer) {
for (int x=0; x<w; ++x, ++address) {
*(buffer++) = doc::rgba_getr(*address);
*(buffer++) = doc::rgba_getg(*address);
*(buffer++) = doc::rgba_getb(*address);
*(buffer++) = doc::rgba_geta(*address);
}
}
};
template<>
class PixelIO<doc::GrayscaleTraits> {
int k, a;
public:
doc::GrayscaleTraits::pixel_t read_pixel(FileInterface* f) {
k = f->read8();
a = f->read8();
return doc::graya(k, a);
}
void write_pixel(FileInterface* f, doc::GrayscaleTraits::pixel_t c) {
f->write8(doc::graya_getv(c));
f->write8(doc::graya_geta(c));
}
void read_scanline(doc::GrayscaleTraits::address_t address,
int w, uint8_t* buffer)
{
for (int x=0; x<w; ++x, ++address) {
k = *(buffer++);
a = *(buffer++);
*address = doc::graya(k, a);
}
}
void write_scanline(doc::GrayscaleTraits::address_t address,
int w, uint8_t* buffer)
{
for (int x=0; x<w; ++x, ++address) {
*(buffer++) = doc::graya_getv(*address);
*(buffer++) = doc::graya_geta(*address);
}
}
};
template<>
class PixelIO<doc::IndexedTraits> {
public:
doc::IndexedTraits::pixel_t read_pixel(FileInterface* f) {
return f->read8();
}
void write_pixel(FileInterface* f, doc::IndexedTraits::pixel_t c) {
f->write8(c);
}
void read_scanline(doc::IndexedTraits::address_t address,
int w, uint8_t* buffer) {
std::memcpy(address, buffer, w);
}
void write_scanline(doc::IndexedTraits::address_t address,
int w, uint8_t* buffer) {
std::memcpy(buffer, address, w);
}
};
template<>
class PixelIO<doc::TilemapTraits> {
int b1, b2, b3, b4;
public:
doc::TilemapTraits::pixel_t read_pixel(FileInterface* f) {
int b1 = f->read8();
int b2 = f->read8();
int b3 = f->read8();
int b4 = f->read8();
if (f->ok()) {
// Little endian
return ((b4 << 24) | (b3 << 16) | (b2 << 8) | b1);
}
else
return 0;
}
void read_scanline(doc::TilemapTraits::address_t address,
int w, uint8_t* buffer) {
for (int x=0; x<w; ++x, ++address) {
b1 = *(buffer++);
b2 = *(buffer++);
b3 = *(buffer++);
b4 = *(buffer++);
*address = ((b4 << 24) | (b3 << 16) | (b2 << 8) | b1);
}
}
};
} // namespace dio
#endif