File: draw_image.c

package info (click to toggle)
pixelize 0.9.2-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 304 kB
  • ctags: 199
  • sloc: ansic: 2,349; makefile: 77
file content (222 lines) | stat: -rw-r--r-- 6,104 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*

Copyright (C) 2000  Paul Wilkins

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/
/* draw_image.c by Paul Wilkins 1/2/2000 */

#include <stdio.h>
#include <math.h>
#include <gtk/gtk.h>
#include <gdk_imlib.h>

#include "globals.h"
#include "render_image.h"
#include "read_db.h"
#include "draw_image.h"


int check_options(struct IMAGE_OPTIONS *im_opt){
   int ret = 1;
   if(im_opt->opt_alg == PIX_SIZE) {
      if(im_opt->pixW < 1){
         printf("Image size X: invalid number\n");
         ret = 0;
      }
      if(im_opt->pixH < 1){
         printf("Image size Y: invalid number\n");
         ret = 0;
      }
   } else {
      if(im_opt->nPixW < 1){
         printf("Image count X: invalid number\n");
         ret = 0;
      }
      if(im_opt->nPixH < 1){
         printf("Image count Y: invalid number\n");
         ret = 0;
      }
   }
   if(-1 == im_opt->proximity){
      printf("Image proximity: invalid number\n");
      ret = 0;
   }

   return ret;
}

int calc_dimensions(struct IMAGE_OPTIONS *im_opt){

   if(0 == check_options(im_opt)){
      printf("error with options\n");
      return 0;
   }

   if(im_opt->opt_alg == PIX_SIZE) {
      if(globals.in_im == NULL){
         im_opt->nPixW = 0;
         im_opt->nPixH = 0;
      } else {
	 im_opt->nPixW = ceil((double)globals.in_im->rgb_width / (double)im_opt->pixW);
	 im_opt->nPixH = ceil((double)globals.in_im->rgb_height / (double)im_opt->pixH);
      }
   } else if (im_opt->opt_alg == PIX_COUNT) {
      if(globals.in_im == NULL){
         im_opt->pixW = 0;
         im_opt->pixH = 0;
      } else {
	 im_opt->pixW = ceil((double)globals.in_im->rgb_width / (double)im_opt->nPixW);
	 im_opt->pixH = ceil((double)globals.in_im->rgb_height / (double)im_opt->nPixH);
      }
   } else {
      fprintf(stderr, "calc_dimensions: I shouldn't get here.\n");
      exit(1);
   }

   im_opt->width = im_opt->nPixW * im_opt->pixW;
   im_opt->height = im_opt->nPixH * im_opt->pixH;

   return 1;
}

int copy_opt_data(){
   int ret = 0;
   if(globals.cur_opt.width != globals.new_opt.width) ret = 1;
   if(globals.cur_opt.height != globals.new_opt.height) ret = 1;

   globals.cur_opt.pixW = globals.new_opt.pixW;
   globals.cur_opt.pixH = globals.new_opt.pixH;
   globals.cur_opt.nPixW = globals.new_opt.nPixW;
   globals.cur_opt.nPixH = globals.new_opt.nPixH;
   globals.cur_opt.width = globals.new_opt.width;
   globals.cur_opt.height = globals.new_opt.height;
   globals.cur_opt.opt_alg = globals.new_opt.opt_alg;
   globals.cur_opt.proximity = globals.new_opt.proximity;

   return ret;
}

void redraw_screen(int x, int y, int w, int h){
   GtkStyle *style;

   /* if we have opened or rendered an image */
   if(globals.in_im != NULL){

      /* we may need to create the pixmap it */
      if(globals.pixmap == NULL){

         /* has the picture been rendered yet? */
         if(globals.image){
            render_image_pixmap(globals.out_im);
         } else {
            render_image_pixmap(globals.in_im);
         }
      }

      /* copy it to the screen */
      style = gtk_widget_get_style(GTK_WIDGET(globals.picDA));
      gdk_draw_pixmap(GTK_WIDGET(globals.picDA)->window,
                      style->fg_gc[GTK_WIDGET_STATE(GTK_WIDGET(globals.picDA))],
                      globals.pixmap,
                      x, y, x, y, w, h);
   }
}

void render_image_pixmap(GdkImlibImage *im){

   /* Render the original 24-bit Image data into a pixmap of size w * h */
   gdk_imlib_render(im,
      globals.cur_opt.width, globals.cur_opt.height);

   if(globals.pixmap != NULL){
      gdk_imlib_free_pixmap(globals.pixmap);
   }

   /* Extract the Image and mask pixmaps from the Image */
   globals.pixmap = gdk_imlib_move_image(im);
}


int draw_big_image(GdkImlibImage *im){

   render_image_pixmap(im);

   gdk_draw_pixmap(globals.picDA->window,
		   globals.picDA->style->fg_gc[GTK_WIDGET_STATE(globals.picDA)],
		   globals.pixmap,
                   0, 0, 
                   0, 0, 
                   globals.cur_opt.width, globals.cur_opt.height);

   return 1;
}


int change_small_image(int xx, int yy){
   struct PIC_DB *db;
   struct IMAGE_INFO *inf;
   GdkImlibImage *im;
   GdkImlibImage *scale_im;

   if(globals.image != NULL){
      inf = &(globals.image[yy][xx]);
      db = inf->matches[inf->match_no];

      /* load the image */
      if(NULL == (im=gdk_imlib_load_image(db->fname))){
	 fprintf(stderr, "Error: Unable to open %s\n", db->fname);
	 return 0;
      }

      /* scale the image */
      if(NULL == (scale_im=gdk_imlib_clone_scaled_image(im,
	 globals.cur_opt.pixW, globals.cur_opt.pixH))){
	 fprintf(stderr, "Error: Unable to scale image %s\n", db->fname);
	 return 0;
      }

      draw_small_image(scale_im, xx, yy);
      copy_image_RGB(scale_im->rgb_data, globals.out_im->rgb_data, xx, yy);

      /* tell imlib we messed with the RGB data */
      gdk_imlib_changed_image(globals.out_im);

      /* this is no longer valid */
      if(globals.pixmap != NULL){
	 gdk_imlib_free_pixmap(globals.pixmap);
      }
      globals.pixmap = NULL;

      /* free the images we are done with */
      gdk_imlib_destroy_image(im);
      gdk_imlib_destroy_image(scale_im);

   }
   return 1;
}

int draw_small_image(GdkImlibImage *scale_im, int x, int y){

   gdk_imlib_paste_image(scale_im, 
      globals.picDA->window, 
      globals.cur_opt.pixW * x, 
      globals.cur_opt.pixH * y, 
      globals.cur_opt.pixW, 
      globals.cur_opt.pixH);

   return 1;
}