-
Notifications
You must be signed in to change notification settings - Fork 0
/
bmp-read-loadindexed.c
122 lines (98 loc) · 2.92 KB
/
bmp-read-loadindexed.c
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
/* bmplib - bmp-read-loadindexed.c
*
* Copyright (c) 2024, Rupert Weber.
*
* This file is part of bmplib.
* bmplib is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library.
* If not, see <https://www.gnu.org/licenses/>
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#define BMPLIB_LIB
#include "config.h"
#include "bmplib.h"
#include "logging.h"
#include "bmp-common.h"
#include "bmp-read.h"
/* Functions in this file are for retreiving an indexed BMP as
* index + palette. (Unlike the bmplib default which is to
* return 24bit RGB data.)
*/
/********************************************************
* bmpread_num_palette_colors
*
* will return the number of palette-color. For non-
* indexed BMPs, 0 is returned.
*******************************************************/
API int bmpread_num_palette_colors(BMPHANDLE h)
{
BMPREAD rp;
if (!(h && cm_check_is_read_handle(h)))
return 0;
rp = (BMPREAD)(void*)h;
if (rp->palette)
return rp->palette->numcolors;
return 0;
}
/********************************************************
* bmpread_load_palette
* palette entries are always passed on as a
* character array, 4 bytes per color (R-G-B-0)
*******************************************************/
API BMPRESULT bmpread_load_palette(BMPHANDLE h, unsigned char **palette)
{
BMPREAD rp;
int i,c;
size_t memsize;
if (!(h && cm_check_is_read_handle(h)))
return 0;
rp = (BMPREAD)(void*)h;
if (!rp->getinfo_called) {
logerr(rp->log, "Must call bmpread_load_info() before loading palette");
return BMP_RESULT_ERROR;
}
if (!rp->palette) {
logerr(rp->log, "Image has no palette");
return BMP_RESULT_ERROR;
}
if (!palette) {
logerr(rp->log, "palette is NULL");
return BMP_RESULT_ERROR;
}
memsize = rp->palette->numcolors * 4;
if (!*palette) {
if (!(*palette = malloc(memsize))) {
logsyserr(rp->log, "allocating palette");
return BMP_RESULT_ERROR;
}
}
memset(*palette, 0, memsize);
/* irreversible. image will be returned as indexed pixels */
if (!rp->result_indexed) {
rp->result_indexed = TRUE;
rp->dimensions_queried = FALSE;
rp->dim_queried_channels = FALSE;
rp->result_channels = 1;
if (!br_set_resultbits(rp))
return BMP_RESULT_ERROR;
}
for (i = 0; i < rp->palette->numcolors; i++) {
for (c = 0; c < 3; c++) {
(*palette)[4*i + c] = rp->palette->color[i].value[c];
}
}
return BMP_RESULT_OK;
}