File: mapcompositingfilter.c

package info (click to toggle)
mapserver 7.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 16,620 kB
  • sloc: ansic: 176,974; cpp: 58,161; python: 2,969; xml: 1,676; cs: 997; yacc: 856; lex: 766; java: 588; perl: 428; makefile: 374; sh: 184; tcl: 158; ruby: 55
file content (223 lines) | stat: -rw-r--r-- 7,977 bytes parent folder | download | duplicates (3)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/******************************************************************************
 *
 * Project:  MapServer
 * Purpose:  RFC 113 Layer compositing
 * Author:   Thomas Bonfort and the MapServer team.
 *
 ******************************************************************************
 * Copyright (c) 1996-2015 Regents of the University of Minnesota.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies of this Software or works derived from this Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *****************************************************************************/
#include "mapserver.h"
#include <regex.h>
#define pixmove(rb,srcx,srcy,dstx,dsty) \
  memcpy(rb->data.rgba.pixels+dsty*rb->data.rgba.row_step+dstx*4,\
          rb->data.rgba.pixels+srcy*rb->data.rgba.row_step+srcx*4,\
          4)
#define pixerase(rb,x,y) memset(rb->data.rgba.pixels+y*rb->data.rgba.row_step+x*4,0,4)

void msApplyTranslationCompositingFilter(rasterBufferObj *rb, int xtrans, int ytrans) {
  int src_sx,src_sy,dst_sx,dst_sy,x,y,dst_x,dst_y;
  if(abs(xtrans)>=rb->width || abs(ytrans)>=rb->height) {
    for(y = 0; y<rb->height; y++)
      for(x = 0; x<rb->width; x++)
        pixerase(rb,x,y);
  }
  if(xtrans == 0 && ytrans == 0)
    return;
  if(xtrans>=0) {
    if(ytrans>=0) {
      src_sx = rb->width - xtrans - 1;
      src_sy = rb->height - ytrans - 1;
      dst_sx = rb->width - 1;
      dst_sy = rb->height -1;
      for(y = src_sy,dst_y= dst_sy;y>=0;y--,dst_y--) {
        for(x = src_sx,dst_x= dst_sx;x>=0;x--,dst_x--) {
          pixmove(rb,x,y,dst_x,dst_y);
        }
      }
      for(y=0;y<ytrans;y++)
        for(x=0;x<rb->width;x++)
          pixerase(rb,x,y);  
      for(y=ytrans;y<rb->height;y++)
        for(x=0;x<xtrans;x++)
          pixerase(rb,x,y);
    } else {
      src_sx = rb->width - xtrans - 1;
      src_sy = - ytrans;
      dst_sx = rb->width - 1;
      dst_sy = 0;
      for(y = src_sy,dst_y= dst_sy;y<rb->height;y++,dst_y++) {
        for(x = src_sx,dst_x= dst_sx;x>=0;x--,dst_x--) {
          pixmove(rb,x,y,dst_x,dst_y);
        }
      }
      for(y=0;y<rb->height+ytrans;y++)
        for(x=0;x<xtrans;x++)
          pixerase(rb,x,y);  
      for(y=rb->height+ytrans;y<rb->height;y++)
        for(x=0;x<rb->width;x++)
          pixerase(rb,x,y);
    }
  } else {
    if(ytrans>=0) {
      src_sx = - xtrans;
      src_sy = rb->height - ytrans - 1;
      dst_sx = 0;
      dst_sy = rb->height -1;
      for(y = src_sy,dst_y= dst_sy;y>=0;y--,dst_y--) {
        for(x = src_sx,dst_x= dst_sx;x<rb->width;x++,dst_x++) {
          pixmove(rb,x,y,dst_x,dst_y);
        }
      }
      for(y=0;y<ytrans;y++)
        for(x=0;x<rb->width;x++)
          pixerase(rb,x,y);
      for(y=ytrans;y<rb->height;y++)
        for(x=rb->width+xtrans;x<rb->width;x++)
          pixerase(rb,x,y);
    } else {
      src_sx = - xtrans;
      src_sy = - ytrans;
      dst_sx = 0;
      dst_sy = 0;
      for(y = src_sy,dst_y= dst_sy;y<rb->height;y++,dst_y++) {
        for(x = src_sx,dst_x= dst_sx;x<rb->width;x++,dst_x++) {
          pixmove(rb,x,y,dst_x,dst_y);
        }
      }
      for(y=0;y<rb->height+ytrans;y++)
        for(x=rb->width+xtrans;x<rb->width;x++)
          pixerase(rb,x,y);
      for(y=rb->height+ytrans;y<rb->height;y++)
        for(x=0;x<rb->width;x++)
          pixerase(rb,x,y);
    }
  }
}

