|
| 1 | +/* XQF - Quake server browser and launcher |
| 2 | + * render a chunk of memory as GdkPixmap and GdkBitmap |
| 3 | + * Copyright (C) 2004 Ludwig Nussel <l-n@users.sourceforge.net> |
| 4 | + * |
| 5 | + * This program is free software; you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation; either version 2 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program; if not, write to the Free Software |
| 17 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
| 18 | + */ |
| 19 | + |
| 20 | +#include <stdio.h> |
| 21 | +#include <gtk/gtk.h> |
| 22 | +#include <gdk/gdk.h> |
| 23 | + |
| 24 | +#include <sys/types.h> |
| 25 | +#include <sys/stat.h> |
| 26 | +#include <fcntl.h> |
| 27 | +#include <unistd.h> |
| 28 | + |
| 29 | +#include "debug.h" |
| 30 | +#include "memtopixmap.h" |
| 31 | + |
| 32 | +GdkPixbuf* renderMemToPixbuf(const guchar* mem, size_t len) { |
| 33 | + GdkPixbufLoader* loader = NULL; |
| 34 | + gboolean ok = FALSE; |
| 35 | + GdkPixbuf* pixbuf = NULL; |
| 36 | + |
| 37 | + GError *err=NULL; |
| 38 | + |
| 39 | + if (!mem) return NULL; |
| 40 | + if (!len) return NULL; |
| 41 | + |
| 42 | + loader = gdk_pixbuf_loader_new(); |
| 43 | + g_return_val_if_fail(loader!=NULL, NULL); |
| 44 | + |
| 45 | + ok = gdk_pixbuf_loader_write(loader, mem, len,&err); |
| 46 | + if (err != NULL) { |
| 47 | + xqf_warning("%s", err->message); |
| 48 | + g_error_free(err); |
| 49 | + } |
| 50 | + err = NULL; |
| 51 | + gdk_pixbuf_loader_close(loader,&err); |
| 52 | + if (err != NULL) { |
| 53 | + xqf_warning("%s", err->message); |
| 54 | + g_error_free(err); |
| 55 | + } |
| 56 | + |
| 57 | + if (!ok) { |
| 58 | + return NULL; |
| 59 | + } |
| 60 | + else { |
| 61 | + pixbuf = gdk_pixbuf_loader_get_pixbuf(loader); |
| 62 | + g_object_ref(pixbuf); |
| 63 | + g_object_unref(G_OBJECT(loader)); |
| 64 | + } |
| 65 | + |
| 66 | + return pixbuf; |
| 67 | +} |
| 68 | + |
| 69 | +void renderMemToGtkPixmap(const guchar* mem, size_t len, GdkPixmap **pix, GdkBitmap **mask, guint* width, guint* height, gushort overBrightBits) { |
| 70 | + GdkPixbuf* pixbuf = renderMemToPixbuf(mem, len); |
| 71 | + |
| 72 | + if (pixbuf) { |
| 73 | + GdkPixbuf* pixbuf_tmp = NULL; |
| 74 | + pixbuf_tmp = pixbuf; |
| 75 | + pixbuf = gdk_pixbuf_scale_simple(pixbuf,320,240,GDK_INTERP_TILES); |
| 76 | + *height = gdk_pixbuf_get_height(pixbuf); |
| 77 | + *width = gdk_pixbuf_get_width(pixbuf); |
| 78 | + |
| 79 | + if (overBrightBits > 0 && gdk_pixbuf_get_n_channels (pixbuf) >= 3) // gamma correction |
| 80 | + { |
| 81 | + unsigned x, y; |
| 82 | + unsigned w = gdk_pixbuf_get_width (pixbuf); |
| 83 | + unsigned h = gdk_pixbuf_get_height (pixbuf); |
| 84 | + unsigned rs = gdk_pixbuf_get_rowstride (pixbuf); |
| 85 | + unsigned c = gdk_pixbuf_get_n_channels (pixbuf); |
| 86 | + unsigned char* p = gdk_pixbuf_get_pixels (pixbuf); |
| 87 | + register unsigned tmp; |
| 88 | + for (y=0; y < h; ++y) { |
| 89 | + for (x=0; x < w; ++x, p+=c) { |
| 90 | + tmp = ((int)p[0]) << overBrightBits; |
| 91 | + p[0] = (tmp>0xFF)?0xFF:tmp; |
| 92 | + tmp = ((int)p[1]) << overBrightBits; |
| 93 | + p[1] = (tmp>0xFF)?0xFF:tmp; |
| 94 | + tmp = ((int)p[2]) << overBrightBits; |
| 95 | + p[2] = (tmp>0xFF)?0xFF:tmp; |
| 96 | + } |
| 97 | + if (x*c<rs) { |
| 98 | + p += (rs - x*c); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + gdk_pixbuf_render_pixmap_and_mask(pixbuf,pix,mask,0); |
| 104 | + |
| 105 | + g_object_unref(pixbuf); |
| 106 | + g_object_unref(pixbuf_tmp); |
| 107 | + } |
| 108 | + else { |
| 109 | + *width=0; |
| 110 | + *height=0; |
| 111 | + } |
| 112 | +} |
0 commit comments