source: abuse/trunk/src/imlib/filter.cpp

Last change on this file was 682, checked in by Sam Hocevar, 8 years ago

core: rename vec2i to ivec2 and update matrix.h from Lol Engine.

File size: 4.1 KB
Line 
1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
4 *  Copyright (c) 2005-2011 Sam Hocevar <sam@hocevar.net>
5 *
6 *  This software was released into the Public Domain. As with most public
7 *  domain software, no warranty is made or implied by Crack dot Com, by
8 *  Jonathan Clark, or by Sam Hocevar.
9 */
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#include "common.h"
16
17#include "image.h"
18#include "filter.h"
19
20Filter::Filter(int colors)
21{
22    CONDITION(colors >= 0 && colors <= 256, "bad colors value");
23    m_size = colors;
24    m_table = (uint8_t *)malloc(m_size);
25    memset(m_table, 0, m_size * sizeof(*m_table));
26}
27
28// Creates a conversion filter from one palette to another
29Filter::Filter(palette *from, palette *to)
30{
31    m_size = Max(from->pal_size(), to->pal_size());
32    m_table = (uint8_t *)malloc(m_size);
33
34    uint8_t *dst = m_table;
35    uint8_t *src = (uint8_t *)from->addr();
36    int dk = to->darkest(1);
37
38    for (int i = 0; i < m_size; i++)
39    {
40       int r = *src++;
41       int g = *src++;
42       int b = *src++;
43       int color = to->find_closest(r, g, b);
44
45       // Make sure non-blacks don't get remapped to the transparency
46       if ((r || g || b) && to->red(color) == 0
47            && to->green(color) == 0 && to->blue(color) == 0)
48           color = dk;
49
50       *dst++ = color;
51    }
52}
53
54Filter::~Filter()
55{
56    free(m_table);
57}
58
59void Filter::Set(int color_num, int change_to)
60{
61    CONDITION(color_num >= 0 && color_num < m_size, "Bad colors_num");
62    m_table[color_num] = change_to;
63}
64
65void Filter::Apply(image *im)
66{
67    im->Lock();
68    uint8_t *dst = im->scan_line(0);
69    int npixels = im->Size().x * im->Size().y;
70    while (npixels--)
71    {
72        CONDITION(*dst < m_size, "not enough filter colors");
73        *dst = m_table[*dst];
74        dst++;
75    }
76    im->Unlock();
77}
78
79/* This is only ever used in the editor, when showing the toolbar. It
80 * does not look like it's very useful. */
81void Filter::PutImage(image *screen, image *im, ivec2 pos)
82{
83    ivec2 aa = ivec2(0), bb = im->Size(), caa, cbb;
84    screen->GetClip(caa, cbb);
85
86    // See if the image gets clipped off the screen
87    if (!(pos < cbb && pos + (bb - aa) > caa))
88        return;
89
90    aa += Max(caa - pos, 0);
91    pos = Max(pos, caa);
92    bb = Min(bb, cbb - pos + aa);
93
94    if (!(aa < bb))
95        return;
96
97    ivec2 span = bb - aa;
98
99    screen->AddDirty(pos, pos + span);
100
101    screen->Lock();
102    im->Lock();
103
104    for (int j = 0; j < span.y; j++)
105    {
106        uint8_t *src = im->scan_line(aa.y + j) + aa.x;
107        uint8_t *dst = screen->scan_line(pos.y + j) + pos.x;
108
109        for (int i = 0; i < span.x; i++, src++, dst++)
110            if (*src)
111                *dst = m_table[*src];
112    }
113
114    im->Unlock();
115    screen->Unlock();
116}
117
118ColorFilter::ColorFilter(palette *pal, int color_bits)
119{
120    int max = pal->pal_size();
121    int mul = 1 << (8 - color_bits);
122    m_size = 1 << color_bits;
123    m_table = (uint8_t *)malloc(m_size * m_size * m_size);
124
125    /* For each colour in the RGB cube, find the nearest palette element. */
126    for (int r = 0; r < m_size; r++)
127    for (int g = 0; g < m_size; g++)
128    for (int b = 0; b < m_size; b++)
129    {
130        int best = 256 * 256 * 3;
131        int color = 0;
132        uint8_t *pp = (uint8_t *)pal->addr();
133
134        for (int i = 0; i < max; i++)
135        {
136            int rd = *pp++ - r * mul,
137                gd = *pp++ - g * mul,
138                bd = *pp++ - b * mul;
139
140            int dist = rd * rd + bd * bd + gd * gd;
141            if (dist < best)
142            {
143                best = dist;
144                color = i;
145            }
146        }
147        m_table[(r * m_size + g) * m_size + b] = color;
148    }
149}
150
151ColorFilter::ColorFilter(spec_entry *e, bFILE *fp)
152{
153    fp->seek(e->offset, 0);
154    m_size = fp->read_uint16();
155    m_table = (uint8_t *)malloc(m_size * m_size * m_size);
156    fp->read(m_table, m_size * m_size * m_size);
157}
158
159ColorFilter::~ColorFilter()
160{
161    free(m_table);
162}
163
164size_t ColorFilter::DiskUsage()
165{
166    return sizeof(uint16_t) + m_size * m_size * m_size;
167}
168
169int ColorFilter::Write(bFILE *fp)
170{
171    fp->write_uint16(m_size);
172    int bytes = m_size * m_size * m_size;
173    return fp->write(m_table, bytes) == bytes;
174}
175
Note: See TracBrowser for help on using the repository browser.