textures, overlay images, libimago
[demo_prior] / libs / imago2 / src / byteord.c
1 /*
2 libimago - a multi-format image file input/output library.
3 Copyright (C) 2010-2019 John Tsiombikas <nuclear@member.fsf.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published
7 by the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include "byteord.h"
19
20 int16_t img_read_int16(struct img_io *io)
21 {
22         int16_t v;
23         io->read(&v, 2, io->uptr);
24         return v;
25 }
26
27 int16_t img_read_int16_inv(struct img_io *io)
28 {
29         int16_t v;
30         io->read(&v, 2, io->uptr);
31         return ((v >> 8) & 0xff) | (v << 8);
32 }
33
34 uint16_t img_read_uint16(struct img_io *io)
35 {
36         uint16_t v;
37         io->read(&v, 2, io->uptr);
38         return v;
39 }
40
41 uint16_t img_read_uint16_inv(struct img_io *io)
42 {
43         int16_t v;
44         io->read(&v, 2, io->uptr);
45         return (v >> 8) | (v << 8);
46 }
47
48
49 void img_write_int16(struct img_io *io, int16_t val)
50 {
51         io->write(&val, 2, io->uptr);
52 }
53
54 void img_write_int16_inv(struct img_io *io, int16_t val)
55 {
56         val = ((val >> 8) & 0xff) | (val << 8);
57         io->write(&val, 2, io->uptr);
58 }
59
60 void img_write_uint16(struct img_io *io, uint16_t val)
61 {
62         io->write(&val, 2, io->uptr);
63 }
64
65 void img_write_uint16_inv(struct img_io *io, uint16_t val)
66 {
67         val = (val >> 8) | (val << 8);
68         io->write(&val, 2, io->uptr);
69 }
70
71 uint32_t img_read_uint32(struct img_io *io)
72 {
73         uint32_t v;
74         io->read(&v, 4, io->uptr);
75         return v;
76 }
77
78 uint32_t img_read_uint32_inv(struct img_io *io)
79 {
80         uint32_t v;
81         io->read(&v, 4, io->uptr);
82         return (v << 24) | ((v & 0xff00) << 8) | ((v & 0xff0000) >> 8) | (v >> 24);
83 }