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 415 lines (355 sloc) 10.043 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
/*
=head1 NAME

imgdouble.c - implements double per sample images

=head1 SYNOPSIS

i_img *im = i_img_double_new(int x, int y, int channels);
# use like a normal image

=head1 DESCRIPTION

Implements double/sample images.

This basic implementation is required so that we have some larger
sample image type to work with.

=over

=cut
*/

#include "imager.h"
#include "imageri.h"

static int i_ppix_ddoub(i_img *im, int x, int y, const i_color *val);
static int i_gpix_ddoub(i_img *im, int x, int y, i_color *val);
static int i_glin_ddoub(i_img *im, int l, int r, int y, i_color *vals);
static int i_plin_ddoub(i_img *im, int l, int r, int y, const i_color *vals);
static int i_ppixf_ddoub(i_img *im, int x, int y, const i_fcolor *val);
static int i_gpixf_ddoub(i_img *im, int x, int y, i_fcolor *val);
static int i_glinf_ddoub(i_img *im, int l, int r, int y, i_fcolor *vals);
static int i_plinf_ddoub(i_img *im, int l, int r, int y, const i_fcolor *vals);
static int i_gsamp_ddoub(i_img *im, int l, int r, int y, i_sample_t *samps,
                       int const *chans, int chan_count);
static int i_gsampf_ddoub(i_img *im, int l, int r, int y, i_fsample_t *samps,
                        int const *chans, int chan_count);

/*
=item IIM_base_16bit_direct

Base structure used to initialize a 16-bit/sample image.

Internal.

=cut
*/
static i_img IIM_base_double_direct =
{
  0, /* channels set */
  0, 0, 0, /* xsize, ysize, bytes */
  ~0U, /* ch_mask */
  i_double_bits, /* bits */
  i_direct_type, /* type */
  0, /* virtual */
  NULL, /* idata */
  { 0, 0, NULL }, /* tags */
  NULL, /* ext_data */

  i_ppix_ddoub, /* i_f_ppix */
  i_ppixf_ddoub, /* i_f_ppixf */
  i_plin_ddoub, /* i_f_plin */
  i_plinf_ddoub, /* i_f_plinf */
  i_gpix_ddoub, /* i_f_gpix */
  i_gpixf_ddoub, /* i_f_gpixf */
  i_glin_ddoub, /* i_f_glin */
  i_glinf_ddoub, /* i_f_glinf */
  i_gsamp_ddoub, /* i_f_gsamp */
  i_gsampf_ddoub, /* i_f_gsampf */

  NULL, /* i_f_gpal */
  NULL, /* i_f_ppal */
  NULL, /* i_f_addcolors */
  NULL, /* i_f_getcolors */
  NULL, /* i_f_colorcount */
  NULL, /* i_f_maxcolors */
  NULL, /* i_f_findcolor */
  NULL, /* i_f_setcolors */

  NULL, /* i_f_destroy */

  i_gsamp_bits_fb,
  NULL, /* i_f_psamp_bits */
};

/*
=item i_img_double_new(int x, int y, int ch)
=category Image creation/destruction
=synopsis i_img *img = i_img_double_new(width, height, channels);

Creates a new double per sample image.

=cut
*/
i_img *i_img_double_new(int x, int y, int ch) {
  int bytes;
  i_img *im;

  mm_log((1,"i_img_double_new(x %d, y %d, ch %d)\n", x, y, ch));

  if (x < 1 || y < 1) {
    i_push_error(0, "Image sizes must be positive");
    return NULL;
  }
  if (ch < 1 || ch > MAXCHANNELS) {
    i_push_errorf(0, "channels must be between 1 and %d", MAXCHANNELS);
    return NULL;
  }
  bytes = x * y * ch * sizeof(double);
  if (bytes / y / ch / sizeof(double) != x) {
    i_push_errorf(0, "integer overflow calculating image allocation");
    return NULL;
  }
  
  im = i_img_alloc();
  *im = IIM_base_double_direct;
  i_tags_new(&im->tags);
  im->xsize = x;
  im->ysize = y;
  im->channels = ch;
  im->bytes = bytes;
  im->ext_data = NULL;
  im->idata = mymalloc(im->bytes);
  memset(im->idata, 0, im->bytes);
  i_img_init(im);
  
  return im;
}

