summaryrefslogtreecommitdiff
path: root/im/src/process/im_effects.cpp
blob: 7f65ce6027a1fb0e893d7fc12c34979400f399bc (plain)
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
/** \file
 * \brief Effects
 *
 * See Copyright Notice in im_lib.h
 * $Id: im_effects.cpp,v 1.1 2008/10/17 06:16:33 scuri Exp $
 */


#include <im.h>
#include <im_util.h>
#include <im_math.h>
#include <im_complex.h>

#include "im_process_pon.h"
#include "im_math_op.h"

#include <stdlib.h>
#include <memory.h>

static unsigned char BoxMean(imbyte *map, int offset, int shift, int hbox_size, int vbox_size)
{
  map += offset;
  int acum = 0;
  for (int i = 0; i < vbox_size; i++)
  {
    for (int j = 0; j < hbox_size; j++)
    {
      acum += *map++;
    }

    map += shift;
  }

  return (unsigned char)(acum / (vbox_size*hbox_size));
}

static void BoxSet(imbyte *map, int offset, int shift, int hbox_size, int vbox_size, unsigned char value)
{
  map += offset;
  for (int i = 0; i < vbox_size; i++)
  {
    for (int j = 0; j < hbox_size; j++)
    {
      *map++ = value;
    }

    map += shift;
  }
}

void imProcessPixelate(const imImage* src_image, imImage* dst_image, int box_size)
{
  int hbox = ((src_image->width + box_size-1)/ box_size);
  int vbox = ((src_image->height + box_size-1)/ box_size);

  for (int i = 0; i < src_image->depth; i++)
  {
    imbyte *src_map=(imbyte*)src_image->data[i];
    imbyte *dst_map=(imbyte*)dst_image->data[i];
    int vbox_size = box_size;

    for (int bv = 0; bv < vbox; bv++)
    {
      int bv_pos = bv*box_size;
      if (bv == vbox-1) vbox_size = src_image->height - bv_pos;
      int hbox_size = box_size;

      for (int bh = 0; bh < hbox; bh++)
      {
        int bh_pos = bh*box_size;
        if (bh == hbox-1) hbox_size = src_image->width - bh_pos;
        int offset = bv_pos*src_image->width + bh_pos;
        int shift = src_image->width - hbox_size;
        unsigned char mean = BoxMean(src_map, offset, shift, hbox_size, vbox_size);
        BoxSet(dst_map, offset, shift, hbox_size, vbox_size, mean);
      }
    }
  }
}

void imProcessPosterize(const imImage* src_image, imImage* dst_image, int level)
{
  unsigned char mask = (unsigned char)(0xFF << level);
  imProcessBitMask(src_image, dst_image, mask, IM_BIT_AND);
}