summaryrefslogtreecommitdiffstats
path: root/fitsy++/photo.C
diff options
context:
space:
mode:
authorWilliam Joye <wjoye@cfa.harvard.edu>2020-03-21 20:29:02 (GMT)
committerWilliam Joye <wjoye@cfa.harvard.edu>2020-03-21 20:29:02 (GMT)
commita004d413772c16e43004929efb3df2f36c53314c (patch)
treef635a49cb52ebff944062e7dd6d8833c14a0b40b /fitsy++/photo.C
parentc8abfb35e0303f03f75d9fe74dac893b1767924c (diff)
downloadblt-a004d413772c16e43004929efb3df2f36c53314c.zip
blt-a004d413772c16e43004929efb3df2f36c53314c.tar.gz
blt-a004d413772c16e43004929efb3df2f36c53314c.tar.bz2
libfitsy++
Diffstat (limited to 'fitsy++/photo.C')
-rw-r--r--fitsy++/photo.C154
1 files changed, 154 insertions, 0 deletions
diff --git a/fitsy++/photo.C b/fitsy++/photo.C
new file mode 100644
index 0000000..4c62939
--- /dev/null
+++ b/fitsy++/photo.C
@@ -0,0 +1,154 @@
+// Copyright (C) 1999-2018
+// Smithsonian Astrophysical Observatory, Cambridge, MA, USA
+// For conditions of distribution and use, see copyright notice in "copyright"
+
+#include <iostream>
+#include <sstream>
+#include <iomanip>
+using namespace std;
+
+#include <tk.h>
+
+#include "photo.h"
+#include "util.h"
+
+FitsPhoto::FitsPhoto(Tcl_Interp* interp, const char* ph)
+{
+ // reset
+ valid_ = 0;
+
+ if (*ph == '\0') {
+ Tcl_AppendResult(interp, "bad image name ", NULL);
+ return;
+ }
+ Tk_PhotoHandle photo = Tk_FindPhoto(interp, ph);
+ if (!photo) {
+ Tcl_AppendResult(interp, "bad image handle ", NULL);
+ return;
+ }
+ Tk_PhotoImageBlock block;
+ if (!Tk_PhotoGetImage(photo,&block)) {
+ Tcl_AppendResult(interp, "bad image block ", NULL);
+ return;
+ }
+ int width =0;
+ int height =0;
+ Tk_PhotoGetSize(photo, &width, &height);
+
+ // new header
+ head_ = new FitsHead(width, height, 1, 8);
+ if (!head_->isValid())
+ return;
+
+ size_t size = (size_t)width*height;
+ unsigned char* dest = new unsigned char[size];
+ data_ = dest;
+
+ dataSize_ = size;
+ dataSkip_ = 0;
+
+ unsigned char* src = block.pixelPtr;
+ for (int jj=height-1; jj>=0; jj--)
+ for (int ii=0; ii<width; ii++) {
+ int pp = (jj*width+ii)*block.pixelSize;
+ unsigned char rr = src[pp+block.offset[0]];
+ unsigned char gg = src[pp+block.offset[1]];
+ unsigned char bb = src[pp+block.offset[2]];
+ //unsigned char vv = (rr+gg+bb)/3.;
+ //unsigned char vv = .2126*rr + .7152*gg + .0722*bb;
+ // must round
+ unsigned char vv = (.299*rr + .587*gg + .114*bb)+.5;
+ *dest++ = vv;
+ }
+
+ // made it this far, must be valid
+ byteswap_ = 0;
+ endian_ = lsb() ? LITTLE : BIG;
+ valid_ = 1;
+}
+
+FitsPhoto::~FitsPhoto()
+{
+ if (data_)
+ delete [] (unsigned char*)data_;
+}
+
+FitsPhotoCube::FitsPhotoCube(Tcl_Interp* interp, const char* ph)
+{
+ // reset
+ valid_ = 0;
+
+ if (*ph == '\0') {
+ Tcl_AppendResult(interp, "bad image name ", NULL);
+ return;
+ }
+ Tk_PhotoHandle photo = Tk_FindPhoto(interp, ph);
+ if (!photo) {
+ Tcl_AppendResult(interp, "bad image handle ", NULL);
+ return;
+ }
+ Tk_PhotoImageBlock block;
+ if (!Tk_PhotoGetImage(photo,&block)) {
+ Tcl_AppendResult(interp, "bad image block ", NULL);
+ return;
+ }
+ int width =0;
+ int height =0;
+ int depth =3;
+ Tk_PhotoGetSize(photo, &width, &height);
+
+ // new header
+ head_ = new FitsHead(width, height, depth, 8);
+ if (!head_->isValid())
+ return;
+
+ size_t size = (size_t)width*height*depth;
+ unsigned char* dest = new unsigned char[size];
+ data_ = dest;
+
+ dataSize_ = size;
+ dataSkip_ = 0;
+
+ unsigned char* src = block.pixelPtr;
+ for (int kk=0; kk<depth; kk++) {
+ for (int jj=height-1; jj>=0; jj--) {
+ for (int ii=0; ii<width; ii++) {
+ int pp = (jj*width+ii)*block.pixelSize;
+ *dest++ = src[pp+block.offset[kk]];
+ }
+ }
+ }
+
+ // made it this far, must be valid
+ byteswap_ = 0;
+ endian_ = lsb() ? LITTLE : BIG;
+ valid_ = 1;
+}
+
+FitsPhotoCube::~FitsPhotoCube()
+{
+ if (data_)
+ delete [] (unsigned char*)data_;
+}
+
+FitsPhotoCubeNext::FitsPhotoCubeNext(FitsFile* prev)
+{
+ primary_ = prev->primary();
+ managePrimary_ = 0;
+
+ head_ = prev->head();
+ manageHead_ = 0;
+ FitsImageHDU* hdu = (FitsImageHDU*)head_->hdu();
+
+ data_ = (char*)prev->data() + hdu->imgbytes();
+ dataSize_ = 0;
+ dataSkip_ = 0;
+
+ ext_ = prev->ext();
+ inherit_ = head_->inherit();
+ byteswap_ = prev->byteswap();
+ endian_ = prev->endian();
+ valid_ = 1;
+
+ return;
+}