static int i_ppix_ddoub(i_img *im, int x, int y, const i_color *val) {
  int off, ch;

  if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize)
    return -1;

  off = (x + y * im->xsize) * im->channels;
  if (I_ALL_CHANNELS_WRITABLE(im)) {
    for (ch = 0; ch < im->channels; ++ch)
      ((double*)im->idata)[off+ch] = Sample8ToF(val->channel[ch]);
  }
  else {
    for (ch = 0; ch < im->channels; ++ch)
      if (im->ch_mask & (1<<ch))
((double*)im->idata)[off+ch] = Sample8ToF(val->channel[ch]);
  }

  return 0;
}

static int i_gpix_ddoub(i_img *im, int x, int y, i_color *val) {
  int off, ch;

  if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize)
    return -1;

  off = (x + y * im->xsize) * im->channels;
  for (ch = 0; ch < im->channels; ++ch)
    val->channel[ch] = SampleFTo8(((double *)im->idata)[off+ch]);

  return 0;
}

static int i_ppixf_ddoub(i_img *im, int x, int y, const i_fcolor *val) {
  int off, ch;

  if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize)
    return -1;

  off = (x + y * im->xsize) * im->channels;
  if (I_ALL_CHANNELS_WRITABLE(im)) {
    for (ch = 0; ch < im->channels; ++ch)
      ((double *)im->idata)[off+ch] = val->channel[ch];
  }
  else {
    for (ch = 0; ch < im->channels; ++ch)
      if (im->ch_mask & (1 << ch))
((double *)im->idata)[off+ch] = val->channel[ch];
  }

  return 0;
}

static int i_gpixf_ddoub(i_img *im, int x, int y, i_fcolor *val) {
  int off, ch;

  if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize)
    return -1;

  off = (x + y * im->xsize) * im->channels;
  for (ch = 0; ch < im->channels; ++ch)
    val->channel[ch] = ((double *)im->idata)[off+ch];

  return 0;
}

static int i_glin_ddoub(i_img *im, int l, int r, int y, i_color *vals) {
  int ch, count, i;
  int off;
  if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
    if (r > im->xsize)
      r = im->xsize;
    off = (l+y*im->xsize) * im->channels;
    count = r - l;
    for (i = 0; i < count; ++i) {
      for (ch = 0; ch < im->channels; ++ch) {
vals[i].channel[ch] = SampleFTo8(((double *)im->idata)[off]);
        ++off;
      }
    }
    return count;
  }
  else {
    return 0;
  }
}

static int i_plin_ddoub(i_img *im, int l, int r, int y, const i_color *vals) {
  int ch, count, i;
  int off;
  if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
    if (r > im->xsize)
      r = im->xsize;
    off = (l+y*im->xsize) * im->channels;
    count = r - l;
    if (I_ALL_CHANNELS_WRITABLE(im)) {
      for (i = 0; i < count; ++i) {
for (ch = 0; ch < im->channels; ++ch) {
((double *)im->idata)[off] = Sample8ToF(vals[i].channel[ch]);
++off;
}
      }
    }
    else {
      for (i = 0; i < count; ++i) {
for (ch = 0; ch < im->channels; ++ch) {
if (im->ch_mask & (1 << ch))
((double *)im->idata)[off] = Sample8ToF(vals[i].channel[ch]);
++off;
}
      }
    }
    return count;
  }
  else {
    return 0;
  }
}

static int i_glinf_ddoub(i_img *im, int l, int r, int y, i_fcolor *vals) {
  int ch, count, i;
  int off;
  if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
    if (r > im->xsize)
      r = im->xsize;
    off = (l+y*im->xsize) * im->channels;
    count = r - l;
    for (i = 0; i < count; ++i) {
      for (ch = 0; ch < im->channels; ++ch) {
vals[i].channel[ch] = ((double *)im->idata)[off];
        ++off;
      }
    }
    return count;
  }
  else {
    return 0;
  }
}

