bug repair: loading .png image, from Adrian Daerr
[gpiv.git] / src / display_image.c
blobef1aa5a3ba251c466bcd670479cac3fee65391b3
1 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 c-style: "K&R" -*- */
3 /*----------------------------------------------------------------------
5 gpiv - Graphic program for Particle Image Velocimetry, based on gtk/gnome
6 libraries.
8 Copyright (C) 2006, 2007, 2008
9 Gerber van der Graaf
11 This file is part of gpiv.
13 Gpiv is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2, or (at your option)
16 any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software Foundation,
25 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 ----------------------------------------------------------------------*/
29 #include "gpiv_gui.h"
30 #include "display_image.h"
33 void
34 create_img (Display *disp
36 /* ----------------------------------------------------------------------------
37 * Creates pixbuf image in gnome canvas
38 * row stride; each row is a 4-byte buffer array
42 /* GpivImagePar image_par = disp->img->image->header; */
43 guchar *pos1 = NULL, *pos2 = NULL;
44 gint i, j;
45 /* GdkPixbuf *pixbuf1 = NULL, *pixbuf2 = NULL; */
46 GdkPixbuf *px;
47 guint16 fact = 1;
48 gint depth = 8;
50 assert (disp != NULL);
51 assert (disp->img->image->frame1[0] != NULL);
52 assert (disp->img->exist_img);
54 disp->img->pixbuf1 = NULL;
55 disp->img->pixbuf2 = NULL;
57 fact = fact << (disp->img->image->header->depth - depth);
58 /* g_message ("create_img:: fact = %d", fact); */
61 * BUGFIX: this works for rowstride
63 disp->img->rgb_img_width = disp->img->image->header->ncolumns * 3;
64 while ((disp->img->rgb_img_width) % 4 != 0) {
65 disp->img->rgb_img_width++;
69 * this is a more formal way to do it
71 px = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
72 FALSE,
73 depth,
74 disp->img->image->header->ncolumns,
75 disp->img->image->header->nrows);
78 #ifdef DEBUG
79 g_message("create_img:: rowstride = %d, rgb_img_width = %d",
80 gdk_pixbuf_get_rowstride(px),
81 disp->img->rgb_img_width);
82 #endif
84 disp->img->rgbbuf_img1 = g_malloc (gdk_pixbuf_get_rowstride(px) *
85 disp->img->image->header->nrows);
87 disp->img->pixbuf1 = gdk_pixbuf_new_from_data (disp->img->rgbbuf_img1,
88 GDK_COLORSPACE_RGB,
89 FALSE, /* gboolean has_alpha */
90 depth, /* image_par->depth */
91 disp->img->image->header->ncolumns,
92 disp->img->image->header->nrows,
93 disp->img->rgb_img_width, /* rowstride */
94 NULL,
95 NULL);
97 if (disp->img->gci_img1 != NULL) {
98 destroy_img(disp);
101 disp->img->gci_img1 =
102 gnome_canvas_item_new( gnome_canvas_root( GNOME_CANVAS
103 (disp->canvas)),
104 gnome_canvas_pixbuf_get_type (),
105 "pixbuf", disp->img->pixbuf1,
106 NULL);
108 pos1 = disp->img->rgbbuf_img1;
111 for (i = 0; i < disp->img->image->header->nrows; i++) {
112 for (j = 0; j < disp->img->image->header->ncolumns; j++) {
113 *pos1++ = (guchar) (disp->img->image->frame1[i][j] / fact);
114 *pos1++ = (guchar) (disp->img->image->frame1[i][j] / fact);
115 *pos1++ = (guchar) (disp->img->image->frame1[i][j] / fact);
119 gdk_pixbuf_ref (disp->img->pixbuf1);
122 if (disp->img->image->header->x_corr) {
123 pos2 = NULL;
124 /* NEW */
125 disp->img->rgbbuf_img2 = g_malloc(gdk_pixbuf_get_rowstride(px) *
126 disp->img->image->header->nrows);
127 /* OLD */
128 /* disp->img->rgbbuf_img2 = g_malloc(disp->img->rgb_img_width * */
129 /* disp->img->image->header->nrows); */
131 disp->img->pixbuf2 = gdk_pixbuf_new_from_data (disp->img->rgbbuf_img2,
132 GDK_COLORSPACE_RGB,
133 FALSE,
134 depth,
135 disp->img->image->header->ncolumns,
136 disp->img->image->header->nrows,
137 disp->img->rgb_img_width,
138 NULL,
139 NULL);
140 if (disp->img->gci_img2 != NULL) {
141 destroy_img(disp);
144 disp->img->gci_img2 =
145 gnome_canvas_item_new( gnome_canvas_root( GNOME_CANVAS
146 (disp->canvas)),
147 gnome_canvas_pixbuf_get_type (),
148 "pixbuf", disp->img->pixbuf2,
149 NULL);
151 pos2 = disp->img->rgbbuf_img2;
152 for (i = 0; i < disp->img->image->header->nrows; i++) {
153 for (j = 0; j < disp->img->image->header->ncolumns; j++) {
154 *pos2++ = (guchar) (disp->img->image->frame2[i][j] / fact);
155 *pos2++ = (guchar) (disp->img->image->frame2[i][j] / fact);
156 *pos2++ = (guchar) (disp->img->image->frame2[i][j] / fact);
160 gdk_pixbuf_ref (disp->img->pixbuf2);
162 } else {
163 disp->img->gci_img2 = disp->img->gci_img1;
166 gdk_pixbuf_unref (px);
171 void
172 hide_img1 (Display *disp
174 /*-----------------------------------------------------------------------------
177 assert (disp != NULL);
179 if (disp->img->exist_img && disp->img->gci_img1 != NULL) {
180 gnome_canvas_item_hide (GNOME_CANVAS_ITEM (disp->img->gci_img1));
181 disp->display_backgrnd = SHOW_BG_DARKBLUE;
187 void
188 show_img1 (Display *disp
190 /*-----------------------------------------------------------------------------
193 assert (disp != NULL);
195 if (disp->img->exist_img && disp->img->gci_img1 != NULL) {
196 gnome_canvas_item_show (GNOME_CANVAS_ITEM (disp->img->gci_img1));
197 disp->display_backgrnd = SHOW_BG_IMG1;
203 void
204 hide_img2 (Display *disp
206 /*-----------------------------------------------------------------------------
209 assert (disp != NULL);
211 if (disp->img->exist_img && disp->img->gci_img2 != NULL) {
212 gnome_canvas_item_hide (GNOME_CANVAS_ITEM (disp->img->gci_img2));
213 disp->display_backgrnd = SHOW_BG_DARKBLUE;
218 void
219 show_img2 (Display *disp
221 /*-----------------------------------------------------------------------------
224 assert (disp != NULL);
226 if (disp->img->exist_img && disp->img->gci_img2 != NULL) {
227 gnome_canvas_item_show (GNOME_CANVAS_ITEM (disp->img->gci_img2));
228 disp->display_backgrnd = SHOW_BG_IMG2;
234 void
235 destroy_img (Display *disp
237 /*-----------------------------------------------------------------------------
240 assert (disp->img->gci_img1 != NULL);
241 gtk_object_destroy(GTK_OBJECT
242 (disp->img->gci_img1));
243 /* g_free(disp->img->gci_img1); */
244 disp->img->gci_img1 = NULL;
246 if(display_act->img->image->header->x_corr) {
247 gtk_object_destroy(GTK_OBJECT
248 (disp->img->gci_img2));
249 disp->img->gci_img2 = NULL;
255 GnomeCanvasItem *
256 create_background (Display *disp
258 /* ----------------------------------------------------------------------------
259 * Displays backgroundcolor
262 GnomeCanvasItem *bg = NULL;
263 gchar *color = NULL;
266 if (gpiv_par->display__backgrnd == SHOW_BG_DARKBLUE) {
267 color = "darkblue";
268 } else if (gpiv_par->display__backgrnd == SHOW_BG_BLACK) {
269 color = "black";
272 bg =
273 gnome_canvas_item_new(gnome_canvas_root(GNOME_CANVAS(disp->canvas)),
274 gnome_canvas_rect_get_type(),
275 "x1", (double) 0,
276 "y1", (double) 0,
277 "x2", (double) disp->img->image->header->ncolumns,
278 "y2", (double) disp->img->image->header->nrows,
279 "fill_color", color,
280 "width_units", 1.0,
281 NULL);
284 return bg;
289 void
290 destroy_background (GnomeCanvasItem *gci
292 /*-----------------------------------------------------------------------------
295 assert (gci != NULL);
296 gtk_object_destroy(GTK_OBJECT
297 (gci));
298 gci = NULL;