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 463 lines (403 sloc) 9.446 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 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 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
#include "imager.h"
#include "imageri.h"

/*
=item i_copyto(C<dest>, C<src>, C<x1>, C<y1>, C<x2>, C<y2>, C<tx>, C<ty>)

=category Image

Copies image data from the area (C<x1>,C<y1>)-[C<x2>,C<y2>] in the
source image to a rectangle the same size with it's top-left corner at
(C<tx>,C<ty>) in the destination image.

If C<x1> > C<x2> or C<y1> > C<y2> then the corresponding co-ordinates
are swapped.

=cut
*/

void
i_copyto(i_img *im, i_img *src, i_img_dim x1, i_img_dim y1, i_img_dim x2, i_img_dim y2, i_img_dim tx, i_img_dim ty) {
  i_img_dim y, t, ttx, tty;
  
  if (x2<x1) { t=x1; x1=x2; x2=t; }
  if (y2<y1) { t=y1; y1=y2; y2=t; }
  if (tx < 0) {
    /* adjust everything equally */
    x1 += -tx;
    x2 += -tx;
    tx = 0;
  }
  if (ty < 0) {
    y1 += -ty;
    y2 += -ty;
    ty = 0;
  }
  if (x1 >= src->xsize || y1 >= src->ysize)
    return; /* nothing to do */
  if (x2 > src->xsize)
    x2 = src->xsize;
  if (y2 > src->ysize)
    y2 = src->ysize;
  if (x1 == x2 || y1 == y2)
    return; /* nothing to do */

  mm_log((1,"i_copyto(im* %p, src %p, p1(" i_DFp "), p2(" i_DFp "), t("
i_DFp "))\n",
im, src, i_DFcp(x1, y1), i_DFcp(x2, y2), i_DFcp(tx, ty)));

#code im->bits == i_8_bits
  IM_COLOR *row = mymalloc(sizeof(IM_COLOR) * (x2-x1));
  tty = ty;
  for(y=y1; y<y2; y++) {
    ttx = tx;
    IM_GLIN(src, x1, x2, y, row);
    if (src->channels != im->channels)
      IM_ADAPT_COLORS(im->channels, src->channels, row, x2-x1);
    IM_PLIN(im, tx, tx+x2-x1, tty, row);
    tty++;
  }
  myfree(row);
#/code
}

