34 char error_status[64];
36 fits_open_file(&fptr, filename, READONLY, &status);
43 fits_movabs_hdu(fptr, 1, IMAGE_HDU, &status);
50 long naxes[3] = { 1, 1, 1 };
51 fits_get_img_param(fptr, 3, &bpp, &dims, naxes, &status);
57 int dim, nelements = 1;
58 for(dim = 0; dim < dims; dim++) {
59 nelements *= naxes[dim];
61 void *array = malloc(((
size_t)abs(bpp) * nelements / 8));
66 fits_read_img(fptr, TBYTE, 1, (
long)nelements, NULL, array, &anynul, &status);
71 fits_read_img(fptr, TUSHORT, 1, (
long)nelements, NULL, array, &anynul, &status);
72 if((
size_t)abs(bpp) > 8*
sizeof(
dsp_t))
77 fits_read_img(fptr, TUSHORT, 1, (
long)nelements, NULL, array, &anynul, &status);
78 if((
size_t)abs(bpp) > 8*
sizeof(
dsp_t))
83 fits_read_img(fptr, TULONG, 1, (
long)nelements, NULL, array, &anynul, &status);
84 if((
size_t)abs(bpp) > 8*
sizeof(
dsp_t))
89 fits_read_img(fptr, TULONG, 1, (
long)nelements, NULL, array, &anynul, &status);
90 if((
size_t)abs(bpp) > 8*
sizeof(
dsp_t))
95 fits_read_img(fptr, TLONGLONG, 1, (
long)nelements, NULL, array, &anynul, &status);
96 if((
size_t)abs(bpp) > 8*
sizeof(
dsp_t))
101 fits_read_img(fptr, TFLOAT, 1, (
long)nelements, NULL, array, &anynul, &status);
102 if((
size_t)abs(bpp) > 8*
sizeof(
dsp_t))
107 fits_read_img(fptr, TDOUBLE, 1, (
long)nelements, NULL, array, &anynul, &status);
108 if((
size_t)abs(bpp) > 8*
sizeof(
dsp_t))
117 ffgkey(fptr,
"XBAYROFF", value, comment, &status);
123 ffgkey(fptr,
"YBAYROFF", value, comment, &status);
127 red |= atoi(value)<<1;
130 fits_close_file(fptr, &status);
136 int sizes[2] = { naxes[0], naxes[1] };
142 *channels = naxes[2];
149 for (x = 0; x < *channels; x++) {
158 fits_get_errstatus(status, error_status);
159 perr(
"FITS Error: %s\n", error_status);
270 int img_type = USHORT_IMG;
271 int byte_type = TUSHORT;
272 char bit_depth[64] =
"16 bits per sample";
273 void* buf = malloc((
size_t)(tmp->
len * components * (
size_t)abs(bpp) / 8 + 512));
275 int naxis = tmp->
dims + 1;
276 long *naxes = (
long*)malloc(
sizeof(
long) * (size_t)(tmp->
dims + 1));
277 long nelements = tmp->
len * components;
278 char error_status[64];
280 for (i = 0; i < tmp->
dims; i++)
281 naxes[i] = tmp->
sizes[i];
282 naxes[i] = components;
283 dsp_t max = (1<<(size_t)abs(bpp))/2-1;
284 for(x = 0; x < components; x++) {
293 strcpy(bit_depth,
"8 bits unsigned integer per sample");
298 img_type = USHORT_IMG;
300 strcpy(bit_depth,
"16 bits unsigned integer per sample");
305 img_type = ULONG_IMG;
307 strcpy(bit_depth,
"32 bits unsigned integer per sample");
311 byte_type = TLONGLONG;
312 img_type = LONGLONG_IMG;
314 strcpy(bit_depth,
"64 bits unsigned integer per sample");
319 img_type = FLOAT_IMG;
321 strcpy(bit_depth,
"32 bits floating point per sample");
326 img_type = DOUBLE_IMG;
328 strcpy(bit_depth,
"64 bits floating point per sample");
332 perr(
"Unsupported bits per sample value %d", bpp);
341 fits_create_file(&fptr, filename, &status);
348 fits_create_img(fptr, img_type, naxis, naxes, &status);
355 fits_write_img(fptr, byte_type, 1, nelements, buf, &status);
362 fits_close_file(fptr, &status);
366 fits_get_errstatus(status, error_status);
367 perr(
"FITS Error: %s\n", error_status);
379 int img_type = USHORT_IMG;
380 int byte_type = TUSHORT;
381 char bit_depth[64] =
"16 bits per sample";
383 void* data = malloc((
size_t)(len * (
size_t)abs(bpp) / 8 + 512));
385 int naxis = tmp->
dims;
386 long *naxes = (
long*)malloc(
sizeof(
long) * (size_t)(tmp->
dims));
387 long nelements = tmp->
len;
388 char error_status[64];
390 for (i = 0; i < tmp->
dims; i++)
391 naxes[i] = tmp->
sizes[i];
395 for(x = 0; x < components; x++) {
402 dsp_buffer_copy(buf, ((
unsigned char*)((
unsigned char*)data)), stream[components]->len);
403 strcpy(bit_depth,
"8 bits unsigned integer per sample");
408 img_type = USHORT_IMG;
409 dsp_buffer_copy(buf, ((
unsigned short*)((
unsigned short*)data)), stream[components]->len);
410 strcpy(bit_depth,
"16 bits unsigned integer per sample");
415 img_type = ULONG_IMG;
416 dsp_buffer_copy(buf, ((
unsigned int*)((
unsigned int*)data)), stream[components]->len);
417 strcpy(bit_depth,
"32 bits unsigned integer per sample");
421 byte_type = TLONGLONG;
422 img_type = LONGLONG_IMG;
423 dsp_buffer_copy(buf, ((
unsigned long*)((
unsigned long*)data)), stream[components]->len);
424 strcpy(bit_depth,
"64 bits unsigned integer per sample");
429 img_type = FLOAT_IMG;
430 dsp_buffer_copy(buf, ((
float*)((
float*)data)), stream[components]->len);
431 strcpy(bit_depth,
"32 bits floating point per sample");
436 img_type = DOUBLE_IMG;
437 dsp_buffer_copy(buf, ((
double*)((
double*)data)), stream[components]->len);
438 strcpy(bit_depth,
"64 bits floating point per sample");
442 perr(
"Unsupported bits per sample value %d", bpp);
450 fits_create_file(&fptr, filename, &status);
457 fits_create_img(fptr, img_type, naxis, naxes, &status);
465 int redy = (red>>1)&1;
467 fits_write_key(fptr, TINT,
"XBAYROFF", &redx,
"X Bayer Offset", &status);
473 fits_write_key(fptr, TINT,
"YBAYROFF", &redy,
"Y Bayer Offset", &status);
480 fits_write_key(fptr, TSTRING,
"BAYERPAT",
"RGGB",
"Y Bayer Offset", &status);
483 fits_write_key(fptr, TSTRING,
"BAYERPAT",
"GRGB",
"Y Bayer Offset", &status);
486 fits_write_key(fptr, TSTRING,
"BAYERPAT",
"GBRG",
"Y Bayer Offset", &status);
489 fits_write_key(fptr, TSTRING,
"BAYERPAT",
"BGGR",
"Y Bayer Offset", &status);
497 fits_write_img(fptr, byte_type, 1, nelements, data, &status);
504 fits_close_file(fptr, &status);
508 fits_get_errstatus(status, error_status);
509 perr(
"FITS Error: %s\n", error_status);
520 struct jpeg_decompress_struct info;
521 struct jpeg_error_mgr err;
523 info.err = jpeg_std_error(& err);
524 jpeg_create_decompress(& info);
526 FILE *jpeg = fopen (filename,
"r");
530 jpeg_stdio_src(&info, jpeg);
531 jpeg_read_header(&info,
TRUE);
532 info.dct_method = JDCT_FLOAT;
533 jpeg_start_decompress(&info);
534 width = (int)info.output_width;
535 height = (int)info.output_height;
536 components = (int)info.num_components;
538 int row_stride = (components * width);
539 buf = (
unsigned char *)malloc((
size_t)(width * height * components));
540 unsigned char *image = buf;
542 for (row = 0; row < height; row++)
544 jpeg_read_scanlines(&info, &image, 1);
547 jpeg_finish_decompress(&info);
548 *channels = components;
554 int width = stream->
sizes[0];
555 int height = stream->
sizes[1];
556 int components = (stream->
red>=0) ? 3 : 1;
557 void *buf = malloc((
size_t)(stream->
len*(stream->
red>=0?3:1)));
558 unsigned char *image = (
unsigned char *)buf;
566 struct jpeg_compress_struct cinfo;
567 struct jpeg_error_mgr jerr;
570 cinfo.err = jpeg_std_error(&jerr);
571 jpeg_create_compress(&cinfo);
572 if ((outfile = fopen(filename,
"wb")) == NULL) {
573 perr(
"can't open %s\n", filename);
576 jpeg_stdio_dest(&cinfo, outfile);
577 cinfo.image_width = (
unsigned int)width;
578 cinfo.image_height = (
unsigned int)height;
579 cinfo.in_color_space = (components == 1 ? JCS_GRAYSCALE : JCS_RGB);
580 cinfo.input_components = components;
581 jpeg_set_defaults(&cinfo);
582 cinfo.dct_method = JDCT_FLOAT;
583 cinfo.optimize_coding =
TRUE;
584 cinfo.restart_in_rows = 1;
586 jpeg_set_quality(&cinfo, quality,
TRUE);
587 jpeg_start_compress(&cinfo,
TRUE);
588 row_stride = width * (stream->
red>=0?3:1);
591 for (row = 0; row < height; row++)
593 jpeg_write_scanlines(&cinfo, &image, 1);
598 jpeg_finish_compress(&cinfo);
600 jpeg_destroy_compress(&cinfo);
606 unsigned int row_stride;
607 int width = stream[components]->
sizes[0];
608 int height = stream[components]->
sizes[1];
609 void *buf = malloc((
size_t)(stream[components]->len*components));
610 unsigned char *image = (
unsigned char *)buf;
613 struct jpeg_compress_struct cinfo;
614 struct jpeg_error_mgr jerr;
616 cinfo.err = jpeg_std_error(&jerr);
617 if ((outfile = fopen(filename,
"wb")) == NULL) {
618 perr(
"can't open %s\n", filename);
621 jpeg_create_compress(&cinfo);
622 jpeg_stdio_dest(&cinfo, outfile);
623 cinfo.image_width = (
unsigned int)width;
624 cinfo.image_height = (
unsigned int)height;
625 cinfo.in_color_space = (components == 1 ? JCS_GRAYSCALE : JCS_RGB);
626 cinfo.input_components = components;
627 cinfo.dct_method = JDCT_FLOAT;
628 cinfo.optimize_coding =
TRUE;
629 cinfo.restart_in_rows = 1;
630 jpeg_set_defaults(&cinfo);
632 jpeg_set_quality(&cinfo, quality,
TRUE);
633 jpeg_start_compress(&cinfo,
TRUE);
634 row_stride = (
unsigned int)width * (
unsigned int)components;
636 for (row = 0; row < height; row++)
638 jpeg_write_scanlines(&cinfo, &image, 1);
642 jpeg_finish_compress(&cinfo);
643 jpeg_destroy_compress(&cinfo);
651 dsp_t *rawpt, *scanpt;
657 size = width * height;
659 for (i = 0; i < size; i++)
661 if ((i / width) % 2 == 0)
666 if ((i > width) && ((i % width) > 0))
669 (*(rawpt - width - 1) + *(rawpt - width + 1) + *(rawpt + width - 1) + *(rawpt + width + 1)) /
671 val += (*(rawpt - 1) + *(rawpt + 1) + *(rawpt + width) + *(rawpt - width)) / 4;
676 val = *(rawpt + width + 1);
677 val += (*(rawpt + 1) + *(rawpt + width)) / 2;
684 if ((i > width) && ((i % width) < (width - 1)))
686 val = (*(rawpt + width) + *(rawpt - width)) / 2;
688 val += (*(rawpt - 1) + *(rawpt + 1)) / 2;
692 val = *(rawpt + width);
703 if ((i < (width * (height - 1))) && ((i % width) > 0))
705 val = (*(rawpt - 1) + *(rawpt + 1)) / 2;
707 val += (*(rawpt + width) + *(rawpt - width)) / 2;
714 val += *(rawpt - width);
720 if (i < (width * (height - 1)) && ((i % width) < (width - 1)))
723 val += (*(rawpt - 1) + *(rawpt + 1) + *(rawpt - width) + *(rawpt + width)) / 4;
725 (*(rawpt - width - 1) + *(rawpt - width + 1) + *(rawpt + width - 1) + *(rawpt + width + 1)) /
732 val += (*(rawpt - 1) + *(rawpt - width)) / 2;
733 val += *(rawpt - width - 1);
747 dsp_t *rawpt, *red, *green, *blue;
754 size = width * height;
755 for (i = 0; i < size; i++)
757 if ((i / width) % 2 == ((r&2)>>1))
759 if ((i % 2) == (r&1))
762 if ((i > width) && ((i % width) > 0))
764 rawpt[i - width - 1] += red[i];
765 rawpt[i - width + 1] += red[i];
766 rawpt[i + width - 1] += red[i];
767 rawpt[i + width + 1] += red[i];
768 rawpt[i - 1] += green[i];
769 rawpt[i + 1] += green[i];
770 rawpt[i + width] += green[i];
771 rawpt[i - width] += green[i];
776 rawpt[i + width + 1] += red[i];
777 rawpt[i + 1] += green[i];
778 rawpt[i + width] += green[i];
785 if ((i > width) && ((i % width) < (width - 1)))
787 rawpt[i + width] += red[i];
788 rawpt[i - width] += red[i];
789 rawpt[i] += green[i];
790 rawpt[i - 1] += blue[i];
791 rawpt[i + 1] += blue[i];
795 rawpt[i + width] += red[i];
796 rawpt[i] += green[i];
797 rawpt[i - 1] += blue[i];
803 if ((i % 2) == (r&1))
806 if ((i < (width * (height - 1))) && ((i % width) > 0))
808 rawpt[i - 1] += red[i];
809 rawpt[i + 1] += red[i];
810 rawpt[i] += green[i];
811 rawpt[i + width] += blue[i];
812 rawpt[i - width] += blue[i];
816 rawpt[i + 1] += red[i];
817 rawpt[i] += green[i];
818 rawpt[i - width] += blue[i];
823 if (i < (width * (height - 1)) && ((i % width) < (width - 1)))
826 rawpt[i - 1] += green[i];
827 rawpt[i + 1] += green[i];
828 rawpt[i - width] += green[i];
829 rawpt[i + width] += green[i];
830 rawpt[i - width - 1] += blue[i];
831 rawpt[i - width + 1] += blue[i];
832 rawpt[i + width + 1] += blue[i];
833 rawpt[i + width + 1] += blue[i];
839 rawpt[i - 1] += green[i];
840 rawpt[i - width] += green[i];
841 rawpt[i - width - 1] += blue[i];
853 dsp_t *rawpt, *red, *green, *blue;
859 green = &dst[width*height];
860 red = &dst[width*height*2];
861 size = width * height;
862 for (i = 0; i < size; i++)
864 if ((i / width) % 2 == ((r&2)>>1))
866 if ((i % 2) == (r&1))
869 if ((i > width) && ((i % width) > 0))
872 (*(rawpt - width - 1) + *(rawpt - width + 1) + *(rawpt + width - 1) + *(rawpt + width + 1)) /
874 *green++ = (*(rawpt - 1) + *(rawpt + 1) + *(rawpt + width) + *(rawpt - width)) / 4;
879 *red++ = *(rawpt + width + 1);
880 *green++ = (*(rawpt + 1) + *(rawpt + width)) / 2;
887 if ((i > width) && ((i % width) < (width - 1)))
889 *red++ = (*(rawpt + width) + *(rawpt - width)) / 2;
891 *blue++ = (*(rawpt - 1) + *(rawpt + 1)) / 2;
895 *red++ = *(rawpt + width);
897 *blue++ = *(rawpt - 1);
903 if ((i % 2) == (r&1))
906 if ((i < (width * (height - 1))) && ((i % width) > 0))
908 *red++ = (*(rawpt - 1) + *(rawpt + 1)) / 2;
910 *blue++ = (*(rawpt + width) + *(rawpt - width)) / 2;
915 *red++ = *(rawpt + 1);
917 *blue++ = *(rawpt - width);
923 if (i < (width * (height - 1)) && ((i % width) < (width - 1)))
926 *green++ = (*(rawpt - 1) + *(rawpt + 1) + *(rawpt - width) + *(rawpt + width)) / 4;
928 (*(rawpt - width - 1) + *(rawpt - width + 1) + *(rawpt + width - 1) + *(rawpt + width + 1)) /
935 *green++ = (*(rawpt - 1) + *(rawpt - width)) / 2;
936 *blue++ = *(rawpt - width - 1);
949 dsp_t *rawpt, *scanpt;
955 size = width * height;
957 for (i = 0; i < size; i++)
959 if ((i / width) % 2 == ((red&2)>>1))
961 if ((i % 2) == (red&1))
964 if ((i > width) && ((i % width) > 0))
967 (*(rawpt - width - 1) + *(rawpt - width + 1) + *(rawpt + width - 1) + *(rawpt + width + 1)) /
969 *scanpt++ = (*(rawpt - 1) + *(rawpt + 1) + *(rawpt + width) + *(rawpt - width)) / 4;
974 *scanpt++ = *(rawpt + width + 1);
975 *scanpt++ = (*(rawpt + 1) + *(rawpt + width)) / 2;
982 if ((i > width) && ((i % width) < (width - 1)))
984 *scanpt++ = (*(rawpt + width) + *(rawpt - width)) / 2;
986 *scanpt++ = (*(rawpt - 1) + *(rawpt + 1)) / 2;
990 *scanpt++ = *(rawpt + width);
992 *scanpt++ = *(rawpt - 1);
998 if ((i % 2) == (red&1))
1001 if ((i < (width * (height - 1))) && ((i % width) > 0))
1003 *scanpt++ = (*(rawpt - 1) + *(rawpt + 1)) / 2;
1005 *scanpt++ = (*(rawpt + width) + *(rawpt - width)) / 2;
1010 *scanpt++ = *(rawpt + 1);
1012 *scanpt++ = *(rawpt - width);
1018 if (i < (width * (height - 1)) && ((i % width) < (width - 1)))
1021 *scanpt++ = (*(rawpt - 1) + *(rawpt + 1) + *(rawpt - width) + *(rawpt + width)) / 4;
1023 (*(rawpt - width - 1) + *(rawpt - width + 1) + *(rawpt + width - 1) + *(rawpt + width + 1)) /
1030 *scanpt++ = (*(rawpt - 1) + *(rawpt - width)) / 2;
1031 *scanpt++ = *(rawpt - width - 1);