• Skip to content
  • Skip to link menu
KDE 3.5 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

kstars

ccdpreviewwg.cpp

Go to the documentation of this file.
00001 /*  CCD Preview
00002     Copyright (C) 2005 Dirk Huenniger <hunniger@cip.physik.uni-bonn.de>
00003 
00004     Adapted from streamwg by Jasem Mutlaq
00005 
00006     This application is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU General Public
00008     License as published by the Free Software Foundation; either
00009     version 2 of the License, or (at your option) any later version.
00010     
00011  */
00012  
00013 #include "ccdpreviewwg.h"
00014 #include "indistd.h"
00015 #include "indidriver.h"
00016 #include "indimenu.h"
00017 #include "Options.h"
00018 
00019 #include <kmessagebox.h>
00020 #include <klocale.h>
00021 #include <kdebug.h>
00022 #include <kpushbutton.h>
00023 #include <kiconloader.h>
00024 #include <ktempfile.h>
00025 #include <kio/netaccess.h>
00026 #include <kfiledialog.h>
00027 #include <kcombobox.h>
00028 #include <kurl.h>
00029 #include <klineedit.h>
00030 
00031 #include <qsocketnotifier.h>
00032 #include <qimage.h>
00033 #include <qpainter.h>
00034 #include <qstringlist.h>
00035 #include <qdir.h>
00036 #include <qlayout.h>
00037 #include <qlabel.h>
00038 
00039 
00040 #include <stdlib.h>
00041 #include <unistd.h>
00042 #include <fcntl.h>
00043 #include <math.h>
00044 
00045 
00046 #define STREAMBUFSIZ        1024
00047 
00048 #include "ccdpreviewwg.moc"
00049 
00050 FILE *CCDwfp;
00051 
00052  CCDPreviewWG::CCDPreviewWG(INDIStdDevice *inStdDev, QWidget * parent, const char * name) : CCDPreviewForm(parent, name)
00053  {
00054  
00055    stdDev         = inStdDev;
00056 
00057    fwhm       = -1;
00058    mu         = -1;
00059    streamWidth    = streamHeight = -1;
00060    processStream  = colorFrame = false;
00061    streamFrame      = new CCDVideoWG(videoFrame);
00062    streamFrame->bytesPerPixel= 1;
00063    streamFrame->PixelOrder= 1;   
00064    gammaChanged(gammaBar->value());
00065    brightnessChanged(brightnessBar->value());
00066    contrastChanged(contrastBar->value());    
00067      
00068   KIconLoader *icons = KGlobal::iconLoader();
00069   
00070   playPix    = icons->loadIcon( "player_play", KIcon::Toolbar );
00071   pausePix   = icons->loadIcon( "player_pause", KIcon::Toolbar );
00072   capturePix = icons->loadIcon( "frame_image", KIcon::Toolbar );
00073   
00074   playB->setPixmap(pausePix);   
00075   captureB->setPixmap(capturePix);
00076   
00077   imgFormatCombo->insertStrList(QImage::outputFormats());
00078   
00079   connect(playB, SIGNAL(clicked()), this, SLOT(playPressed()));
00080   connect(captureB, SIGNAL(clicked()), this, SLOT(captureImage()));
00081   connect(brightnessBar, SIGNAL(valueChanged(int)), this, SLOT(brightnessChanged(int)));
00082   connect(contrastBar, SIGNAL(valueChanged(int)), this, SLOT(contrastChanged(int)));
00083   connect(gammaBar, SIGNAL(valueChanged(int)), this, SLOT(gammaChanged(int)));
00084   connect(focalEdit, SIGNAL(returnPressed()), this, SLOT(updateFWHM()));
00085  }
00086  
00087 CCDPreviewWG::~CCDPreviewWG()
00088 {
00089 
00090 }
00091 
00092 void CCDPreviewWG::closeEvent ( QCloseEvent * e )
00093 {
00094   stdDev->streamDisabled();
00095   processStream = false;
00096   e->accept();
00097 }
00098 
00099 void CCDPreviewWG::setColorFrame(bool color)
00100 {
00101   colorFrame = color;
00102 }
00103 
00104 /*void CCDPreviewWG::establishDataChannel(QString host, int port)
00105 {
00106         QString errMsg;
00107     struct sockaddr_in pin;
00108     struct hostent *serverHostName = gethostbyname(host.ascii());
00109     errMsg = QString("Connection to INDI host at %1 on port %2 failed.").arg(host).arg(port);
00110     
00111     memset(&pin, 0, sizeof(pin));
00112     pin.sin_family      = AF_INET;
00113     pin.sin_addr.s_addr     = ((struct in_addr *) (serverHostName->h_addr))->s_addr;
00114     pin.sin_port        = htons(port);
00115 
00116     if ( (streamFD = socket(AF_INET, SOCK_STREAM, 0)) == -1)
00117     {
00118      KMessageBox::error(0, i18n("Cannot create socket."));
00119      return;
00120     }
00121 
00122     if ( ::connect(streamFD, (struct sockaddr*) &pin, sizeof(pin)) == -1)
00123     {
00124       KMessageBox::error(0, errMsg);
00125       streamFD = -1;
00126       return;
00127     }
00128 
00129     // callback notified
00130     sNotifier = new QSocketNotifier( streamFD, QSocketNotifier::Read, this);
00131         QObject::connect( sNotifier, SIGNAL(activated(int)), this, SLOT(streamReceived()));
00132 }*/
00133 
00134 void CCDPreviewWG::enableStream(bool enable)
00135 {
00136   if (enable)
00137   {
00138     processStream = true;
00139     show();
00140   }
00141   else
00142   {
00143     processStream = false;
00144     playB->setPixmap(pausePix);
00145     hide();
00146   }
00147   
00148 }
00149 
00150 void CCDPreviewWG::setCtrl(int wd, int ht,int po, int bpp,unsigned long mgd)
00151 {
00152   long i;
00153   streamWidth  = wd;
00154   streamHeight = ht;
00155   streamFrame->totalBaseCount = wd * ht * bpp;
00156  // fprintf(stderr,"%d %d %d",wd,ht,bpp)
00157   streamFrame->Width = wd;
00158   streamFrame->Height = ht; 
00159   streamFrame->bytesPerPixel=bpp;
00160   streamFrame->PixelOrder=po;
00161   streamFrame->maxGoodData=mgd; 
00162   if (streamFrame->streamBuffer!=NULL) {
00163     free(streamFrame->streamBuffer);      
00164   }
00165   streamFrame->streamBufferPos=0;
00166   streamFrame->streamBuffer=(unsigned char*) 
00167     malloc(sizeof(unsigned char)*streamFrame->totalBaseCount);
00168   for (i=0;i<streamFrame->totalBaseCount;i++) {
00169     streamFrame->streamBuffer[i]=0;
00170   }
00171   resize(wd + layout()->margin() * 2 , ht + playB->height() + brightnessLabel->height()
00172       + contrastLabel->height() + gammaLabel->height() + focalEdit->height() + FWHMLabel->height() + layout()->margin() * 2 + layout()->spacing()*6);  
00173   streamFrame->resize(wd, ht);
00174 }
00175 
00176 
00177 void CCDPreviewWG::setCCDInfo(double in_fwhm, int in_mu)
00178 {
00179   fwhm = in_fwhm;
00180   mu = in_mu;
00181 
00182   updateFWHM();
00183 
00184 }
00185 
00186 void CCDPreviewWG::updateFWHM()
00187 {
00188   double focal_length(-1), fwhm_arcsec;
00189 
00190   focal_length = focalEdit->text().toDouble();
00191 
00192   if (focal_length <= 0 || fwhm <= 0 || mu <= 0)
00193   {
00194     FWHMLabel->setText("--");
00195     return;
00196   }
00197 
00198   fwhm_arcsec = (206.26 / focal_length) * fwhm * mu;
00199 
00200   FWHMLabel->setText(QString("%1").arg(fwhm_arcsec, 0, 'g', 3));
00201   
00202 }
00203 
00204 
00205 void CCDPreviewWG::resizeEvent(QResizeEvent *ev)
00206 {
00207   streamFrame->resize(ev->size().width() - layout()->margin() * 2, ev->size().height() - playB->height() - layout()->margin() * 2 - layout()->spacing());
00208 }
00209  
00210 void CCDPreviewWG::playPressed()
00211 {
00212 
00213  if (processStream)
00214  {
00215   playB->setPixmap(playPix);    
00216   processStream = false;
00217  }
00218  else
00219  {
00220   playB->setPixmap(pausePix);   
00221   processStream = true;
00222  }
00223  
00224 }
00225 
00226 void CCDPreviewWG::captureImage()
00227 {
00228   QString fname;
00229   QString fmt;
00230   KURL currentFileURL;
00231   QString currentDir = Options::fitsSaveDirectory();
00232   KTempFile tmpfile;
00233   tmpfile.setAutoDelete(true);
00234 
00235   fmt = imgFormatCombo->currentText();
00236 
00237   currentFileURL = KFileDialog::getSaveURL( currentDir, fmt );
00238   
00239   if (currentFileURL.isEmpty()) return;
00240 
00241   if ( currentFileURL.isValid() )
00242   {
00243     currentDir = currentFileURL.directory();
00244 
00245     if ( currentFileURL.isLocalFile() )
00246        fname = currentFileURL.path();
00247     else
00248        fname = tmpfile.name();
00249 
00250     if (fname.right(fmt.length()).lower() != fmt.lower()) 
00251     {
00252       fname += ".";
00253       fname += fmt.lower();
00254     }
00255       
00256     streamFrame->qPix.save(fname, fmt.ascii());
00257     
00258     //set rwx for owner, rx for group, rx for other
00259     chmod( fname.ascii(), S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH );
00260 
00261     if ( tmpfile.name() == fname )
00262     { //need to upload to remote location
00263     
00264       if ( ! KIO::NetAccess::upload( tmpfile.name(), currentFileURL, (QWidget*) 0 ) )
00265       {
00266         QString message = i18n( "Could not upload image to remote location: %1" ).arg( currentFileURL.prettyURL() );
00267         KMessageBox::sorry( 0, message, i18n( "Could not upload file" ) );
00268       }
00269     }
00270   }
00271   else
00272   {
00273         QString message = i18n( "Invalid URL: %1" ).arg( currentFileURL.url() );
00274         KMessageBox::sorry( 0, message, i18n( "Invalid URL" ) );
00275   }
00276 
00277 }
00278 
00279 
00280 CCDVideoWG::CCDVideoWG(QWidget * parent, const char * name) : QFrame(parent, name, Qt::WNoAutoErase)
00281 {
00282   streamImage    = NULL;
00283   streamBuffer    = NULL;
00284   displayBuffer    = NULL;
00285   grayTable=new QRgb[256];
00286   for (int i=0;i<256;i++) {
00287         grayTable[i]=qRgb(i,i,i);
00288   }
00289   grayTable[255]=qRgb(255,0,0);
00290 }
00291       
00292 CCDVideoWG::~CCDVideoWG() 
00293 {
00294   delete (streamImage);
00295   if (displayBuffer!=NULL) {
00296     free(displayBuffer);      
00297   }
00298   if (streamBuffer!=NULL) {
00299     free(streamBuffer);   
00300   }
00301 
00302   delete [] (grayTable);
00303 }
00304 
00305 void CCDVideoWG::newFrame(unsigned char *buffer, int buffSize, int w, int h)
00306 {
00307   long i,offs,d;
00308   offs=0;   
00309   Width=w;
00310   Height=h;
00311   d=2*bytesPerPixel;
00312   if (streamBuffer==NULL) {
00313     return;
00314   }
00315   if (streamBufferPos>=totalBaseCount) {
00316     streamBufferPos=0;
00317   }
00318   for (i=streamBufferPos;((i<streamBufferPos+buffSize)&&(i<totalBaseCount));i++) {
00319     if (PixelOrder==PIXELORDER_NORMAL) {
00320       streamBuffer[i]=buffer[i-streamBufferPos];
00321     }
00322     if (PixelOrder==PIXELORDER_DUAL) {
00323       if (i%d==0) {
00324           offs=i/2;
00325       }
00326       if ((i%d)<bytesPerPixel) {
00327     streamBuffer[i-offs]=buffer[i-streamBufferPos];
00328       }
00329       else {
00330         streamBuffer[Width*Height*bytesPerPixel-(i-offs)]=buffer[i-streamBufferPos];
00331       }
00332     }
00333   }
00334   streamBufferPos=i;
00335   /*if (buffSize > totalBaseCount)
00336      streamImage = new QImage(buffer, w, h, 32, 0, 0, QImage::BigEndian);
00337    else
00338     streamImage = new QImage(streamBuffer, w, h, 8, grayTable, 256, QImage::IgnoreEndian);
00339    update();
00340   */
00341   redrawVideoWG();
00342 }
00343 
00344 void CCDVideoWG::redrawVideoWG(void)
00345 {
00346   int x,y,b;
00347   double val;
00348   unsigned long dat;
00349   if (displayBuffer!=NULL) {
00350     displayBuffer=(unsigned char*) 
00351       realloc(displayBuffer, sizeof(unsigned char)*Width*Height);
00352   }
00353   else {
00354   displayBuffer=(unsigned char*) 
00355       malloc(sizeof(unsigned char)*Width*Height);
00356   } 
00357   if (displayBuffer==NULL) {
00358      return;
00359   }
00360   if (streamBuffer==NULL) {
00361     return;
00362   }
00363   for (x=0;x<Width;x++) {
00364     for (y=0;y<Height;y++) {
00365       dat=0;
00366       for (b=0;b<bytesPerPixel;b++) {
00367         dat=(unsigned long) (dat+ streamBuffer[Width*y*bytesPerPixel+x*bytesPerPixel+b]*pow(256.0,b));
00368       }
00369       if (dat<=maxGoodData) {
00370         val=128+scale*(dat-offset)/(pow(256.0,bytesPerPixel)-1.0);
00371     if (val<0.0) {
00372         val=0.0;
00373     }
00374         val=pow(val/255.0,1.0/gamma)*255.0;
00375         if (val>255.0) {
00376           val=255.0; 
00377     }
00378     val=qRound(val*(254.0/255.0));
00379     displayBuffer[Width*y+x]=(int) val;
00380      }  
00381      else {
00382         displayBuffer[Width*y+x]=255;
00383       } 
00384     }
00385   }  
00386   streamImage = new QImage(displayBuffer, Width, Height, 8, grayTable, 256, QImage::IgnoreEndian);
00387   update();
00388 }
00389 
00390 
00391 void CCDVideoWG::paintEvent(QPaintEvent */*ev*/)
00392 {
00393     
00394    if (streamImage)
00395    {
00396     if (streamImage->isNull()) return;
00397     //qPix = kPixIO.convertToPixmap(*streamImage);/*streamImage->smoothScale(width(), height()));*/
00398     qPix = kPixIO.convertToPixmap(streamImage->scale(width(), height()));
00399     delete (streamImage);
00400     streamImage = NULL;
00401    }
00402    
00403    bitBlt(this, 0, 0, &qPix);
00404    
00405 }
00406 
00407 
00408 void CCDPreviewWG::brightnessChanged(int value)
00409 {
00410     streamFrame->offset=pow(pow(256.0,streamFrame->bytesPerPixel),1.0-(value/200.0))-1.0;
00411     //fprintf(stderr,"offs=%lf\n",streamFrame->offset);
00412     streamFrame->redrawVideoWG();   
00413 }
00414 
00415 void CCDPreviewWG::contrastChanged(int value)
00416 {
00417     streamFrame->scale=pow(pow(256.0,streamFrame->bytesPerPixel+1),value/200.0)-1.0;
00418     //fprintf(stderr,"scale=%lf\n",streamFrame->scale);
00419     streamFrame->redrawVideoWG();
00420 }
00421 
00422 void CCDPreviewWG::gammaChanged(int value)
00423 {
00424     streamFrame->gamma=3.0*(value/200.0);
00425     //fprintf(stderr,"gamma=%lf\n",streamFrame->gamma);
00426     streamFrame->redrawVideoWG();   
00427 }

kstars

Skip menu "kstars"
  • Main Page
  • Modules
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

API Reference

Skip menu "API Reference"
  • keduca
  • kstars
Generated for API Reference by doxygen 1.5.9
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal