Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 192 lines (169 sloc) 4.493 kb
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
#include "imager.h"

static void flip_h(i_img *im);
static void flip_v(i_img *im);
static void flip_hv(i_img *im);

#define XAXIS 0
#define YAXIS 1
#define XYAXIS 2

/*
=item i_flipxy(im, axis)

Flips the image inplace around the axis specified.
Returns 0 if parameters are invalid.

   im - Image pointer
   axis - 0 = x, 1 = y, 2 = both

=cut
*/

undef_int
i_flipxy(i_img *im, int direction) {
  i_clear_error();

  mm_log((1, "i_flipxy(im %p, direction %d)\n", im, direction ));

  if (!im)
    return 0;

  switch (direction) {
  case XAXIS: /* Horizontal flip */
    flip_h(im);
    break;

  case YAXIS: /* Vertical flip */
    flip_v(im);
    break;

  case XYAXIS: /* Horizontal and Vertical flip */
    flip_hv(im);
    break;

  default:
    mm_log((1, "i_flipxy: direction is invalid\n" ));
    i_push_errorf(0, "direction %d invalid", direction);
    return 0;

  }
  return 1;
}

static void
flip_row_pal(i_palidx *row, i_img_dim width) {
  i_palidx tmp;
  i_palidx *leftp = row;
  i_palidx *rightp = row + width - 1;
  
  while (leftp < rightp) {
    tmp = *leftp;
    *leftp = *rightp;
    *rightp = tmp;
    ++leftp;
    --rightp;
  }
}

#code

static void
IM_SUFFIX(flip_row)(IM_COLOR *row, i_img_dim width) {
  IM_COLOR tmp;
  IM_COLOR *leftp = row;
  IM_COLOR *rightp = row + width - 1;
  
  while (leftp < rightp) {
    tmp = *leftp;
    *leftp = *rightp;
    *rightp = tmp;
    ++leftp;
    --rightp;
  }
}

#/code

static void
flip_h(i_img *im) {
  i_img_dim y;
  if (im->type == i_palette_type) {
    i_palidx *line = mymalloc(im->xsize * sizeof(i_palidx));
    for (y = 0; y < im->ysize; ++y) {
      i_gpal(im, 0, im->xsize, y, line);
      flip_row_pal(line, im->xsize);
      i_ppal(im, 0, im->xsize, y, line);
    }
    myfree(line);
  }
  else {
#code im->bits == i_8_bits
    IM_COLOR *line = mymalloc(im->xsize * sizeof(IM_COLOR));
    for (y = 0; y < im->ysize; ++y) {
      IM_GLIN(im, 0, im->xsize, y, line);
      IM_SUFFIX(flip_row)(line, im->xsize);
      IM_PLIN(im, 0, im->xsize, y, line);
    }
    myfree(line);
#/code
  }
}

static void
flip_v(i_img *im) {
  i_img_dim topy = 0;
  i_img_dim boty = im->ysize - 1;
  if (im->type == i_palette_type) {
    i_palidx *top_line = mymalloc(im->xsize * sizeof(i_palidx));
    i_palidx *bot_line = mymalloc(im->xsize * sizeof(i_palidx));
    while (topy < boty) {
      i_gpal(im, 0, im->xsize, topy, top_line);
      i_gpal(im, 0, im->xsize, boty, bot_line);
      i_ppal(im, 0, im->xsize, topy, bot_line);
      i_ppal(im, 0, im->xsize, boty, top_line);
      ++topy;
      --boty;
    }
    myfree(bot_line);
    myfree(top_line);
  }
  else {
#code im->bits == i_8_bits
    IM_COLOR *top_line = mymalloc(im->xsize * sizeof(IM_COLOR));
    IM_COLOR *bot_line = mymalloc(im->xsize * sizeof(IM_COLOR));
    while (topy < boty) {
      IM_GLIN(im, 0, im->xsize, topy, top_line);
      IM_GLIN(im, 0, im->xsize, boty, bot_line);
      IM_PLIN(im, 0, im->xsize, topy, bot_line);
      IM_PLIN(im, 0, im->xsize, boty, top_line);
      ++topy;
      --boty;
    }
    myfree(top_line);
    myfree(bot_line);
#/code
  }
}

static void
flip_hv(i_img *im) {
  i_img_dim topy = 0;
  i_img_dim boty = im->ysize - 1;
  if (im->type == i_palette_type) {
    i_palidx *top_line = mymalloc(im->xsize * sizeof(i_palidx));
    i_palidx *bot_line = mymalloc(im->xsize * sizeof(i_palidx));
    while (topy < boty) {
      i_gpal(im, 0, im->xsize, topy, top_line);
      i_gpal(im, 0, im->xsize, boty, bot_line);
      flip_row_pal(top_line, im->xsize);
      flip_row_pal(bot_line, im->xsize);
      i_ppal(im, 0, im->xsize, topy, bot_line);
      i_ppal(im, 0, im->xsize, boty, top_line);
      ++topy;
      --boty;
    }
    myfree(bot_line);
    myfree(top_line);
  }
  else {
#code im->bits == i_8_bits
    IM_COLOR *top_line = mymalloc(im->xsize * sizeof(IM_COLOR));
    IM_COLOR *bot_line = mymalloc(im->xsize * sizeof(IM_COLOR));
    while (topy < boty) {
      IM_GLIN(im, 0, im->xsize, topy, top_line);
      IM_GLIN(im, 0, im->xsize, boty, bot_line);
      IM_SUFFIX(flip_row)(top_line, im->xsize);
      IM_SUFFIX(flip_row)(bot_line, im->xsize);
      IM_PLIN(im, 0, im->xsize, topy, bot_line);
      IM_PLIN(im, 0, im->xsize, boty, top_line);
      ++topy;
      --boty;
    }
    if (topy == boty) {
      IM_GLIN(im, 0, im->xsize, topy, top_line);
      IM_SUFFIX(flip_row)(top_line, im->xsize);
      IM_PLIN(im, 0, im->xsize, topy, top_line);
    }
    myfree(top_line);
    myfree(bot_line);
#/code
  }
}
Something went wrong with that request. Please try again.