From cca66b9ec4e494c1d919bff0f71a820d8afab1fa Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 20:24:48 +0200 Subject: Adding upstream version 1.2.2. Signed-off-by: Daniel Baumann --- src/trace/imagemap-gdk.cpp | 185 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 185 insertions(+) create mode 100644 src/trace/imagemap-gdk.cpp (limited to 'src/trace/imagemap-gdk.cpp') diff --git a/src/trace/imagemap-gdk.cpp b/src/trace/imagemap-gdk.cpp new file mode 100644 index 0000000..10fe27e --- /dev/null +++ b/src/trace/imagemap-gdk.cpp @@ -0,0 +1,185 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/** @file + * TODO: insert short description here + *//* + * Authors: see git history + * + * Copyright (C) 2018 Authors + * Released under GNU GPL v2+, read the file 'COPYING' for more information. + */ +#include + +#include "imagemap-gdk.h" + + +/*######################################################################### +## G R A Y M A P +#########################################################################*/ + +GrayMap *gdkPixbufToGrayMap(GdkPixbuf *buf) +{ + if (!buf) + return nullptr; + + int width = gdk_pixbuf_get_width(buf); + int height = gdk_pixbuf_get_height(buf); + guchar *pixdata = gdk_pixbuf_get_pixels(buf); + int rowstride = gdk_pixbuf_get_rowstride(buf); + int n_channels = gdk_pixbuf_get_n_channels(buf); + + GrayMap *grayMap = GrayMapCreate(width, height); + if (!grayMap) + return nullptr; + + //### Fill in the odd cells with RGB values + int x,y; + int row = 0; + for (y=0 ; ysetPixel(grayMap, x, y, bright); + p += n_channels; + } + row += rowstride; + } + + return grayMap; +} + +GdkPixbuf *grayMapToGdkPixbuf(GrayMap *grayMap) +{ + if (!grayMap) + return nullptr; + + guchar *pixdata = (guchar *) + malloc(sizeof(guchar) * grayMap->width * grayMap->height * 3); + if (!pixdata) + { + g_warning("grayMapToGdkPixbuf: can not allocate memory for conversion."); + return nullptr; + } + + int n_channels = 3; + int rowstride = grayMap->width * 3; + + GdkPixbuf *buf = gdk_pixbuf_new_from_data(pixdata, GDK_COLORSPACE_RGB, + 0, 8, grayMap->width, grayMap->height, + rowstride, (GdkPixbufDestroyNotify)g_free, nullptr); + + //### Fill in the odd cells with RGB values + int x,y; + int row = 0; + for (y=0 ; yheight ; y++) + { + guchar *p = pixdata + row; + for (x=0 ; xwidth ; x++) + { + unsigned long pix = grayMap->getPixel(grayMap, x, y) / 3; + p[0] = p[1] = p[2] = (guchar)(pix & 0xff); + p += n_channels; + } + row += rowstride; + } + + return buf; +} + + +/*######################################################################### +## R G B M A P +#########################################################################*/ + +RgbMap *gdkPixbufToRgbMap(GdkPixbuf *buf) +{ + if (!buf) + return nullptr; + + int width = gdk_pixbuf_get_width(buf); + int height = gdk_pixbuf_get_height(buf); + guchar *pixdata = gdk_pixbuf_get_pixels(buf); + int rowstride = gdk_pixbuf_get_rowstride(buf); + int n_channels = gdk_pixbuf_get_n_channels(buf); + + RgbMap *rgbMap = RgbMapCreate(width, height); + if (!rgbMap) + return nullptr; + + //### Fill in the cells with RGB values + int x,y; + int row = 0; + for (y=0 ; ysetPixel(rgbMap, x, y, r, g, b); + p += n_channels; + } + row += rowstride; + } + + return rgbMap; +} + + + +/*######################################################################### +## I N D E X E D M A P +#########################################################################*/ + + +GdkPixbuf *indexedMapToGdkPixbuf(IndexedMap *iMap) +{ + if (!iMap) + return nullptr; + + guchar *pixdata = (guchar *) + malloc(sizeof(guchar) * iMap->width * iMap->height * 3); + if (!pixdata) + { + g_warning("indexedMapToGdkPixbuf: can not allocate memory for conversion."); + return nullptr; + } + + int n_channels = 3; + int rowstride = iMap->width * 3; + + GdkPixbuf *buf = gdk_pixbuf_new_from_data(pixdata, GDK_COLORSPACE_RGB, + 0, 8, iMap->width, iMap->height, + rowstride, (GdkPixbufDestroyNotify)g_free, nullptr); + + //### Fill in the cells with RGB values + int x,y; + int row = 0; + for (y=0 ; yheight ; y++) + { + guchar *p = pixdata + row; + for (x=0 ; xwidth ; x++) + { + RGB rgb = iMap->getPixelValue(iMap, x, y); + p[0] = rgb.r & 0xff; + p[1] = rgb.g & 0xff; + p[2] = rgb.b & 0xff; + p += n_channels; + } + row += rowstride; + } + + return buf; +} + +/*######################################################################### +## E N D O F F I L E +#########################################################################*/ -- cgit v1.2.3