#code
void
#ifdef IM_EIGHT_BIT
i_adapt_colors
#else
i_adapt_fcolors
#endif
(int out_channels, int in_channels, IM_COLOR *colors,
size_t count) {
  if (out_channels == in_channels)
    return;
  if (count == 0)
    return;

  switch (out_channels) {
  case 1:
    {
      switch (in_channels) {
      case 2:
/* apply alpha against a black background */
while (count) {
colors->channel[0] = colors->channel[0] * colors->channel[1] / IM_SAMPLE_MAX;
++colors;
--count;
}
return;

      case 3:
/* convert to grey */
while (count) {
colors->channel[0] = IM_ROUND(color_to_grey(colors));
++colors;
--count;
}
return;

      case 4:
while (count) {
colors->channel[0] = IM_ROUND(color_to_grey(colors) * colors->channel[3] / IM_SAMPLE_MAX);
++colors;
--count;
}
return;

      default:
i_fatal(3, "i_adapt_colors: in_channels of %d invalid\n", in_channels);
return; /* avoid warnings */
      }
    }

  case 2:
    {
      switch (in_channels) {
      case 1:
while (count) {
colors->channel[1] = IM_SAMPLE_MAX;
++colors;
--count;
}
return;

      case 3:
while (count) {
colors->channel[0] = IM_ROUND(color_to_grey(colors));
colors->channel[1] = IM_SAMPLE_MAX;
++colors;
--count;
}
return;

      case 4:
while (count) {
colors->channel[0] = IM_ROUND(color_to_grey(colors));
colors->channel[1] = colors->channel[3];
++colors;
--count;
}
return;

      default:
i_fatal(3, "i_adapt_colors: in_channels of %d invalid\n", in_channels);
return; /* avoid warnings */
      }
    }

  case 3:
    {
      switch (in_channels) {
      case 1:
while (count) {
colors->channel[1] = colors->channel[2] = colors->channel[0];
++colors;
--count;
}
return;

      case 2:
while (count) {
int alpha = colors->channel[1];
colors->channel[0] = colors->channel[1] = colors->channel[2] =
IM_ROUND(colors->channel[0] * alpha / IM_SAMPLE_MAX);
++colors;
--count;
}
return;

      case 4:
while (count) {
int alpha = colors->channel[3];
colors->channel[0] =
IM_ROUND(colors->channel[0] * alpha / IM_SAMPLE_MAX);
colors->channel[1] =
IM_ROUND(colors->channel[1] * alpha / IM_SAMPLE_MAX);
colors->channel[2] =
IM_ROUND(colors->channel[2] * alpha / IM_SAMPLE_MAX);
++colors;
--count;
}
return;

      default:
i_fatal(3, "i_adapt_colors: in_channels of %d invalid\n", in_channels);
return; /* avoid warnings */
      }
    }

  case 4:
    {
      switch (in_channels) {
      case 1:
while (count) {
colors->channel[1] = colors->channel[2] = colors->channel[0];
colors->channel[3] = IM_SAMPLE_MAX;
++colors;
--count;
}
return;

      case 2:
while (count) {
colors->channel[3] = colors->channel[1];
colors->channel[1] = colors->channel[2] = colors->channel[0];
++colors;
--count;
}
return;

      case 3:
while (count) {
colors->channel[3] = IM_SAMPLE_MAX;
++colors;
--count;
}
return;

      default:
i_fatal(3, "i_adapt_colors: in_channels of %d invalid\n", in_channels);
return; /* avoid warnings */
      }
    }

  default:
    i_fatal(3, "i_adapt_colors: out_channels of %d invalid\n", out_channels);
    return; /* avoid warnings */
  }
}

void
#ifdef IM_EIGHT_BIT
i_adapt_colors_bg
#else
i_adapt_fcolors_bg
#endif
(int out_channels, int in_channels, IM_COLOR *colors,
size_t count, IM_COLOR const *bg) {
  if (out_channels == in_channels)
    return;
  if (count == 0)
    return;

  switch (out_channels) {
  case 2:
  case 4:
    IM_ADAPT_COLORS(out_channels, in_channels, colors, count);
    return;

  case 1:
    switch (in_channels) {
    case 3:
      IM_ADAPT_COLORS(out_channels, in_channels, colors, count);
      return;

    case 2:
      {
/* apply alpha against our given background */
IM_WORK_T grey_bg = IM_ROUND(color_to_grey(bg));
while (count) {
colors->channel[0] =
(colors->channel[0] * colors->channel[1] +
grey_bg * (IM_SAMPLE_MAX - colors->channel[1])) / IM_SAMPLE_MAX;
++colors;
--count;
}
      }
      break;

    case 4:
      {
IM_WORK_T grey_bg = IM_ROUND(color_to_grey(bg));
while (count) {
IM_WORK_T src_grey = IM_ROUND(color_to_grey(colors));
colors->channel[0] =
(src_grey * colors->channel[3]
+ grey_bg * (IM_SAMPLE_MAX - colors->channel[3])) / IM_SAMPLE_MAX;
++colors;
--count;
}
      }
      break;
    }
    break;
      
  case 3:
    switch (in_channels) {
    case 1:
      IM_ADAPT_COLORS(out_channels, in_channels, colors, count);
      return;

    case 2:
      {
while (count) {
int ch;
IM_WORK_T src_grey = colors->channel[0];
IM_WORK_T src_alpha = colors->channel[1];
for (ch = 0; ch < 3; ++ch) {
colors->channel[ch] =
(src_grey * src_alpha
+ bg->channel[ch] * (IM_SAMPLE_MAX - src_alpha))
/ IM_SAMPLE_MAX;
}
++colors;
--count;
}
      }
      break;

    case 4:
      {
while (count) {
int ch;
IM_WORK_T src_alpha = colors->channel[3];
for (ch = 0; ch < 3; ++ch) {
colors->channel[ch] =
(colors->channel[ch] * src_alpha
+ bg->channel[ch] * (IM_SAMPLE_MAX - src_alpha))
/ IM_SAMPLE_MAX;
}
++colors;
--count;
}
      }
      break;
    }
    break;
  }
}

