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 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
|
/*** File wcslib/imio.c
*** By Doug Mink, Harvard-Smithsonian Center for Astrophysics
*** June 13, 1996
* Module: imio.c (image pixel manipulation)
* Purpose: Read and write pixels from arbitrary data type 2D arrays
* Subroutine: getpix (image, bitpix, w, h, x, y)
* Get pixel from 2D image of any numeric type
* Subroutine: putpix (image, bitpix, w, h, x, y, dpix)
* Copy pixel into 2D image of any numeric type
* Subroutine: getvec (image, bitpix, pix1, pixoff, dpix)
* Get vector from 2D image of any numeric type
* Subroutine: putvec (image, bitpix, pix1, pixoff, dpix)
* Copy pixel vector into 2D image of any numeric type
* Subroutine: movepix (image1, bitpix, w1, x1, y1, image2, w2, x2, y2)
* Copy pixel from one image location to another
* Subroutine: imswap (bitpix,string,nbytes)
* Swap bytes in string in place, with FITS bits/pixel code
* Subroutine: imswap2 (string,nbytes)
* Swap bytes in string in place
* Subroutine imswap4 (string,nbytes)
* Reverse bytes of Integer*4 or Real*4 vector in place
* Subroutine imswap8 (string,nbytes)
* Reverse bytes of Real*8 vector in place
* Subroutine imswapped ()
* Return 1 if PC/DEC byte order, else 0
* Copyright: 1996 Smithsonian Astrophysical Observatory
* You may do anything you like with this file except remove
* this copyright. The Smithsonian Astrophysical Observatory
* makes no representations about the suitability of this
* software for any purpose. It is provided "as is" without
* express or implied warranty.
*/
#include <stdlib.h>
#include <stdio.h>
#include "fitshead.h"
static int verbose=0; /* if 1 print diagnostics */
/* GETPIX -- Get pixel from 2D image of any numeric type */
double
getpix (image, bitpix, w, h, x, y)
char *image; /* Image array as 1-D vector */
int bitpix; /* FITS bits per pixel */
/* 16 = short, -16 = unsigned short, 32 = int */
/* -32 = float, -64 = double */
int w; /* Image width in pixels */
int h; /* Image height in pixels */
int x;
int y;
{
short *im2;
int *im4;
unsigned int *imu;
float *imr;
double *imd;
/* Return 0 if coordinates are not inside image */
if (x < 0 || x >= w)
return (0.0);
if (y < 0 || y >= h)
return (0.0);
/* Extract pixel from appropriate type of array */
switch (bitpix) {
case 16:
im2 = (short *)image;
return ((double) im2[(y*w) + x]);
case 32:
im4 = (int *)image;
return ((double) im4[(y*w) + x]);
case -16:
imu = (unsigned int *)image;
return ((double) imu[(y*w) + x]);
case -32:
imr = (float *)image;
return ((double) imr[(y*w) + x]);
case -64:
imd = (double *)image;
return (imd[(y*w) + x]);
default:
return (0.0);
}
}
/* PUTPIX -- Copy pixel into 2D image of any numeric type */
void
putpix (image, bitpix, w, h, x, y, dpix)
char *image;
int bitpix; /* Number of bits per pixel */
/* 16 = short, -16 = unsigned short, 32 = int */
/* -32 = float, -64 = double */
int w;
int h;
int x;
int y;
double dpix;
{
short *im2;
int *im4;
unsigned int *imu;
float *imr;
double *imd;
/* Return if coordinates are not inside image */
if (x < 0 || x >= w)
return;
if (y < 0 || y >= h)
return;
switch (bitpix) {
case 16:
im2 = (short *)image;
im2[(y*w) + x] = (short) dpix;
break;
case 32:
im4 = (int *)image;
im4[(y*w) + x] = (int) dpix;
break;
case -16:
imu = (unsigned int *)image;
imu[(y*w) + x] = (unsigned int) dpix;
break;
case -32:
imr = (float *)image;
imr[(y*w) + x] = (float) dpix;
break;
case -64:
imd = (double *)image;
imd[(y*w) + x] = dpix;
break;
}
return;
}
/* MOVEPIX -- Copy pixel between 2D images of same numeric type */
void
movepix (image1, bitpix1, w1, x1, y1, image2, bitpix2, w2, x2, y2)
char *image1; /* Pointer to first pixel in input image */
int bitpix1; /* Bits per input pixel (FITS codes) */
/* 16 = short, -16 = unsigned short, 32 = int */
/* -32 = float, -64 = double */
int w1; /* Number of horizontal pixels in input image */
int x1, y1; /* Row and column for input pixel */
char *image2; /* Pointer to first pixel in output image */
int bitpix2; /* Bits per output pixel (FITS codes) */
/* 16 = short, -16 = unsigned short, 32 = int */
/* -32 = float, -64 = double */
int w2; /* Number of horizontal pixels in output image */
int x2, y2; /* Row and column for output pixel */
{
short *ims1, *ims2;
int *imi1, *imi2;
unsigned int *imu1, *imu2;
float *imr1, *imr2;
double *imd1, *imd2;
switch (bitpix1) {
case 16:
switch (bitpix2) {
case 16:
ims1 = (short *)image1;
ims2 = (short *)image2;
ims2[(y2*w2) + x2] = ims1[(y1*w1) + x1];
break;
case 32:
ims1 = (short *)image1;
imi2 = (int *)image2;
imi2[(y2*w2) + x2] = (int) ims1[(y1*w1) + x1];
break;
case -16:
ims1 = (short *)image1;
imu2 = (unsigned int *)image2;
imu2[(y2*w2) + x2] = (unsigned int) ims1[(y1*w1) + x1];
break;
case -32:
ims1 = (short *)image1;
imr2 = (float *)image2;
imr2[(y2*w2) + x2] = (float) ims1[(y1*w1) + x1];
break;
case -64:
ims1 = (short *)image1;
imd2 = (double *)image2;
imd2[(y2*w2) + x2] = (double) ims1[(y1*w1) + x1];
break;
}
break;
case 32:
switch (bitpix2) {
case 16:
imi1 = (int *)image1;
ims2 = (short *)image2;
ims2[(y2*w2) + x2] = (short) imi1[(y1*w1) + x1];
break;
case 32:
imi1 = (int *)image1;
imi2 = (int *)image2;
imi2[(y2*w2) + x2] = imi1[(y1*w1) + x1];
break;
case -16:
imi1 = (int *)image1;
imu2 = (unsigned int *)image2;
imu2[(y2*w2) + x2] = (unsigned int) imi1[(y1*w1) + x1];
break;
case -32:
imi1 = (int *)image1;
imr2 = (float *)image2;
imr2[(y2*w2) + x2] = (float) imi1[(y1*w1) + x1];
break;
case -64:
imi1 = (int *)image1;
imd2 = (double *)image2;
imd2[(y2*w2) + x2] = (double) imi1[(y1*w1) + x1];
break;
}
break;
case -16:
switch (bitpix2) {
case 16:
imu1 = (unsigned int *)image1;
ims2 = (short *)image2;
ims2[(y2*w2) + x2] = (short) imu1[(y1*w1) + x1];
break;
case 32:
imu1 = (unsigned int *)image1;
imi2 = (int *)image2;
imi2[(y2*w2) + x2] = (int) imu1[(y1*w1) + x1];
break;
case -16:
imu1 = (unsigned int *)image1;
imu2 = (unsigned int *)image2;
imu2[(y2*w2) + x2] = imu1[(y1*w1) + x1];
break;
case -32:
imu1 = (unsigned int *)image1;
imr2 = (float *)image2;
imr2[(y2*w2) + x2] = (float) imu1[(y1*w1) + x1];
break;
case -64:
imu1 = (unsigned int *)image1;
imd2 = (double *)image2;
imd2[(y2*w2) + x2] = (double) imu1[(y1*w1) + x1];
break;
}
break;
case -32:
switch (bitpix2) {
case 16:
imr1 = (float *)image1;
ims2 = (short *)image2;
ims2[(y2*w2) + x2] = (short) imr1[(y1*w1) + x1];
break;
case 32:
imr1 = (float *)image1;
imi2 = (int *)image2;
imi2[(y2*w2) + x2] = (int) imr1[(y1*w1) + x1];
break;
case -16:
imr1 = (float *)image1;
imu2 = (unsigned int *)image2;
imu2[(y2*w2) + x2] = (unsigned int) imr1[(y1*w1) + x1];
break;
case -32:
imr1 = (float *)image1;
imr2 = (float *)image2;
imr2[(y2*w2) + x2] = imr1[(y1*w1) + x1];
break;
case -64:
imr1 = (float *)image1;
imd2 = (double *)image2;
imd2[(y2*w2) + x2] = (float) imr1[(y1*w1) + x1];
break;
}
break;
case -64:
switch (bitpix2) {
case 16:
imd1 = (double *)image1;
ims2 = (short *)image2;
ims2[(y2*w2) + x2] = (short) imd1[(y1*w1) + x1];
break;
case 32:
imd1 = (double *)image1;
imi2 = (int *)image2;
imi2[(y2*w2) + x2] = (int) imd1[(y1*w1) + x1];
break;
case -16:
imd1 = (double *)image1;
imu2 = (unsigned int *)image2;
imu2[(y2*w2) + x2] = (unsigned int) imd1[(y1*w1) + x1];
break;
case -32:
imd1 = (double *)image1;
imr2 = (float *)image2;
imr2[(y2*w2) + x2] = (float) imd1[(y1*w1) + x1];
break;
case -64:
imd1 = (double *)image1;
imd2 = (double *)image2;
imd2[(y2*w2) + x2] = imd1[(y1*w1) + x1];
break;
}
break;
}
return;
}
/* GETVEC -- Get vector from 2D image of any numeric type */
void
getvec (image, bitpix, pix1, pixoff, dpix)
char *image;
int bitpix; /* Number of bits per pixel */
/* 16 = short, -16 = unsigned short, 32 = int */
/* -32 = float, -64 = double */
int pix1;
int pixoff;
double *dpix;
{
short *im2;
int *im4;
unsigned int *imu;
float *imr;
double *imd;
int ipix, pix2;
pix2 = pix1 + pixoff;
switch (bitpix) {
case 16:
im2 = (short *)image;
for (ipix = pix1; ipix < pix2; ipix++)
*dpix++ = (double) *(im2+ipix);
break;
case 32:
im4 = (int *)image;
for (ipix = pix1; ipix < pix2; ipix++)
*dpix++ = (double) *(im4+ipix);
break;
case -16:
imu = (unsigned int *)image;
for (ipix = pix1; ipix < pix2; ipix++)
*dpix++ = (double) *(imu+ipix);
break;
case -32:
imr = (float *)image;
for (ipix = pix1; ipix < pix2; ipix++)
*dpix++ = (double) *(imr+ipix);
break;
case -64:
imd = (double *)image;
for (ipix = pix1; ipix < pix2; ipix++)
*dpix++ = *(imd+ipix);
break;
}
return;
}
/* PUTVEC -- Copy pixel vector into 2D image of any numeric type */
void
putvec (image, bitpix, pix1, pixoff, dpix)
char *image;
int bitpix; /* Number of bits per pixel */
/* 16 = short, -16 = unsigned short, 32 = int */
/* -32 = float, -64 = double */
int pix1;
int pixoff;
double *dpix;
{
short *im2;
int *im4;
unsigned int *imu;
float *imr;
double *imd;
int ipix, pix2;
double *dp = dpix;
pix2 = pix1 + pixoff;
switch (bitpix) {
case 16:
im2 = (short *)image;
for (ipix = pix1; ipix < pix2; ipix++)
*(im2+ipix) = (short) *dp++;
break;
case 32:
im4 = (int *)image;
for (ipix = pix1; ipix < pix2; ipix++)
*(im4+ipix) = (int) *dp++;
break;
case -16:
imu = (unsigned int *)image;
for (ipix = pix1; ipix < pix2; ipix++)
*(imu+ipix) = (unsigned int) *dp++;
break;
case -32:
imr = (float *)image;
for (ipix = pix1; ipix < pix2; ipix++)
*(imr+ipix) = (float) *dp++;
break;
case -64:
imd = (double *)image;
for (ipix = pix1; ipix < pix2; ipix++)
*(imd+ipix) = (double) *dp++;
break;
}
return;
}
/* IMSWAP -- Reverse bytes of any type of vector in place */
void
imswap (bitpix, string, nbytes)
int bitpix; /* Number of bits per pixel */
/* 16 = short, -16 = unsigned short, 32 = int */
/* -32 = float, -64 = double */
char *string; /* Address of starting point of bytes to swap */
int nbytes; /* Number of bytes to swap */
{
switch (bitpix) {
case 16:
if (nbytes < 2) return;
imswap2 (string,nbytes);
break;
case 32:
if (nbytes < 4) return;
imswap4 (string,nbytes);
break;
case -16:
if (nbytes < 2) return;
imswap2 (string,nbytes);
break;
case -32:
if (nbytes < 4) return;
imswap4 (string,nbytes);
break;
case -64:
if (nbytes < 8) return;
imswap8 (string,nbytes);
break;
}
return;
}
/* IMSWAP2 -- Swap bytes in string in place */
void
imswap2 (string,nbytes)
char *string; /* Address of starting point of bytes to swap */
int nbytes; /* Number of bytes to swap */
{
char *sbyte, temp, *slast;
slast = string + nbytes;
sbyte = string;
while (sbyte < slast) {
temp = sbyte[0];
sbyte[0] = sbyte[1];
sbyte[1] = temp;
sbyte= sbyte + 2;
}
return;
}
/* IMSWAP4 -- Reverse bytes of Integer*4 or Real*4 vector in place */
void
imswap4 (string,nbytes)
char *string; /* Address of Integer*4 or Real*4 vector */
int nbytes; /* Number of bytes to reverse */
{
char *sbyte, *slast;
char temp0, temp1, temp2, temp3;
slast = string + nbytes;
sbyte = string;
while (sbyte < slast) {
temp3 = sbyte[0];
temp2 = sbyte[1];
temp1 = sbyte[2];
temp0 = sbyte[3];
sbyte[0] = temp0;
sbyte[1] = temp1;
sbyte[2] = temp2;
sbyte[3] = temp3;
sbyte = sbyte + 4;
}
return;
}
/* IMSWAP8 -- Reverse bytes of Real*8 vector in place */
void
imswap8 (string,nbytes)
char *string; /* Address of Real*8 vector */
int nbytes; /* Number of bytes to reverse */
{
char *sbyte, *slast;
char temp[8];
slast = string + nbytes;
sbyte = string;
while (sbyte < slast) {
temp[7] = sbyte[0];
temp[6] = sbyte[1];
temp[5] = sbyte[2];
temp[4] = sbyte[3];
temp[3] = sbyte[4];
temp[2] = sbyte[5];
temp[1] = sbyte[6];
temp[0] = sbyte[7];
sbyte[0] = temp[0];
sbyte[1] = temp[1];
sbyte[2] = temp[2];
sbyte[3] = temp[3];
sbyte[4] = temp[4];
sbyte[5] = temp[5];
sbyte[6] = temp[6];
sbyte[7] = temp[7];
sbyte = sbyte + 8;
}
return;
}
/* IMSWAPPED -- Returns 0 if big-endian (Sun,Mac),
1 if little-endian(PC,Alpha) */
int
imswapped ()
{
char *ctest;
int itest;
itest = 1;
ctest = (char *)&itest;
if (*ctest)
return (1);
else
return (0);
}
/* Apr 17 1996 New file
* May 22 1996 Add H so that PUTPIX and GETPIX can check coordinates
* Jun 11 1996 Simplify NEWIMAGE subroutine
* Jun 12 1996 Add byte-swapping subroutines
*/
|