File: tilebuf.c

package info (click to toggle)
gimp-plugin-registry 7.20140602
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 55,856 kB
  • ctags: 4,984
  • sloc: ansic: 46,100; lisp: 20,560; python: 14,721; sh: 3,306; cpp: 2,528; makefile: 1,023; xml: 546
file content (407 lines) | stat: -rw-r--r-- 11,939 bytes parent folder | download
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/* Refocus plug-in
 * Copyright (C) 1999-2003 Ernst Lippe
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * Version $Id: tilebuf.c,v 1.1.1.1 2003/01/30 21:30:19 ernstl Exp $
 */

#include <stdio.h>
#include "tilebuf.h"
#include "util.h"
#include <string.h>

#ifndef lint
static char vcid[] GCC_UNUSED = "$Id: tilebuf.c,v 1.1.1.1 2003/01/30 21:30:19 ernstl Exp $";
#endif /* lint */

/**
 * pixpos_in_buf:
 * @buf: 
 * @x: x coordinate of pixel.
 * @y: y coordinate of pixel.
 * 
 * Return a pointer to the pixel in @buf with coordinates (@x,@y)
 * 
 * Return value: 
 **/
guchar *
pixpos_in_buf (TileStripBuffer * buf, const gint x, const gint y)
{
  /*
    & data = array(? lines x row_stride columns)
    & pixpos_in_buf = &data[(x - real_x)*bytes_per_pixel, y - real_y]
    & (real_x, real_y) = origin of tile.
   */
  return (&buf->data[buf->row_stride * (y - buf->real_y) +
                     buf->bpp * (x - buf->real_x)]);
}

static void
copy_tile_to_buf (TileStripBuffer * buf, TileSource * source, gint x, gint y)
     /* Copy the tile from at position (x,y) to buf */
{
  const gint tile_x = floorm (x, tile_width ());
  const gint tile_y = floorm (y, tile_height ());

  if ((tile_x >= 0) && (tile_x < source->width) &&
      (tile_y >= 0) && (tile_y < source->height))
    {
      GimpTile *tile = tile_source_get_tile (source, tile_x, tile_y);

      copy_rect (buf->data,
                 buf->real_x, buf->real_y, buf->real_width, buf->real_height,
                 tile->data,
                 tile_x, tile_y, tile->ewidth, tile->eheight, buf->bpp);
      tile_source_tile_unref (source, tile);
    }
}

static void
zero_rect_in_buf (TileStripBuffer * buf, gint x, gint y, gint w, gint h)
{
  gint row;
  guchar *p = pixpos_in_buf (buf, x, y);

  for (row = h; row > 0; row--)
    {
      memset (p, 0, w * buf->bpp);
      p += buf->row_stride;
    };
}

void
finalize_buf (TileStripBuffer * buf)
     /* Free the buffers */
{
  if (buf && buf->data)
    {
      g_free (buf->data);
    };
}

static void
copy_col (TileStripBuffer * buf, const gint x, const gint y,
          const gint height, const gint x2)
     /* Copy one collumn of pixels. The collumn starts at coordinates (x,y) */
     /* with height pixels. The destination collumn starts at (x2,y) */
{
  const gint row_stride = buf->row_stride;
  const gint bpp_minus1 = buf->bpp - 1;
  register guchar *src = pixpos_in_buf (buf, x, y);
  guchar *dest = pixpos_in_buf (buf, x2, y);
  register gint row_nr, b;

  for (row_nr = height - 1; row_nr >= 0; row_nr--)
    {
      for (b = bpp_minus1; b >= 0; b--)
        {
          dest[b] = src[b];
        };
      src += row_stride;
      dest += row_stride;
    };
}

static void
copy_row (TileStripBuffer * buf, const gint x, const gint y, const gint width,
          const gint y2)
     /* Copy one row of pixels. The collumn starts at coordinates (x,y) */
     /* with with pixels. The destination collumn starts at (x,y2) */
{
  memmove (pixpos_in_buf (buf, x, y2),
           pixpos_in_buf (buf, x, y), width * buf->bpp);
}

