xshmimg.C

Go to the documentation of this file.
00001 /* Gxsm - Gnome X Scanning Microscopy
00002  * universal STM/AFM/SARLS/SPALEED/... controlling and
00003  * data analysis software
00004  * 
00005  * Copyright (C) 1999,2000,2001,2002,2003 Percy Zahl
00006  *
00007  * Authors: Percy Zahl <zahl@users.sf.net>
00008  * additional features: Andreas Klust <klust@users.sf.net>
00009  * WWW Home: http://gxsm.sf.net
00010  *
00011  * This program is free software; you can redistribute it and/or modify
00012  * it under the terms of the GNU General Public License as published by
00013  * the Free Software Foundation; either version 2 of the License, or
00014  * (at your option) any later version.
00015  *
00016  * This program is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU General Public License
00022  * along with this program; if not, write to the Free Software
00023  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
00024  */
00025 
00026 /* -*- Mode: C++; indent-tabs-mode: nil; c-basic-offset: 8 c-style: "K&R" -*- */
00027 
00028 #include <iostream>
00029 #include <fstream>
00030 
00031 #include <gdk-pixbuf/gdk-pixbuf.h>
00032 
00033 #include "xshmimg.h"
00034 #include "util.h"
00035 
00036 #include "glbvars.h"
00037 
00038 ShmImage2D::ShmImage2D(GtkWidget* area, 
00039                        int Width, int Height, 
00040                        int xorigin, int yorigin
00041                        ){
00042   x0 = xorigin; 
00043   y0 = yorigin;
00044   maxcol  = 0;
00045   ZoomFac = 1;
00046   imgarea = area;
00047 
00048   MkPalette();
00049 
00050   gdk_pixbuf     = NULL;
00051   gitem_image    = NULL;
00052   gitem_red_line = NULL;
00053 
00054   // Setup Red Line Position
00055   red_line_points = gnome_canvas_points_new (2);
00056   red_line_points->coords[0] = x0;
00057   red_line_points->coords[2] = x0+Width;
00058   red_line_points->coords[1] = red_line_points->coords[3] = y0;
00059 
00060   Resize(Width, Height);
00061 }
00062 
00063 void ShmImage2D::Resize(int Width, int Height){
00064   XSM_DEBUG(DBG_L3, "ShmImage2D::Resize" << Width << "x" << Height );
00065   width  = Width;
00066   height = Height;
00067 
00068   
00069   red_line_points->coords[2] = red_line_points->coords[0] + width;
00070   red_line_points->coords[1] = red_line_points->coords[3] = y0;
00071 
00072   // Setup Image
00073   GdkPixbuf *gdk_pixbufold=gdk_pixbuf;
00074 
00075   gdk_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, width, height);
00076   bpp        = gdk_pixbuf_get_bits_per_sample(gdk_pixbuf) * gdk_pixbuf_get_n_channels(gdk_pixbuf) / 8;
00077   rowstride  = gdk_pixbuf_get_rowstride(gdk_pixbuf);
00078   pixels     = gdk_pixbuf_get_pixels(gdk_pixbuf);
00079 
00080   if (!gitem_image)
00081           gitem_image = gnome_canvas_item_new (gnome_canvas_root( GNOME_CANVAS(imgarea)),
00082                                                gnome_canvas_pixbuf_get_type(),
00083                                                "pixbuf", gdk_pixbuf,
00084                                                NULL);
00085   gnome_canvas_item_lower (gitem_image, 1);
00086   if (gdk_pixbufold)
00087           gdk_pixbuf_unref (gdk_pixbufold);
00088 
00089   if (!gitem_red_line){
00090           gitem_red_line = gnome_canvas_item_new (gnome_canvas_root( GNOME_CANVAS(imgarea)),
00091                                                   GNOME_TYPE_CANVAS_LINE,
00092                                                   "points", red_line_points,
00093                                                   "fill_color","red",
00094                                                   "width_pixels",ZoomFac,
00095                                                   NULL);
00096   }else{
00097           gnome_canvas_item_set (gitem_red_line, 
00098                                  "points", red_line_points,
00099                                  "width_pixels",ZoomFac,
00100                                  NULL);
00101   }
00102 
00103   XSM_DEBUG(DBG_L3, "ShmImage2D::Resize done" );
00104 }
00105 
00106 ShmImage2D::~ShmImage2D(){
00107         gtk_object_destroy (GTK_OBJECT(gitem_image));
00108         gtk_object_destroy (GTK_OBJECT(gitem_red_line));
00109         gnome_canvas_points_free (red_line_points);
00110         gdk_pixbuf_unref (gdk_pixbuf);
00111 }
00112 
00113 
00114 void ShmImage2D::MkPalette(char *name){
00115         // Make Color Look Up Table fpr Grey-Scale -> TrueColor
00116         unsigned long r, g, b, cval;
00117   
00118         if (name){
00119                 std::ifstream cpal;
00120                 char pline[256];
00121                 int nx,ny;
00122                 cpal.open(name, std::ios::in);
00123                 if(cpal.good()){
00124                         cpal.getline(pline, 255);
00125                         cpal.getline(pline, 255);
00126                         cpal >> nx >> ny;
00127                         cpal.getline(pline, 255);
00128                         cpal.getline(pline, 255);
00129                         
00130                         for(maxcol = MIN(nx, IMGMAXCOLORENTRYS), cval=0; cval<maxcol; ++cval){
00131                                 cpal >> r >> g >> b;
00132                                 if (WORDS_BIGENDIAN)
00133                                         ULColorTable[cval] = (r << 16) | (g << 8) | b ;
00134                                 else
00135                                         ULColorTable[cval] = (b << 16) | (g << 8) | r ;
00136                         }
00137                         return;
00138                 }
00139                 XSM_SHOW_ALERT(ERR_SORRY, ERR_PALFILEREAD, name, 1);
00140         }
00141         // default grey and fallback mode:
00142         for (maxcol=64, cval=0; cval<maxcol; ++cval){
00143                 r = b = g = cval * 255 / maxcol; 
00144                 ULColorTable[cval] = (b << 16) | (g << 8) | r ;
00145         }
00146         
00147         // red/blue entries at end
00148         if (WORDS_BIGENDIAN){
00149                 ULColorTable[IMGMAXCOLORENTRYS  ] = (255 << 16) | (0 << 8) | 0 ;
00150                 ULColorTable[IMGMAXCOLORENTRYS+1] = (0 << 16) | (0 << 8) | 255 ;
00151         } else {
00152                 ULColorTable[IMGMAXCOLORENTRYS  ] = (0 << 16) | (0 << 8) | 255 ;
00153                 ULColorTable[IMGMAXCOLORENTRYS+1] = (255 << 16) | (0 << 8) | 0 ;
00154         }
00155 }
00156 
00157 void ShmImage2D::saveimage(gchar *name){
00158 // why are these not existent, but in the docu???
00159 //      gdk_pixbuf_save (gdk_pixbuf, name, "jpeg", NULL);
00160 //      gdk_pixbuf_save (gdk_pixbuf, name, "png", NULL);
00161 }
00162 
00163 void ShmImage2D::ShowPic(int xs, int ys){ 
00164         gnome_canvas_item_set (gitem_image,
00165                                "pixbuf", gdk_pixbuf,
00166                                NULL);
00167         
00168         red_line_points->coords[1] = red_line_points->coords[3] = y0;
00169         gnome_canvas_item_set (gitem_red_line, "points", red_line_points, NULL);
00170 
00171 }
00172 
00173 void ShmImage2D::ShowSubPic(int xs, int ys, int w, int h){ 
00174         xs*=ZoomFac; ys*=ZoomFac;
00175         w*=ZoomFac; h*=ZoomFac;
00176         if (xs >= width || ys >= height || w > width || h > height)
00177                 return;
00178         
00179         // look for:
00180         //  ArtUta *uta=art_uta_new(xs,ys,xs+w,ys+h);
00181         // // gnome_canvas_update_now(canvas);
00182         //  gnome_canvas_request_redraw_uta(GNOME_CANVAS(imgarea), uta);
00183         //  art_uta_free(uta);
00184         // geht so nicht :=(
00185         
00186         // Trick: rote Markierungslinie erzwingt bei Verschiebung
00187         // Refresh des darunter liegenden Bereiches...
00188         if (h>ZoomFac){
00189                 gnome_canvas_item_set (gitem_image,
00190                                        "pixbuf", gdk_pixbuf,
00191                                        NULL);
00192         }
00193         
00194         red_line_points->coords[1] = red_line_points->coords[3] = ys+y0 + .5*ZoomFac;
00195         gnome_canvas_item_set (gitem_red_line,
00196                                "points", red_line_points, 
00197                                "width_pixels", ZoomFac+1,
00198                                NULL);
00199 }
00200 
00201 void ShmImage2D::PutPixel(unsigned long x, unsigned long y, unsigned long val){
00202         ((U24*)(pixels + rowstride*y + bpp*x))->rgb = ULColorTable[val];
00203 }
00204 
00205 

Generated on Sat Apr 1 09:04:07 2006 for GXSM by  doxygen 1.4.6