xgray_tcl.cpp

Go to the documentation of this file.
00001 /* Additional functions  for the Tcl interface */
00002 #include "xgray_tcl.hpp"
00003 #include <iostream> 
00004 
00005 void getimage (const bild16& bmp, Tk_PhotoHandle vhandle) {
00006   Tk_PhotoImageBlock pb;
00007   pb.pixelPtr=new unsigned char [512*512];
00008   pb.width=512;
00009   pb.height=512;
00010   pb.pixelSize=1;
00011   pb.pitch=512;
00012   pb.offset[0]=0; // red, green and blue all point to the same value
00013   pb.offset[1]=0; 
00014   pb.offset[2]=0;
00015   pb.offset[3]=0; // Alpha should not been considered
00016   MAP (bmp) {
00017     int addr=(y*512+x);
00018     pb.pixelPtr[addr]=bmp(y,x)/256; // simple linear transform
00019  }   
00020 
00021  Tk_PhotoPutBlock(vhandle, &pb, 0, 0, 512, 512, TK_PHOTO_COMPOSITE_SET);
00022  delete[] pb.pixelPtr;
00023 }
00024 
00025 void getimage_contrast (const bild16& bmp, Tk_PhotoHandle vhandle, int low, int high) THROWSPEC
00026 {
00027   if (low==high) STHROW("Error: Zero range (getimage_contrast), low==high=="<<low);
00028   
00029   Tk_PhotoImageBlock pb;
00030   pb.pixelPtr=new unsigned char [512*512];
00031   pb.width=512;
00032   pb.height=512;
00033   pb.pixelSize=1;
00034   pb.pitch=512;
00035   pb.offset[0]=0; // red, green and blue all point to the same value
00036   pb.offset[1]=0; 
00037   pb.offset[2]=0;
00038   pb.offset[3]=0; // Alpha should not been considered
00039   
00040   MAP (bmp) {
00041     int addr=(y*512+x);
00042     int val=int(255*double(bmp(y,x)-low)/double(high-low));
00043     if (val<0) val=0;
00044     if (val>255) val=255;
00045     pb.pixelPtr[addr]=val; // simple linear transform
00046  }   
00047 
00048  Tk_PhotoPutBlock(vhandle, &pb, 0, 0, 512, 512, TK_PHOTO_COMPOSITE_SET);
00049  delete[] pb.pixelPtr;
00050 }
00051 
00052 int /*low*/ getimage_range(const bild16& bmp, Tk_PhotoHandle vhandle, int x1, int y1, int x2, int y2, int *high) THROWSPEC
00053 {
00054   int low=getminmax(bmp, x1,y1,x2,y2,high);
00055 
00056   if (low==*high) {
00057     getimage_contrast(bmp, vhandle, 0,65535);
00058   } else {
00059     getimage_contrast(bmp, vhandle, low, *high);
00060   }
00061   return low;
00062 }  
00063 
00064 int /*low*/ getminmax(const bild16& bmp, int x1, int y1, int x2, int y2, int *high) THROWSPEC
00065 {
00066   if ((x1<0) || (y1<0) || (x2<0) || (y2<0) ||
00067       (x1>=bmp.SIZEX) || (x2>=bmp.SIZEX) ||
00068       (y1>=bmp.SIZEX) || (y2>=bmp.SIZEX)) {
00069          STHROW("Index error: getminmax x1="<<x1<<" x2="<<x2<<" y1="<<y1<<" y2="<<y2);
00070   }      
00071   if (x1>x2) std::swap(x1,x2);
00072   if (y1>y2) std::swap(y1,y2);
00073 
00074   int low=bmp(y1,x1);
00075   *high=low;
00076 
00077   for (int y=y1; y<=y2; y++) 
00078     for (int x=x1; x<=x2; x++) {
00079       int val=bmp(y,x);
00080       if (val<low) low=val;
00081       if (val>*high) *high=val;
00082   }
00083   return low;
00084 }  
00085 
00086 
00087 
00088 /* Variante für bildfloats. Geht nur mit Angabe des Bereichs */
00089         
00090 void getimage_contrast (const bildfloat& bmp, Tk_PhotoHandle vhandle, float low, float high) THROWSPEC
00091 {
00092   if (low==high) STHROW("Error: Zero range (getimage_contrast), low==high=="<<low);
00093   Tk_PhotoImageBlock pb;
00094   pb.pixelPtr=new unsigned char [512*512];
00095   pb.width=512;
00096   pb.height=512;
00097   pb.pixelSize=1;
00098   pb.pitch=512;
00099   pb.offset[0]=0; // red, green and blue all point to the same value
00100   pb.offset[1]=0; 
00101   pb.offset[2]=0;
00102   pb.offset[3]=0; // Alpha should not been considered
00103   
00104   MAP (bmp) {
00105     int addr=(y*512+x);
00106     int val=int(255*double(bmp(y,x)-low)/double(high-low));
00107     if (val<0) val=0;
00108     if (val>255) val=255;
00109     pb.pixelPtr[addr]=val; // simple linear transform
00110  }   
00111 
00112  Tk_PhotoPutBlock(vhandle, &pb, 0, 0, 512, 512, TK_PHOTO_COMPOSITE_SET);
00113  delete[] pb.pixelPtr;
00114 }
00115 
00116 void getimage (const bildfloat& bmp, Tk_PhotoHandle vhandle) {
00117    getimage_contrast(bmp, vhandle, 0.0, 1.0);
00118 }   
00119 
00120 
00121 float /*low*/ getimage_range (const bildfloat& bmp, Tk_PhotoHandle vhandle, int x1, int y1, int x2, int y2, float *high) THROWSPEC
00122 {
00123  float low=getminmax(bmp,x1,y1,x2,y2,high);
00124  
00125  if (low==*high) {
00126     getimage_contrast(bmp, vhandle, 0.0,1.0);
00127   } else {
00128     getimage_contrast(bmp, vhandle, low, *high);
00129   }
00130   return low;
00131 }  
00132 
00133 float /*low*/ getminmax (const bildfloat& bmp, int x1, int y1, int x2, int y2, float *high) THROWSPEC
00134 {
00135   if ((x1<0) || (y1<0) || (x2<0) || (y2<0) ||
00136       (x1>=bmp.SIZEX) || (x2>=bmp.SIZEX) ||
00137       (y1>=bmp.SIZEX) || (y2>=bmp.SIZEX)) {
00138          STHROW("Index error: getminmax x1="<<x1<<" x2="<<x2<<" y1="<<y1<<" y2="<<y2);
00139   }      
00140   if (x1>x2) std::swap(x1,x2);
00141   if (y1>y2) std::swap(y1,y2);
00142 
00143   float low=bmp(y1,x1);
00144   *high=low;
00145 
00146   for (int y=y1; y<=y2; y++) 
00147     for (int x=x1; x<=x2; x++) {
00148       float val=bmp(y,x);
00149       if (val<low) low=val;
00150       if (val>*high) *high=val;
00151   }
00152   return low;
00153 }  
00154 
00155        
00156 bild16 *cimg=NULL;
00157 bildfloat *cimg_f=NULL;
00158 bildcomplex *cimg_c=NULL;
00159 

Generated on Fri Jul 24 12:49:17 2009 for Xgrayimg Library by  doxygen 1.5.5