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

Last change on this file since 512 was 512, checked in by Sam Hocevar, 7 years ago

imlib: use vec2i for image::size and unroll all necessary changes
everywhere else in the code.

File size: 5.2 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 or
8 *  Jonathan Clark.
9 */
10
11#include "config.h"
12
13#include "common.h"
14
15#include "image.h"
16#include "macs.h"
17#include "filter.h"
18
19extern unsigned char current_background;
20
21filter::filter(palette *from, palette *to)   // creates a conversion filter from one palette to another
22{
23  nc=from->pal_size() > to->pal_size() ? from->pal_size() : to->pal_size();
24  unsigned char *p=fdat=(unsigned char *)malloc(nc);
25  unsigned char *r,*g,*b;
26  r=g=b=(unsigned char *)from->addr();
27  g++;
28  b+=2;
29
30  int dk=to->darkest(1);
31  for (int i=0; i<nc; i++,p++,r+=3,g+=3,b+=3)
32  {
33    *p=to->find_closest(*r,*g,*b);
34
35    // make sure non-blacks don't get remapped to the transparency
36    if ((*r!=0 || *g!=0 || *b!=0) && (to->red(*p)==0 && to->green(*p)==0 && to->blue(*p)==0))
37      *p=dk;
38  }
39
40}
41
42void filter::clear()
43{
44  int i;
45  for (i=0; i<nc; i++)
46    fdat[i]=i;
47}
48
49void filter::max_threshold(int minv, char blank)
50{
51  int i;
52  CONDITION(minv>=0 && minv<nc,"Bad minv");
53  for (i=0; i<minv; i++)
54    fdat[i]=blank;
55}
56
57void filter::min_threshold(int maxv, char blank)
58{
59  int i;
60  CONDITION(maxv>=0 && maxv<nc,"bad maxv value in filter::max_thresh");
61  for (i=nc-1; i>=maxv; i--)
62    fdat[i]=(unsigned) blank;
63}
64
65
66void filter::set(int color_num, char change_to)
67{
68  CONDITION(color_num>=0 && color_num<nc,"Bad colors_num");
69  fdat[color_num]=(unsigned) change_to;
70}
71
72
73filter::filter(int colors)
74{
75  CONDITION(colors>=0 && colors<=256,"bad colors value");
76  nc=colors;
77  make_block(nc);
78  fdat=(unsigned char *)malloc(nc);
79  clear();
80}
81
82void filter::apply(image *im)
83{
84  int x,y;
85  unsigned char *c;
86  CONDITION(im,"null image passed in filter::apply\n");
87  im->lock();
88  for (y=im->Size().y-1; y>=0; y--)
89  {
90    c=im->scan_line(y);
91    for (x=im->Size().x-1; x>=0; x--)
92    {
93      CONDITION((unsigned) c[x]<nc,"not enough filter colors");
94      c[x]=fdat[(unsigned) c[x]];
95    }
96  }
97  im->unlock();
98}
99
100
101palette *compare_pal;
102
103int color_compare(void *c1, void *c2)
104{
105  long v1,v2;
106  unsigned char r1,g1,b1,r2,g2,b2;
107  compare_pal->get(  *((unsigned char *)c1),r1,g1,b1);
108  compare_pal->get(  *((unsigned char *)c2),r2,g2,b2);
109  v1=(int)r1*(int)r1+(int)g1*(int)g1+(int)b1*(int)b1;
110  v2=(int)r2*(int)r2+(int)g2*(int)g2+(int)b2*(int)b2;
111  if (v1<v2) return -1;
112  else if (v1>v2) return 1;
113  else return 0;
114}
115
116color_filter::color_filter(palette *pal, int color_bits, void (*stat_fun)(int))
117{
118  color_bits=5;      // hard code 5 for now
119  int r,g,b,rv,gv,bv,
120      c=0,i,max=pal->pal_size(),
121      lshift=8-color_bits;
122  unsigned char *pp;
123
124  long dist_sqr,best;
125  int colors=1<<color_bits;
126  color_table=(unsigned char *)malloc(colors*colors*colors);
127  for (r=0; r<colors; r++)
128  {
129    if (stat_fun) stat_fun(r);
130    rv=r<<lshift;
131    for (g=0; g<colors; g++)
132    {
133      gv=g<<lshift;
134      for (b=0; b<colors; b++)
135      {
136    bv=b<<lshift;
137        best=0x7fffffff;
138        for (i=0,pp=(unsigned char *)pal->addr(); i<max; i++)
139        {
140          register long rd=*(pp++)-rv,
141                        gd=*(pp++)-gv,
142                        bd=*(pp++)-bv;
143
144          dist_sqr=(long)rd*rd+(long)bd*bd+(long)gd*gd;
145          if (dist_sqr<best)
146          { best=dist_sqr;
147            c=i;
148          }
149        }
150        color_table[r*colors*colors+g*colors+b]=c;
151      }
152    }
153  }
154}
155
156color_filter::color_filter(spec_entry *e, bFILE *fp)
157{
158  fp->seek(e->offset,0);
159  fp->read_uint16();
160  int colors=32;
161  color_table=(unsigned char *)malloc(colors*colors*colors);
162  fp->read(color_table,colors*colors*colors);
163}
164
165int color_filter::size()
166{
167  int colors=32;
168  return 2+colors*colors*colors;
169}
170
171int color_filter::write(bFILE *fp)
172{
173  int colors=32;
174  fp->write_uint16(colors);
175  return fp->write(color_table,colors*colors*colors)==colors*colors*colors;
176}
177
178
179void filter::put_image(image *screen, image *im, short x, short y,
180                       char transparent)
181{
182    short cx1, cy1, cx2, cy2, x1 = 0, y1 = 0,
183          x2 = im->Size().x - 1, y2 = im->Size().y - 1;
184    screen->get_clip(cx1,cy1,cx2,cy2);
185
186    // see if the image gets clipped off the screen
187    if(x > cx2 || y > cy2 || x + (x2 - x1) < cx1 || y + (y2 - y1) < cy1)
188        return;
189
190    if(x < cx1)
191    {
192        x1 += (cx1 - x);
193        x = cx1;
194    }
195    if(y < cy1)
196    {
197        y1 += (cy1 - y);
198         y = cy1;
199    }
200
201    if(x + x2 - x1 + 1 > cx2)
202        x2 = cx2 - x + x1;
203
204    if(y + y2 - y1 + 1 > cy2)
205        y2 = cy2 - y + y1;
206
207    if(x1 > x2 || y1 > y2)
208        return;
209
210    int xl = x2 - x1 + 1;
211    int yl = y2 - y1 + 1;
212
213    screen->add_dirty(x, y, x + xl - 1, y + yl - 1);
214
215    screen->lock();
216    im->lock();
217
218    uint8_t *pg1 = screen->scan_line(y), *source, *dest;
219    uint8_t *pg2 = im->scan_line(y1);
220    int i;
221    for(int j = 0; j < yl; j++)
222    {
223        for(i = 0, source = &pg2[x1], dest = &pg1[x];
224            i < xl;
225            i++, source++, dest++)
226        {
227            if(!transparent || *source != current_background)
228                *dest=fdat[*source];
229        }
230        pg1 = screen->next_line(y + j, pg1);
231        pg2 = im->next_line(y1 + j, pg2);
232    }
233
234    im->unlock();
235    screen->unlock();
236}
237
Note: See TracBrowser for help on using the repository browser.