void msApplyBlackeningCompositingFilter(rasterBufferObj *rb) {
  int row,col;
  unsigned char *r,*g,*b;
  for(row=0;row<rb->height;row++) {
    r = rb->data.rgba.r + row*rb->data.rgba.row_step;
    g = rb->data.rgba.g + row*rb->data.rgba.row_step;
    b = rb->data.rgba.b + row*rb->data.rgba.row_step;
    for(col=0;col<rb->width;col++) {
      *r = *g = *b = 0;
      r+=4;g+=4;b+=4;
    }    
  }
}

void msApplyWhiteningCompositingFilter(rasterBufferObj *rb) {
  int row,col;
  unsigned char *r,*g,*b,*a;
  for(row=0;row<rb->height;row++) {
    r = rb->data.rgba.r + row*rb->data.rgba.row_step;
    g = rb->data.rgba.g + row*rb->data.rgba.row_step;
    b = rb->data.rgba.b + row*rb->data.rgba.row_step;
    a = rb->data.rgba.a + row*rb->data.rgba.row_step;
    for(col=0;col<rb->width;col++) {
      *r = *g = *b = *a;
      r+=4;g+=4;b+=4;a+=4;
    }    
  }
}

void msApplyGrayscaleCompositingFilter(rasterBufferObj *rb) {
  int row,col;
  unsigned char *r,*g,*b;
  for(row=0;row<rb->height;row++) {
    r = rb->data.rgba.r + row*rb->data.rgba.row_step;
    g = rb->data.rgba.g + row*rb->data.rgba.row_step;
    b = rb->data.rgba.b + row*rb->data.rgba.row_step;
    for(col=0;col<rb->width;col++) {
      unsigned int mix = (unsigned int)*r + (unsigned int)*g + (unsigned int)*b;
      mix /=3;
      *r = *g = *b = (unsigned char)mix;
      r+=4;g+=4;b+=4;
    }    
  }
}

int msApplyCompositingFilter(mapObj *map, rasterBufferObj *rb, CompositingFilter *filter) {
  int rstatus;
  regex_t regex;
  regmatch_t pmatch[3];
  
  /* test for blurring filter */
  regcomp(&regex, "blur\\(([0-9]+)\\)", REG_EXTENDED);
  rstatus = regexec(&regex, filter->filter, 2, pmatch, 0);
  regfree(&regex);
  if(!rstatus) {
    char *rad = malloc(pmatch[1].rm_eo - pmatch[1].rm_so + 1);
    unsigned int irad;
    strncpy(rad,filter->filter+pmatch[1].rm_so,pmatch[1].rm_eo-pmatch[1].rm_so);
    rad[pmatch[1].rm_eo - pmatch[1].rm_so]=0;
    //msDebug("got blur filter with radius %s\n",rad);
    irad = atoi(rad);
    free(rad);
    irad = MS_NINT(irad*map->resolution/map->defresolution);
    msApplyBlurringCompositingFilter(rb,irad);
    return MS_SUCCESS;
  }
  
  /* test for translation filter */
  regcomp(&regex, "translate\\((-?[0-9]+),(-?[0-9]+)\\)", REG_EXTENDED);
  rstatus = regexec(&regex, filter->filter, 3, pmatch, 0);
  regfree(&regex);
  if(!rstatus) {
    char *num;
    int xtrans,ytrans;
    num = malloc(pmatch[1].rm_eo - pmatch[1].rm_so + 1);
    strncpy(num,filter->filter+pmatch[1].rm_so,pmatch[1].rm_eo-pmatch[1].rm_so);
    num[pmatch[1].rm_eo - pmatch[1].rm_so]=0;
    xtrans = atoi(num);
    free(num);
    num = malloc(pmatch[2].rm_eo - pmatch[2].rm_so + 1);
    strncpy(num,filter->filter+pmatch[2].rm_so,pmatch[2].rm_eo-pmatch[2].rm_so);
    num[pmatch[2].rm_eo - pmatch[2].rm_so]=0;
    ytrans = atoi(num);
    free(num);
    //msDebug("got translation filter of radius %d,%d\n",xtrans,ytrans);
    xtrans = MS_NINT(xtrans*map->resolution/map->defresolution);
    ytrans = MS_NINT(ytrans*map->resolution/map->defresolution);
    msApplyTranslationCompositingFilter(rb,xtrans,ytrans);
    return MS_SUCCESS;
  }
  
  /* test for grayscale filter */
  if(!strncmp(filter->filter,"grayscale()",strlen("grayscale()"))) {
    msApplyGrayscaleCompositingFilter(rb);
    return MS_SUCCESS;
  }
  if(!strncmp(filter->filter,"blacken()",strlen("blacken()"))) {
    msApplyBlackeningCompositingFilter(rb);
    return MS_SUCCESS;
  }
  if(!strncmp(filter->filter,"whiten()",strlen("whiten()"))) {
    msApplyWhiteningCompositingFilter(rb);
    return MS_SUCCESS;
  }
  
  msSetError(MS_MISCERR,"unknown compositing filter (%s)", "msApplyCompositingFilter()", filter->filter);
  return MS_FAILURE;
}