static int i_plinf_ddoub(i_img *im, int l, int r, int y, const i_fcolor *vals) {
  int ch, count, i;
  int off;
  if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
    if (r > im->xsize)
      r = im->xsize;
    off = (l+y*im->xsize) * im->channels;
    count = r - l;
    if (I_ALL_CHANNELS_WRITABLE(im)) {
      for (i = 0; i < count; ++i) {
for (ch = 0; ch < im->channels; ++ch) {
((double *)im->idata)[off] = vals[i].channel[ch];
++off;
}
      }
    }
    else {
      for (i = 0; i < count; ++i) {
for (ch = 0; ch < im->channels; ++ch) {
if (im->ch_mask & (1 << ch))
((double *)im->idata)[off] = vals[i].channel[ch];
++off;
}
      }
    }
    return count;
  }
  else {
    return 0;
  }
}

static int i_gsamp_ddoub(i_img *im, int l, int r, int y, i_sample_t *samps,
                       int const *chans, int chan_count) {
  int ch, count, i, w;
  int off;

  if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
    if (r > im->xsize)
      r = im->xsize;
    off = (l+y*im->xsize) * im->channels;
    w = r - l;
    count = 0;

    if (chans) {
      /* make sure we have good channel numbers */
      for (ch = 0; ch < chan_count; ++ch) {
        if (chans[ch] < 0 || chans[ch] >= im->channels) {
          i_push_errorf(0, "No channel %d in this image", chans[ch]);
          return 0;
        }
      }
      for (i = 0; i < w; ++i) {
        for (ch = 0; ch < chan_count; ++ch) {
          *samps++ = SampleFTo8(((double *)im->idata)[off+chans[ch]]);
          ++count;
        }
        off += im->channels;
      }
    }
    else {
      if (chan_count <= 0 || chan_count > im->channels) {
i_push_errorf(0, "chan_count %d out of range, must be >0, <= channels",
chan_count);
return 0;
      }
      for (i = 0; i < w; ++i) {
        for (ch = 0; ch < chan_count; ++ch) {
          *samps++ = SampleFTo8(((double *)im->idata)[off+ch]);
          ++count;
        }
        off += im->channels;
      }
    }

    return count;
  }
  else {
    return 0;
  }
}

static int i_gsampf_ddoub(i_img *im, int l, int r, int y, i_fsample_t *samps,
                        int const *chans, int chan_count) {
  int ch, count, i, w;
  int off;

  if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
    if (r > im->xsize)
      r = im->xsize;
    off = (l+y*im->xsize) * im->channels;
    w = r - l;
    count = 0;

    if (chans) {
      /* make sure we have good channel numbers */
      for (ch = 0; ch < chan_count; ++ch) {
        if (chans[ch] < 0 || chans[ch] >= im->channels) {
          i_push_errorf(0, "No channel %d in this image", chans[ch]);
          return 0;
        }
      }
      for (i = 0; i < w; ++i) {
        for (ch = 0; ch < chan_count; ++ch) {
          *samps++ = ((double *)im->idata)[off+chans[ch]];
          ++count;
        }
        off += im->channels;
      }
    }
    else {
      if (chan_count <= 0 || chan_count > im->channels) {
i_push_errorf(0, "chan_count %d out of range, must be >0, <= channels",
chan_count);
return 0;
      }
      for (i = 0; i < w; ++i) {
        for (ch = 0; ch < chan_count; ++ch) {
          *samps++ = ((double *)im->idata)[off+ch];
          ++count;
        }
        off += im->channels;
      }
    }

    return count;
  }
  else {
    return 0;
  }
}


/*
=back

=head1 AUTHOR

Tony Cook <tony@develop-help.com>

=head1 SEE ALSO

Imager(3)

=cut
*/
Something went wrong with that request. Please try again.