/*
=item i_gsamp_bg(im, l, r, y, samples, out_channels, background)

=category Drawing

Like C<i_gsampf()> but applies the source image color over a supplied
background color.

This is intended for output to image formats that don't support alpha
channels.

=cut

=item i_gsampf_bg(im, l, r, y, samples, out_channels, background)

=category Drawing

Like C<i_gsampf()> but applies the source image color over a supplied
background color.

This is intended for output to image formats that don't support alpha
channels.

=cut
*/
int
#ifdef IM_EIGHT_BIT
i_gsamp_bg
#else
i_gsampf_bg
#endif
(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, IM_SAMPLE_T *samples,
 int out_channels, IM_COLOR const *bg) {
  if (out_channels == im->channels)
    return IM_GSAMP(im, l, r, y, samples, NULL, im->channels);
  
  switch (out_channels) {
  case 1:
    switch (im->channels) {
    case 2:
      {
i_img_dim x;
IM_SAMPLE_T *inp = samples, *outp = samples;
IM_WORK_T grey_bg = IM_ROUND(color_to_grey(bg));
i_img_dim count;

count = IM_GSAMP(im, l, r, y, samples, NULL, im->channels);
if (!count)
return 0;

for (x = l; x < r; ++x) {
*outp++ = ( inp[0] * inp[1] +
grey_bg * (IM_SAMPLE_MAX - inp[1])) / IM_SAMPLE_MAX;
inp += 2;
}

return count;
      }
      break;

    default:
      i_fatal(0, "i_gsamp_bg() can only remove alpha channels");
      break;
    }
    break;
  case 3:
    switch (im->channels) {
    case 1:
      {
int channels[3] = { 0, 0, 0 };
return IM_GSAMP(im, l, r, y, samples, channels, out_channels);
      }
    case 2:
      {
i_img_dim x;
int ch;
IM_SAMPLE_T *inp = samples, *outp = samples;
i_img_dim count;
int channels[4] = { 0, 0, 0, 1 };

count = IM_GSAMP(im, l, r, y, samples, channels, im->channels);
if (!count)
return 0;

for (x = l; x < r; ++x) {
IM_WORK_T alpha = inp[3];
for (ch = 0; ch < 3; ++ch) {
*outp++ = ( *inp++ * alpha +
bg->channel[ch] * (IM_SAMPLE_MAX - alpha)) / IM_SAMPLE_MAX;
}
++inp;
}

return count;
      }

    case 4:
      {
i_img_dim x;
int ch;
IM_SAMPLE_T *inp = samples, *outp = samples;
i_img_dim count;

count = IM_GSAMP(im, l, r, y, samples, NULL, im->channels);
if (!count)
return 0;

for (x = l; x < r; ++x) {
IM_WORK_T alpha = inp[3];
for (ch = 0; ch < 3; ++ch) {
*outp++ = ( *inp++ * alpha +
bg->channel[ch] * (IM_SAMPLE_MAX - alpha)) / IM_SAMPLE_MAX;
}
++inp;
}

return count;
      }
      break;
    default:
      i_fatal(0, "i_gsamp_bg() can only remove alpha channels");
      break;
    }
    break;

  default:
    i_fatal(0, "i_gsamp_bg() can only remove alpha channels");
  }

  return 0;
}

#/code
Something went wrong with that request. Please try again.