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