2008-04-25 13:37:36 +00:00
|
|
|
/*
|
2009-04-27 15:48:12 +01:00
|
|
|
* Cogl
|
2008-04-25 13:37:36 +00:00
|
|
|
*
|
2014-02-22 01:28:54 +00:00
|
|
|
* A Low Level GPU Graphics and Utilities API
|
2008-04-25 13:37:36 +00:00
|
|
|
*
|
2009-04-27 15:48:12 +01:00
|
|
|
* Copyright (C) 2007,2008,2009 Intel Corporation.
|
2008-04-25 13:37:36 +00:00
|
|
|
*
|
2014-02-22 01:28:54 +00:00
|
|
|
* Permission is hereby granted, free of charge, to any person
|
|
|
|
* obtaining a copy of this software and associated documentation
|
|
|
|
* files (the "Software"), to deal in the Software without
|
|
|
|
* restriction, including without limitation the rights to use, copy,
|
|
|
|
* modify, merge, publish, distribute, sublicense, and/or sell copies
|
|
|
|
* of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2008-04-25 13:37:36 +00:00
|
|
|
*
|
2014-02-22 01:28:54 +00:00
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
2008-04-25 13:37:36 +00:00
|
|
|
*
|
2014-02-22 01:28:54 +00:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
|
|
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
2010-03-01 12:56:10 +00:00
|
|
|
*
|
|
|
|
*
|
2008-04-25 13:37:36 +00:00
|
|
|
*/
|
|
|
|
|
2016-05-05 22:21:51 +08:00
|
|
|
#include "cogl-config.h"
|
2008-04-25 13:37:36 +00:00
|
|
|
|
2011-10-13 22:34:30 +01:00
|
|
|
#include "cogl-util.h"
|
2009-04-30 18:00:22 +01:00
|
|
|
#include "cogl-bitmap-private.h"
|
2012-03-13 14:46:18 +00:00
|
|
|
#include "cogl-context-private.h"
|
2012-05-09 19:43:06 +01:00
|
|
|
#include "cogl-private.h"
|
2008-04-25 13:37:36 +00:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <gdk-pixbuf/gdk-pixbuf.h>
|
|
|
|
|
2018-11-24 13:04:47 +01:00
|
|
|
gboolean
|
cogl: improves header and coding style consistency
We've had complaints that our Cogl code/headers are a bit "special" so
this is a first pass at tidying things up by giving them some
consistency. These changes are all consistent with how new code in Cogl
is being written, but the style isn't consistently applied across all
code yet.
There are two parts to this patch; but since each one required a large
amount of effort to maintain tidy indenting it made sense to combine the
changes to reduce the time spent re indenting the same lines.
The first change is to use a consistent style for declaring function
prototypes in headers. Cogl headers now consistently use this style for
prototypes:
return_type
cogl_function_name (CoglType arg0,
CoglType arg1);
Not everyone likes this style, but it seems that most of the currently
active Cogl developers agree on it.
The second change is to constrain the use of redundant glib data types
in Cogl. Uses of gint, guint, gfloat, glong, gulong and gchar have all
been replaced with int, unsigned int, float, long, unsigned long and char
respectively. When talking about pixel data; use of guchar has been
replaced with guint8, otherwise unsigned char can be used.
The glib types that we continue to use for portability are gboolean,
gint{8,16,32,64}, guint{8,16,32,64} and gsize.
The general intention is that Cogl should look palatable to the widest
range of C programmers including those outside the Gnome community so
- especially for the public API - we want to minimize the number of
foreign looking typedefs.
2010-02-10 01:57:32 +00:00
|
|
|
_cogl_bitmap_get_size_from_file (const char *filename,
|
|
|
|
int *width,
|
|
|
|
int *height)
|
2009-01-12 16:52:20 +00:00
|
|
|
{
|
2019-06-17 23:42:01 +02:00
|
|
|
g_return_val_if_fail (filename != NULL, FALSE);
|
2009-01-12 16:52:20 +00:00
|
|
|
|
|
|
|
if (gdk_pixbuf_get_file_info (filename, width, height) != NULL)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2010-07-07 18:44:16 +01:00
|
|
|
CoglBitmap *
|
2012-05-09 19:43:06 +01:00
|
|
|
_cogl_bitmap_from_file (CoglContext *ctx,
|
|
|
|
const char *filename,
|
2019-06-18 08:02:10 +02:00
|
|
|
GError **error)
|
2008-04-25 13:37:36 +00:00
|
|
|
{
|
2012-03-13 14:46:18 +00:00
|
|
|
static CoglUserDataKey pixbuf_key;
|
2012-08-31 19:28:27 +01:00
|
|
|
GdkPixbuf *pixbuf;
|
2018-11-24 13:04:47 +01:00
|
|
|
gboolean has_alpha;
|
2012-08-31 19:28:27 +01:00
|
|
|
GdkColorspace color_space;
|
|
|
|
CoglPixelFormat pixel_format;
|
|
|
|
int width;
|
|
|
|
int height;
|
|
|
|
int rowstride;
|
|
|
|
int bits_per_sample;
|
|
|
|
int n_channels;
|
|
|
|
CoglBitmap *bmp;
|
|
|
|
GError *glib_error = NULL;
|
2012-03-13 14:46:18 +00:00
|
|
|
|
2008-04-25 13:37:36 +00:00
|
|
|
/* Load from file using GdkPixbuf */
|
2012-08-31 19:28:27 +01:00
|
|
|
pixbuf = gdk_pixbuf_new_from_file (filename, &glib_error);
|
2009-01-12 16:52:20 +00:00
|
|
|
if (pixbuf == NULL)
|
2012-08-31 19:28:27 +01:00
|
|
|
{
|
2019-06-18 08:02:10 +02:00
|
|
|
g_propagate_error (error, glib_error);
|
2012-08-31 19:28:27 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
2009-04-27 15:48:12 +01:00
|
|
|
|
2008-04-25 13:37:36 +00:00
|
|
|
/* Get pixbuf properties */
|
|
|
|
has_alpha = gdk_pixbuf_get_has_alpha (pixbuf);
|
|
|
|
color_space = gdk_pixbuf_get_colorspace (pixbuf);
|
|
|
|
width = gdk_pixbuf_get_width (pixbuf);
|
|
|
|
height = gdk_pixbuf_get_height (pixbuf);
|
|
|
|
rowstride = gdk_pixbuf_get_rowstride (pixbuf);
|
|
|
|
bits_per_sample = gdk_pixbuf_get_bits_per_sample (pixbuf);
|
|
|
|
n_channels = gdk_pixbuf_get_n_channels (pixbuf);
|
2009-04-27 15:48:12 +01:00
|
|
|
|
2008-04-25 13:37:36 +00:00
|
|
|
/* According to current docs this should be true and so
|
|
|
|
* the translation to cogl pixel format below valid */
|
|
|
|
g_assert (bits_per_sample == 8);
|
|
|
|
|
|
|
|
if (has_alpha)
|
|
|
|
g_assert (n_channels == 4);
|
|
|
|
else
|
|
|
|
g_assert (n_channels == 3);
|
2009-04-27 15:48:12 +01:00
|
|
|
|
2008-04-25 13:37:36 +00:00
|
|
|
/* Translate to cogl pixel format */
|
|
|
|
switch (color_space)
|
|
|
|
{
|
|
|
|
case GDK_COLORSPACE_RGB:
|
|
|
|
/* The only format supported by GdkPixbuf so far */
|
|
|
|
pixel_format = has_alpha ?
|
|
|
|
COGL_PIXEL_FORMAT_RGBA_8888 :
|
|
|
|
COGL_PIXEL_FORMAT_RGB_888;
|
|
|
|
break;
|
2009-04-27 15:48:12 +01:00
|
|
|
|
2008-04-25 13:37:36 +00:00
|
|
|
default:
|
|
|
|
/* Ouch, spec changed! */
|
|
|
|
g_object_unref (pixbuf);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2009-04-27 15:48:12 +01:00
|
|
|
|
2010-12-17 16:30:23 +00:00
|
|
|
/* We just use the data directly from the pixbuf so that we don't
|
|
|
|
have to copy to a seperate buffer. Note that Cogl is expected not
|
|
|
|
to read past the end of bpp*width on the last row even if the
|
|
|
|
rowstride is much larger so we don't need to worry about
|
|
|
|
GdkPixbuf's semantics that it may under-allocate the buffer. */
|
2012-03-13 14:46:18 +00:00
|
|
|
bmp = cogl_bitmap_new_for_data (ctx,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
pixel_format,
|
|
|
|
rowstride,
|
|
|
|
gdk_pixbuf_get_pixels (pixbuf));
|
|
|
|
|
|
|
|
cogl_object_set_user_data (COGL_OBJECT (bmp),
|
|
|
|
&pixbuf_key,
|
|
|
|
pixbuf,
|
|
|
|
g_object_unref);
|
|
|
|
|
|
|
|
return bmp;
|
2008-04-25 13:37:36 +00:00
|
|
|
}
|