static void
fix_left_boundary (TileStripBuffer * buf, const gint x_lo, const gint y_start,
                   const gint y_end)
{
  if ((x_lo <= buf->real_x) || (y_end <= y_start))
    {
      return;
    };

  switch (buf->boundary_type)
    {
    case TB_BOUNDARY_ZERO:
      zero_rect_in_buf (buf, buf->real_x, y_start,
                        x_lo - buf->real_x, y_end - y_start);
      break;
    case TB_BOUNDARY_MIRROR:
      {
        register gint x;

        for (x = x_lo - 1; x >= buf->real_x; x--)
          {
            copy_col (buf, 2 * x_lo - x, y_start, y_end - y_start, x);
          };
      }
      break;
    default:
      break;
    }
}

static void
fix_right_boundary (TileStripBuffer * buf, const gint x_hi,
                    const gint y_start, const gint y_end)
{
  if ((x_hi >= buf->real_x + buf->real_width) || (y_end <= y_start))
    {
      return;
    };

  switch (buf->boundary_type)
    {
    case TB_BOUNDARY_ZERO:
      zero_rect_in_buf (buf, x_hi, y_start,
                        buf->real_x + buf->real_width - x_hi,
                        y_end - y_start);
      break;
    case TB_BOUNDARY_MIRROR:
      {
        register gint x;

        for (x = x_hi; x < buf->real_x + buf->real_width; x++)
          {
            copy_col (buf, 2 * (x_hi - 1) - x, y_start, y_end - y_start, x);
          };
      }
      break;
    default:
      break;
    }
}

static void
fix_top_boundary (TileStripBuffer * buf, const gint y_lo)
{
  if (y_lo <= buf->real_y)
    {
      return;
    };

  switch (buf->boundary_type)
    {
    case TB_BOUNDARY_ZERO:
      zero_rect_in_buf (buf, buf->real_x, buf->real_y,
                        buf->real_width, y_lo - buf->real_y);
      break;
    case TB_BOUNDARY_MIRROR:
      {
        register gint y;

        for (y = y_lo - 1; y >= buf->real_y; y--)
          {
            copy_row (buf, buf->real_x, 2 * y_lo - y, buf->real_width, y);
          };
      }
      break;
    default:
      break;
    }
}

static void
fix_bottom_boundary (TileStripBuffer * buf, const gint y_hi)
{
  if (y_hi >= buf->real_y + buf->real_height)       /* (1) */
    {
      return;
    };

  switch (buf->boundary_type)
    {
    case TB_BOUNDARY_ZERO:
      zero_rect_in_buf (buf, buf->real_x, y_hi,
                        buf->real_width,
                        buf->real_y + buf->real_height - y_hi);
      break;
    case TB_BOUNDARY_MIRROR:
      {
        register gint y2;

        /*if (y_hi < buf->y_limit)*/
        for (y2 = y_hi; y2 < buf->real_y + buf->real_height; y2++) /*(2)*/
          {
            /*
              &1 y < y_hi              [domain of y, definition of y_hi, (1)]
              &2 y_hi <= y2 < buf->real_y + buf->real_height  [(1),(2)]
              &3 i = 0, 1, ...
              &4 y=y_hi-i-1 -> y2=y_hi+i               [definition of mirror]
              &5 y = y2 - 2*i - 1                      [4]
              &6 y = 2*y2 - 2*i - y2 - 1               [5,algebra]
              &7 y = 2*y_hi - y2 - 1                   [4,6]
            */
            gint y =  2*y_hi - y2 - 1;
            if (y < buf->real_y) break;
#ifdef RLXTEST
           printf("fix_bottom_boundary: Copying row %d to %d, width %d, buf_loc %d %d, real_y %d\n",
                  2*y_hi - y2 - 1, y2, buf->real_width,
                  buf->row_stride * (2*y_hi - y2 - 1 - buf->real_y) +
                   buf->bpp * (buf->real_x - buf->real_x),
                  buf->row_stride * (y2 - buf->real_y) +
                   buf->bpp * (buf->real_x - buf->real_x),
                   buf->real_y);
#endif
            copy_row (buf, buf->real_x, y, buf->real_width,y2);
          };
      }
      break;
    default:
      break;
    }
}

