-
Notifications
You must be signed in to change notification settings - Fork 16
/
usedidx.c
97 lines (88 loc) · 2.71 KB
/
usedidx.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
/*
This file is Copyright © 1994-1995 Olivier Montanuy,
Copyright © 1999-2005 André Majorel,
Copyright © 2006-2021 contributors to the DeuTex project.
DeuTex incorporates code derived from DEU 5.21 that was put in the
public domain in 1994 by Raphaël Quinet and Brendon Wyber.
SPDX-License-Identifier: GPL-2.0-or-later
*/
/*
* usedidx.c
* Palette index usage statistics (cf -usedidx)
*/
#include "deutex.h"
#include "tools.h"
#include "usedidx.h"
/*
* usedidx_begin_lump
* Call this before the first call to usedidx_count for
* a given lump.
*/
void usedidx_begin_lump(cusage_t * cusage, const char *name)
{
int i;
if (cusage == NULL)
Bug("UI10", "usedidx_begin_lump: null cusage");
cusage->lump_uses = Malloc(NCOLOURS * sizeof *cusage->lump_uses);
for (i = 0; i < NCOLOURS; i++)
cusage->lump_uses[i] = 0;
memcpy(cusage->lump_name, name, sizeof cusage->lump_name);
}
/*
* usedidx_pixel
* Call this for each pixel for a given lump.
*/
void usedidx_pixel(cusage_t * cusage, unsigned char idx)
{
cusage->lump_uses[idx]++;
}
/*
* usedidx_end_lump
* Call this when you're done with a given lump.
*/
void usedidx_end_lump(cusage_t * cusage)
{
int i;
if (cusage == NULL)
Bug("UI20", "usedidx_end_lump: null cusage");
for (i = 0; i < NCOLOURS; i++) {
if (cusage->lump_uses[i] > 0) {
if (cusage->uses[i] == 0)
memcpy(cusage->where_first[i], cusage->lump_name,
sizeof cusage->where_first[i]);
cusage->uses[i] += cusage->lump_uses[i];
cusage->nlumps[i]++;
}
}
free(cusage->lump_uses);
}
/*
* usedidx_rectangle
* Call this to update the cusage_t structure for a whole
* array of pixels at once. The array is supposed not to
* contain transparent pixels so this is not for pictures;
* it's for flats and rectangular graphic lumps (E.G.
* 320x200 "TITLE" from Heretic or Hexen and 10x12
* "GNUM[0-9]" from Doom alpha.)
*/
void usedidx_rectangle(const char *buf, long buf_size, const char *name,
cusage_t * cusage)
{
const unsigned char *p = (const unsigned char *) buf;
const unsigned char *pmax = p + buf_size;
unsigned long *uses = Malloc(NCOLOURS * sizeof *uses);
int i;
for (i = 0; i < NCOLOURS; i++)
uses[i] = 0;
for (; p < pmax; p++)
uses[*p]++;
for (i = 0; i < NCOLOURS; i++) {
if (uses[i] > 0) {
if (cusage->uses[i] == 0)
memcpy(cusage->where_first[i], name, 8);
cusage->uses[i] += uses[i];
cusage->nlumps[i]++;
}
}
free(uses);
}