Crossfire Client, Trunk  R19726
image.c
Go to the documentation of this file.
1 /*
2  * Crossfire -- cooperative multi-player graphical RPG and adventure game
3  *
4  * Copyright (c) 1999-2013 Mark Wedel and the Crossfire Development Team
5  * Copyright (c) 1992 Frank Tore Johansen
6  *
7  * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
8  * welcome to redistribute it under certain conditions. For details, see the
9  * 'LICENSE' and 'COPYING' files.
10  *
11  * The authors can be reached via e-mail to crossfire-devel@real-time.com
12  */
13 
21 #include "config.h"
22 
23 #include <png.h>
24 #include <stdlib.h>
25 #include <sys/stat.h>
26 
27 #ifndef WIN32
28 #include <unistd.h>
29 #endif
30 
31 /* Pick up the gtk headers we need */
32 #include <gtk/gtk.h>
33 #ifndef WIN32
34 #include <gdk/gdkx.h>
35 #else
36 #include <gdk/gdkwin32.h>
37 #endif
38 #include <gdk/gdkkeysyms.h>
39 
40 #ifdef HAVE_SDL
41 #include <SDL.h>
42 #include <SDL_image.h>
43 #endif
44 
45 #include "client-types.h"
46 #include "client.h"
47 #include "image.h"
48 #include "main.h"
49 #include "mapdata.h"
50 #include "gtk2proto.h"
51 
52 extern GtkWidget *window_root;
54 
55 struct {
56  char *name;
57  guint32 checksum;
58  guint8 *png_data;
59  guint32 width, height;
61 
62 #define BPP 4
63 
65 
67 
68 /* Do we have new images to display? */
70 
71 /*
72  * this is used to rescale big images that will be drawn in the inventory/look
73  * lists. What the code further below basically does is figure out how big the
74  * object is (in squares), and this looks at the icon_rescale_factor to figure
75  * what scale factor it gives. Not that the icon_rescale_factor values are
76  * passed directly to the rescale routines. These represent percentages - so
77  * even taking into account that the values diminish as the table grows, they
78  * will still appear larger if the location in the table times the factor is
79  * greater than 100. We find the largest dimension that the image has. The
80  * values in the comment is the effective scaling compared to the base image
81  * size that this big image will appear as. Using a table makes it easier to
82  * adjust the values so things look right.
83  */
84 
85 #define MAX_ICON_SPACES 10
86 static const int icon_rescale_factor[MAX_ICON_SPACES] = {
87  100, 100, 80 /* 2 = 160 */, 60 /* 3 = 180 */,
88  50 /* 4 = 200 */, 45 /* 5 = 225 */, 40 /* 6 = 240 */,
89  35 /* 7 = 259 */, 35 /* 8 = 280 */, 33 /* 9 = 300 */
90 };
91 
92 /******************************************************************************
93  *
94  * Code related to face caching.
95  *
96  *****************************************************************************/
97 
98 /* Does not appear to be used anywhere
99 typedef struct Keys {
100  uint8 flags;
101  sint8 direction;
102  KeySym keysym;
103  char *command;
104  struct Keys *next;
105 } Key_Entry;
106 */
107 
108 /* Rotate right from bsd sum. */
109 #define ROTATE_RIGHT(c) if ((c) & 01) (c) = ((c) >>1) + 0x80000000; else (c) >>= 1;
110 
111 /*#define CHECKSUM_DEBUG*/
112 
116 static void create_icon_image(guint8 *data, PixmapInfo *pi) {
117  pi->icon_mask = NULL;
118  pi->icon_image = rgba_to_gdkpixbuf(data, pi->icon_width, pi->icon_height);
119 }
120 
127 static void create_map_image(guint8 *data, PixmapInfo *pi) {
128  pi->map_image = NULL;
129  pi->map_mask = NULL;
130 
132 #if defined(HAVE_SDL)
133  int i;
134  SDL_Surface *fog;
135  guint32 g,*p;
136  guint8 *l;
137 
138 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
139  pi->map_image = SDL_CreateRGBSurfaceFrom(data, pi->map_width,
140  pi->map_height, 32, pi->map_width * 4, 0xff,
141  0xff00, 0xff0000, 0xff000000);
142 
143  fog = SDL_CreateRGBSurface(SDL_SRCALPHA | SDL_HWSURFACE,
144  pi->map_width, pi->map_height, 32, 0xff,
145  0xff00, 0xff0000, 0xff000000);
146  SDL_LockSurface(fog);
147 
148  for (i=0; i < pi->map_width * pi->map_height; i++) {
149  l = (guint8 *) (data + i*4);
150 #if 1
151  g = MAX(*l, *(l+1));
152  g = MAX(g, *(l+2));
153 #else
154  g = ( *l + *(l+1) + *(l+2)) / 3;
155 #endif
156  p = (guint32*) fog->pixels + i;
157  *p = g | (g << 8) | (g << 16) | (*(l + 3) << 24);
158  }
159 
160  SDL_UnlockSurface(fog);
161  pi->fog_image = fog;
162 #else
163  /* Big endian */
164  pi->map_image = SDL_CreateRGBSurfaceFrom(data, pi->map_width,
165  pi->map_height, 32, pi->map_width * 4, 0xff000000,
166  0xff0000, 0xff00, 0xff);
167 
168  fog = SDL_CreateRGBSurface(SDL_SRCALPHA | SDL_HWSURFACE,
169  pi->map_width, pi->map_height, 32, 0xff000000,
170  0xff0000, 0xff00, 0xff);
171  SDL_LockSurface(fog);
172 
173  /*
174  * I think this works out, but haven't tried it on a big endian machine
175  * as my recollection is that the png data would be in the same order,
176  * just the bytes for it to go on the screen are reversed.
177  */
178  for (i=0; i < pi->map_width * pi->map_height; i++) {
179  l = (guint8 *) (data + i*4);
180 #if 1
181  g = MAX(*l, *(l+1));
182  g = MAX(g, *(l+2));
183 #else
184  g = ( *l + *(l+1) + *(l+2)) / 3;
185 #endif
186  p = (guint32*) fog->pixels + i;
187  *p = (g << 8) | (g << 16) | (g << 24) | *(l + 3);
188  }
189 
190  for (i=0; i < pi->map_width * pi->map_height; i+= 4) {
191  guint32 *tmp;
192 
193  /*
194  * The pointer arithemtic below looks suspicious, but it is a patch
195  * that is submitted, so just putting it in as submitted. MSW
196  * 2004-05-11
197  */
198  p = (guint32*) (fog->pixels + i);
199  g = ( ((*p >> 24) & 0xff) + ((*p >> 16) & 0xff) + ((*p >> 8) & 0xff)) / 3;
200  tmp = (guint32*) fog->pixels + i;
201  *tmp = (g << 24) | (g << 16) | (g << 8) | (*p & 0xff);
202  }
203 
204  SDL_UnlockSurface(fog);
205  pi->fog_image = fog;
206 #endif
207 
208 #endif
210 #ifdef HAVE_OPENGL
211  create_opengl_map_image(data, pi);
212 #endif
214  pi->map_image = rgba_to_cairo_surface(data, pi->map_width, pi->map_height);
215  }
216 }
217 
223 static void free_pixmap(PixmapInfo *pi)
224 {
225  if (pi->icon_image) {
226  g_object_unref(pi->icon_image);
227  }
228  if (pi->icon_mask) {
229  g_object_unref(pi->icon_mask);
230  }
231  if (pi->map_mask) {
232  gdk_pixmap_unref(pi->map_mask);
233  }
235 #ifdef HAVE_SDL
236  if (pi->map_image) {
237  SDL_FreeSurface(pi->map_image);
238  free(((SDL_Surface*)pi->map_image)->pixels);
239  SDL_FreeSurface(pi->fog_image);
240  /*
241  * Minor memory leak here - SDL_FreeSurface() frees the pixel
242  * data _unless_ SDL_CreateRGBSurfaceFrom() was used to create
243  * the surface. SDL_CreateRGBSurfaceFrom() is used to create
244  * the map data, which is why we need the free there. The
245  * reason this is a minor memory leak is because
246  * SDL_CreateRGBSurfaceFrom() is used to create the question
247  * mark image, and without this free, that data is not freed.
248  * However, with this, client crashes after disconnecting from
249  * server with double free.
250  */
251  /* free(((SDL_Surface*)pi->fog_image)->pixels);*/
252  }
253 #endif
255 #ifdef HAVE_OPENGL
256  opengl_free_pixmap(pi);
257 #endif
259  if (pi->map_image) {
260  cairo_surface_destroy(pi->map_image);
261  }
262  }
263 }
264 
279  guint8 *rgba_data, int width, int height) {
280  int nx, ny, iscale, factor;
281  PixmapInfo *pi;
282 
283  if (pixmap_num <= 0 || pixmap_num >= MAXPIXMAPNUM) {
284  return 1;
285  }
286 
287  if (pixmaps[pixmap_num] != pixmaps[0]) {
288  /* As per bug 2938906, one can see image corruption when switching between
289  * servers. The cause is that the cache table stores away
290  * a pointer to the pixmap[] entry - if we go and free it,
291  * the cache table can point to garbage, so don't free it.
292  * This causes some memory leak, but if/when there is good
293  * cache support for multiple servers, eventually the amount
294  * of memory consumed will reach a limit (it has every image of
295  * every server in memory
296  *
297  * The cause of image corruption requires a few different things:
298  * 1) images of the same name have different numbers on the 2 serves.
299  * 2) the image number is higher on the first than second server
300  * 3) the image using the high number does not exist/is different
301  * on the second server, causing this routine to be called.
302  */
303 
304  if (!use_config[CONFIG_CACHE]) {
305  free_pixmap(pixmaps[pixmap_num]);
306  free(pixmaps[pixmap_num]);
307  }
308  pixmaps[pixmap_num] = pixmaps[0];
309  }
310 
311  pi = calloc(1, sizeof(PixmapInfo));
312 
313  iscale = use_config[CONFIG_ICONSCALE];
314 
315  /*
316  * If the image is big, figure out what we should scale it to so it fits
317  * better display
318  */
319  if (width > DEFAULT_IMAGE_SIZE || height>DEFAULT_IMAGE_SIZE) {
320  int ts = 100;
321 
322  factor = width / DEFAULT_IMAGE_SIZE;
323  if (factor >= MAX_ICON_SPACES) {
324  factor = MAX_ICON_SPACES - 1;
325  }
326  if (icon_rescale_factor[factor] < ts) {
327  ts = icon_rescale_factor[factor];
328  }
329 
330  factor = height / DEFAULT_IMAGE_SIZE;
331  if (factor >= MAX_ICON_SPACES) {
332  factor = MAX_ICON_SPACES - 1;
333  }
334  if (icon_rescale_factor[factor] < ts) {
335  ts = icon_rescale_factor[factor];
336  }
337 
338  iscale = ts * use_config[CONFIG_ICONSCALE] / 100;
339  }
340 
341  /* In all cases, the icon images are in native form. */
342  if (iscale != 100) {
343  nx=width;
344  ny=height;
345  guint8 *png_tmp = rescale_rgba_data(rgba_data, &nx, &ny, iscale);
346  pi->icon_width = nx;
347  pi->icon_height = ny;
348  create_icon_image(png_tmp, pi);
349  free(png_tmp);
350  } else {
351  pi->icon_width = width;
352  pi->icon_height = height;
353  create_icon_image(rgba_data, pi);
354  }
355 
356  /*
357  * If icon_scale matched use_config[CONFIG_MAPSCALE], we could try to be
358  * more intelligent, but this should not be called too often, and this
359  * keeps the code simpler.
360  */
361  guint8 *png_tmp;
362 
363  if (use_config[CONFIG_MAPSCALE] != 100) {
364  nx=width;
365  ny=height;
366  png_tmp = rescale_rgba_data(rgba_data, &nx, &ny, use_config[CONFIG_MAPSCALE]);
367  pi->map_width = nx;
368  pi->map_height = ny;
369  create_map_image(png_tmp, pi);
370  /*
371  * pixmap mode and opengl don't need the rgba data after they have
372  * created the image, so we can free it. SDL uses the raw rgba data,
373  * so it can't be freed.
374  */
377  free(png_tmp);
378  }
379  } else {
380  pi->map_width = width;
381  pi->map_height = height;
382  /*
383  * If using SDL mode, a copy of the rgba data needs to be stored away.
384  */
386  png_tmp = g_malloc(width * height * BPP);
387  memcpy(png_tmp, rgba_data, width * height * BPP);
388  } else {
389  png_tmp = rgba_data;
390  }
391  create_map_image(png_tmp, pi);
392  }
393  /*
394  * Not ideal, but if it is missing the map or icon image, presume something
395  * failed. However, opengl doesn't set the map_image, so if using that
396  * display mode, don't make this check.
397  */
399  free_pixmap(pi);
400  free(pi);
401  return 1;
402  }
403  if (ce) {
404  ce->image_data = pi;
405  }
406  pixmaps[pixmap_num] = pi;
407  if (use_config[CONFIG_CACHE]) {
408  have_new_image++;
409  }
410 
411  return 0;
412 }
413 
420 void addsmooth(guint16 face, guint16 smooth_face)
421 {
422  pixmaps[face]->smooth_face = smooth_face;
423 }
424 
433 int associate_cache_entry(Cache_Entry *ce, int pixnum)
434 {
435  pixmaps[pixnum] = ce->image_data;
436  return 0;
437 }
438 
446 {
447  int i;
448 
450  /*
451  * The entries in the pixmaps array are also tracked in the image cache in
452  * the common area. We will try to recycle those images that we can.
453  * Thus, if we connect to a new server, we can just re-use the images we
454  * have already rendered.
455  */
456  for (i=1; i<MAXPIXMAPNUM; i++) {
457  if (!want_config[CONFIG_CACHE] && pixmaps[i] != pixmaps[0]) {
458  free_pixmap(pixmaps[i]);
459  free(pixmaps[i]);
460  pixmaps[i] = pixmaps[0];
461  }
462  }
463 }
464 
465 static GtkWidget *pbar=NULL;
466 static GtkWidget *pbar_window=NULL;
467 static GtkAdjustment *padj=NULL;
468 
480 void image_update_download_status(int start, int end, int total)
481 {
482  int x, y, wx, wy, w, h;
483 
484  if (start == 1) {
485  padj = (GtkAdjustment*) gtk_adjustment_new (0, 1, total, 0, 0, 0);
486 
487  pbar = gtk_progress_bar_new_with_adjustment(padj);
488  gtk_progress_set_format_string(GTK_PROGRESS(pbar), "Downloading image %v of %u (%p%% complete)");
489  gtk_progress_bar_set_bar_style(GTK_PROGRESS_BAR(pbar), GTK_PROGRESS_CONTINUOUS);
490  gtk_progress_set_show_text(GTK_PROGRESS(pbar), TRUE);
491  get_window_coord(window_root, &x,&y, &wx,&wy,&w,&h);
492 
493  pbar_window = gtk_window_new(GTK_WINDOW_POPUP);
494  gtk_window_set_policy(GTK_WINDOW(pbar_window), TRUE, TRUE, FALSE);
495  gtk_window_set_transient_for(GTK_WINDOW(pbar_window), GTK_WINDOW (window_root));
496  /*
497  * We more or less want this window centered on the main crossfire
498  * window, and not necessarily centered on the screen or in the upper
499  * left corner.
500  */
501  gtk_widget_set_uposition(pbar_window, (wx + w)/2, (wy + h) / 2);
502 
503  gtk_container_add(GTK_CONTAINER(pbar_window), pbar);
504  gtk_widget_show(pbar);
505  gtk_widget_show(pbar_window);
506  }
507  if (start == total) {
508  gtk_widget_destroy(pbar_window);
509  pbar = NULL;
510  pbar_window = NULL;
511  padj = NULL;
512  return;
513  }
514 
515  gtk_progress_set_value(GTK_PROGRESS(pbar), start);
516  while ( gtk_events_pending() ) {
517  gtk_main_iteration();
518  }
519 }
520 
527 void get_map_image_size(int face, guint8 *w, guint8 *h)
528 {
529  /* We want to calculate the number of spaces this image
530  * uses it. By adding the image size but substracting one,
531  * we cover the cases where the image size is not an even
532  * increment. EG, if the map_image_size is 32, and an image
533  * is 33 wide, we want that to register as two spaces. By
534  * adding 31, that works out.
535  */
536  if ( face < 0 || face >= MAXPIXMAPNUM) {
537  *w = 1;
538  *h = 1;
539  } else {
540  *w = (pixmaps[face]->map_width + map_image_size - 1)/ map_image_size;
541  *h = (pixmaps[face]->map_height + map_image_size - 1)/ map_image_size;
542  }
543 }
544 
545 /******************************************************************************
546  *
547  * Code related to face caching.
548  *
549  *****************************************************************************/
550 
561 {
562  int i;
563  GtkStyle *style;
564 #include "../../pixmaps/question.xpm"
565 
566 
567  LOG(LOG_DEBUG, "gtk-v2::init_image_cache_data", "Init Image Cache");
568 
569  style = gtk_widget_get_style(window_root);
570  pixmaps[0] = g_malloc(sizeof(PixmapInfo));
571  pixmaps[0]->icon_image = gdk_pixmap_create_from_xpm_d(window_root->window,
572  (GdkBitmap**)&pixmaps[0]->icon_mask,
573  &style->bg[GTK_STATE_NORMAL],
574  (gchar **)question_xpm);
575 #ifdef HAVE_SDL
577  /*
578  * Make a semi-transparent question mark symbol to use for the cached
579  * images.
580  */
581 #include "../../pixmaps/question.sdl"
582  pixmaps[0]->map_image = SDL_CreateRGBSurfaceFrom(question_sdl,
583  32, 32, 1, 4, 1, 1, 1, 1);
584  SDL_SetAlpha(pixmaps[0]->map_image, SDL_SRCALPHA, 70);
585  pixmaps[0]->fog_image = SDL_CreateRGBSurfaceFrom(question_sdl,
586  32, 32, 1, 4, 1, 1, 1, 1);
587  SDL_SetAlpha(pixmaps[0]->fog_image, SDL_SRCALPHA, 70);
588  } else
589 #endif
591  pixmaps[0]->map_image = pixmaps[0]->icon_image;
592  pixmaps[0]->fog_image = pixmaps[0]->icon_image;
593  pixmaps[0]->map_mask = pixmaps[0]->icon_mask;
594  }
595 #ifdef HAVE_OPENGL
598  }
599 #endif
600 
601  pixmaps[0]->icon_width = pixmaps[0]->icon_height = pixmaps[0]->map_width = pixmaps[0]->map_height = map_image_size;
602  pixmaps[0]->smooth_face = 0;
603 
604  /* Don't do anything special for SDL image - rather, that drawing
605  * code will check to see if there is no data
606  */
607 
608  /* Initialize all the images to be of the same value. */
609  for (i=1; i<MAXPIXMAPNUM; i++) {
610  pixmaps[i] = pixmaps[0];
611  }
612 
614 }
guint32 width
Definition: image.c:59
void get_window_coord(GtkWidget *win, int *x, int *y, int *wx, int *wy, int *w, int *h)
Gets the coordinates of a specified window.
Definition: main.c:599
#define BPP
Definition: image.c:62
int image_size
Definition: image.c:53
void addsmooth(guint16 face, guint16 smooth_face)
Referenced from common/commands.c.
Definition: image.c:420
gint16 use_config[CONFIG_NUMS]
Definition: init.c:39
#define DEFAULT_IMAGE_SIZE
Definition: image.h:40
static const int icon_rescale_factor[MAX_ICON_SPACES]
Definition: image.c:86
int map_image_size
Definition: map.c:61
void opengl_free_pixmap(PixmapInfo *pi)
#define CONFIG_DISPLAYMODE
Definition: client.h:160
void * fog_image
Definition: image.h:52
guint8 * png_data
Definition: image.c:58
#define MAXPIXMAPNUM
Definition: client.h:489
static GtkWidget * pbar_window
Definition: image.c:466
Client type definitions.
void * image_data
Definition: client.h:502
void image_update_download_status(int start, int end, int total)
Draws a status bar showing where we our in terms of downloading all the image data.
Definition: image.c:480
void * map_mask
Definition: image.h:50
void create_opengl_map_image(guint8 *data, PixmapInfo *pi)
void * map_image
Definition: image.h:50
guint16 icon_height
Definition: image.h:49
int create_and_rescale_image_from_data(Cache_Entry *ce, int pixmap_num, guint8 *rgba_data, int width, int height)
Takes the pixmap to put the data into, as well as the rgba data (ie, already loaded with png_to_data)...
Definition: image.c:278
Contains various global definitions and XML file name and path defaults.
void LOG(LogLevel level, const char *origin, const char *format,...)
Log messages of a certain importance to stderr.
Definition: misc.c:115
#define CFG_DM_PIXMAP
Definition: client.h:205
void init_image_cache_data(void)
Initializes the data for image caching Create question mark to display in each supported rendering mo...
Definition: image.c:560
guint16 smooth_face
A face used for smoothing with this face.
Definition: image.h:53
Pixmap data.
GtkWidget * window_root
In main.c.
Definition: main.c:102
void create_opengl_question_mark(void)
guint32 checksum
Definition: client.h:500
guint16 map_height
Definition: image.h:51
void * icon_image
Definition: image.h:48
guint32 height
Definition: image.c:59
void reset_image_cache_data(void)
We can now connect to different servers, so we need to clear out any old images.
Definition: image.c:514
struct @2 private_cache[MAXPIXMAPNUM]
#define CONFIG_CACHE
Definition: client.h:155
guint16 map_width
Definition: image.h:51
gint16 want_config[CONFIG_NUMS]
Definition: init.c:39
#define MAX_ICON_SPACES
Definition: image.c:85
static void create_map_image(guint8 *data, PixmapInfo *pi)
Helper function to make the code more readable.
Definition: image.c:127
int have_new_image
Definition: image.c:69
cairo_surface_t * rgba_to_cairo_surface(guint8 *data, int width, int height)
Create a Cairo surface for the given RGBA data.
Definition: png.c:533
static void create_icon_image(guint8 *data, PixmapInfo *pi)
Helper function to make the code more readable.
Definition: image.c:116
void reset_image_data(void)
Connecting to different servers, try to clear out any old images.
Definition: image.c:445
#define CONFIG_ICONSCALE
Definition: client.h:157
static GtkAdjustment * padj
Definition: image.c:467
static void free_pixmap(PixmapInfo *pi)
Memory management.
Definition: image.c:223
#define CFG_DM_SDL
Definition: client.h:206
guint16 icon_width
Definition: image.h:49
Used mostly in the cache.c file, however, it can be returned to the graphic side of things so that th...
Definition: client.h:498
PixmapInfo * pixmaps[MAXPIXMAPNUM]
Definition: image.c:64
void * icon_mask
Definition: image.h:48
void init_common_cache_data(void)
Definition: image.c:364
guint8 * rescale_rgba_data(guint8 *data, int *width, int *height, int scale)
Takes png data and scales it accordingly.
Definition: png.c:262
static GtkWidget * pbar
Definition: image.c:465
Includes various dependencies header files needed by most everything.
char * name
Definition: image.c:56
int last_face_num
Definition: image.c:66
GdkPixbuf * rgba_to_gdkpixbuf(guint8 *data, int width, int height)
Create a GdkPixbuf for the given RGBA data.
Definition: png.c:507
#define CFG_DM_OPENGL
Definition: client.h:207
Useful debugging information.
Definition: client.h:407
int associate_cache_entry(Cache_Entry *ce, int pixnum)
This functions associates image_data in the cache entry with the specific pixmap number.
Definition: image.c:433
#define CONFIG_MAPSCALE
Definition: client.h:158
void get_map_image_size(int face, guint8 *w, guint8 *h)
Definition: image.c:527