static void
fix_boundaries (TileStripBuffer * buf, TileSource * source,
                gboolean first_time)
     /* Fix the boundary areas in buf that fall outside the source. */
     /* When first_time is FALSE only the area with y > buf->y + tile_height */
     /* will be fixed */
{
  const gint x_lo = MAX (buf->real_x, source->x);
  const gint x_hi = MIN (buf->real_x + buf->real_width,
                         source->x + source->width);
  const gint y_lo = MAX (buf->real_y, source->y);
  const gint y_hi = MIN (buf->real_y + buf->real_height,
                         source->y + source->height);
  /*
    y:  domain of y.
    & 0 <= y.  y is a 0-index.
    & | & y in the source image
        & 0 <=  source->y <= y < source->y + source->height
      | & y in the buffer
        & 0 <= buf->real_y <= y < buf->real_y + buf->real_height
    x:  domain of x = domain of y with mapping y -> x, height -> width.
   */

#ifdef RLXTEST
  printf("buf->real_y %d, buf->real_height %d, source->y %d, source->heigh %d, y_hi %d\n",
         buf->real_y,  buf->real_height,
       source->y, source->height, y_hi);
#endif
  fix_left_boundary (buf, x_lo, first_time ? buf->y : buf->y + tile_height (),
                     y_hi);
  fix_right_boundary (buf, x_hi,
                      first_time ? buf->y : buf->y + tile_height (), y_hi);
  if (first_time)
    {
      fix_top_boundary (buf, y_lo);
    };
#ifdef RLXTEST
  printf("fix_boundaries: Now goto fix_bottom_boundary: y_hi %d, real_y + real_height %d, data[%d]\n",
         y_hi, buf->real_y + buf->real_height,
         buf->real_height * buf->row_stride);
#endif
  fix_bottom_boundary (buf, y_hi);
#ifdef RLXTEST
  printf("bottom boundary fixed\n");
#endif
}

void
initialize_buf (TileStripBuffer * buf, TileSource * source,
                gint border_width, gint sx, gint sy, gint width, gint height,
                TB_BOUNDARY_TYPE boundary_type)
{
  gint x, y;

  buf->bpp = source->bpp;
  buf->border_width = border_width;
  buf->x = floorm (sx, tile_width ());
  buf->width = ceilm (width + sx - buf->x, tile_width ());
  buf->y = floorm (sy, tile_height ());
  buf->y_limit = sy + height;
  buf->height = tile_height ();
  buf->real_x = buf->x - buf->border_width;
  buf->real_y = buf->y - buf->border_width;
  buf->real_width = buf->width + 2 * buf->border_width;
  buf->real_height = buf->height + buf->border_width + tile_height ();
  buf->row_stride = buf->real_width * buf->bpp;
  buf->first_time = TRUE;
  buf->boundary_type = boundary_type;
  buf->data = g_new0 (guchar, buf->real_height * buf->row_stride);
  for (y = buf->y - tile_height ();
       y <= buf->y + tile_height (); y += tile_height ())
    {
      for (x = buf->x - tile_width ();
           x <= buf->x + buf->width; x += tile_width ())
        {
          copy_tile_to_buf (buf, source, x, y);
        };
    };
  fix_boundaries (buf, source, TRUE);
}


gboolean
shift_buf (TileStripBuffer * buf, TileSource * source)
 /* Shift the buffer to its next position.
    Returns FALSE when the entire image has been processed.
  */
{
  gint x;
  gboolean not_finished = TRUE;

  /*
    & y_limit = height of picture, if full picture.
   */
#ifdef RLXTEST
  printf("buf->y %d + tile_height%d () >= buf->y_limit %d\n",
         buf->y,tile_height(),buf->y_limit);
#endif

  if (buf->first_time)
    {
      /* Buf must already have been initialized so there is nothing to do */
      buf->first_time = FALSE;
    }
  else if (buf->y + tile_height () >= buf->y_limit)
    {
      /* We are finished */
      not_finished = FALSE;
    }
  else
    {
#ifdef RLXTEST
      printf("shift_buf: tile_height %d\n", tile_height());
#endif
      memmove (pixpos_in_buf (buf, buf->real_x,
                              buf->real_y),
               pixpos_in_buf (buf, buf->real_x,
                              buf->real_y + tile_height ()),
               buf->row_stride * (tile_height () + buf->border_width));
      buf->y += tile_height ();
#ifdef RLXTEST
      printf("shift_buf: y %d, y_limit %d\n", buf->y,  buf->y_limit);
#endif
      buf->real_y = buf->y - buf->border_width;
      for (x = buf->x - tile_width ();
           x <= buf->x + buf->width; x += tile_width ())
        {
          copy_tile_to_buf (buf, source, x, buf->y + tile_height ());
#ifdef RLXTEST
          printf("shift_buf: x %d\n", x);
#endif
        };
      fix_boundaries (buf, source, FALSE);
    };
#ifdef RLXTEST
  printf("shift_buf: done\n");
#endif
  return (not_finished);
}