2008-04-25 09:37:36 -04:00
|
|
|
/*
|
2009-04-27 10:48:12 -04:00
|
|
|
* Cogl
|
2008-04-25 09:37:36 -04:00
|
|
|
*
|
2014-02-21 20:28:54 -05:00
|
|
|
* A Low Level GPU Graphics and Utilities API
|
2008-04-25 09:37:36 -04:00
|
|
|
*
|
2009-04-27 10:48:12 -04:00
|
|
|
* Copyright (C) 2007,2008,2009 Intel Corporation.
|
2008-04-25 09:37:36 -04:00
|
|
|
*
|
2014-02-21 20:28:54 -05: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 09:37:36 -04:00
|
|
|
*
|
2014-02-21 20:28:54 -05:00
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
2008-04-25 09:37:36 -04:00
|
|
|
*
|
2014-02-21 20:28:54 -05: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 07:56:10 -05:00
|
|
|
*
|
|
|
|
*
|
2008-04-25 09:37:36 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2011-10-13 17:34:30 -04:00
|
|
|
#include "cogl-util.h"
|
2009-04-30 13:00:22 -04:00
|
|
|
#include "cogl-bitmap-private.h"
|
2012-03-13 10:46:18 -04:00
|
|
|
#include "cogl-context-private.h"
|
2012-05-09 14:43:06 -04:00
|
|
|
#include "cogl-private.h"
|
2012-08-31 14:28:27 -04:00
|
|
|
#include "cogl-error-private.h"
|
2008-04-25 09:37:36 -04:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2008-06-09 17:15:12 -04:00
|
|
|
#ifdef USE_QUARTZ
|
|
|
|
#include <ApplicationServices/ApplicationServices.h>
|
|
|
|
#elif defined(USE_GDKPIXBUF)
|
2008-04-25 09:37:36 -04:00
|
|
|
#include <gdk-pixbuf/gdk-pixbuf.h>
|
|
|
|
#endif
|
|
|
|
|
2008-06-09 17:15:12 -04:00
|
|
|
#ifdef USE_QUARTZ
|
|
|
|
|
Switch use of primitive glib types to c99 equivalents
The coding style has for a long time said to avoid using redundant glib
data types such as gint or gchar etc because we feel that they make the
code look unnecessarily foreign to developers coming from outside of the
Gnome developer community.
Note: When we tried to find the historical rationale for the types we
just found that they were apparently only added for consistent syntax
highlighting which didn't seem that compelling.
Up until now we have been continuing to use some of the platform
specific type such as gint{8,16,32,64} and gsize but this patch switches
us over to using the standard c99 equivalents instead so we can further
ensure that our code looks familiar to the widest range of C developers
who might potentially contribute to Cogl.
So instead of using the gint{8,16,32,64} and guint{8,16,32,64} types this
switches all Cogl code to instead use the int{8,16,32,64}_t and
uint{8,16,32,64}_t c99 types instead.
Instead of gsize we now use size_t
For now we are not going to use the c99 _Bool type and instead we have
introduced a new CoglBool type to use instead of gboolean.
Reviewed-by: Neil Roberts <neil@linux.intel.com>
(cherry picked from commit 5967dad2400d32ca6319cef6cb572e81bf2c15f0)
2012-04-16 16:56:40 -04:00
|
|
|
CoglBool
|
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-09 20:57:32 -05:00
|
|
|
_cogl_bitmap_get_size_from_file (const char *filename,
|
|
|
|
int *width,
|
|
|
|
int *height)
|
2009-01-12 11:52:20 -05:00
|
|
|
{
|
|
|
|
if (width)
|
|
|
|
*width = 0;
|
|
|
|
|
|
|
|
if (height)
|
|
|
|
*height = 0;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2008-06-09 17:15:12 -04:00
|
|
|
/* the error does not contain the filename as the caller already has it */
|
2010-07-07 13:44:16 -04:00
|
|
|
CoglBitmap *
|
2012-05-09 14:43:06 -04:00
|
|
|
_cogl_bitmap_from_file (CoglContext *ctx,
|
|
|
|
const char *filename,
|
2012-08-31 14:28:27 -04:00
|
|
|
CoglError **error)
|
2008-06-09 17:15:12 -04:00
|
|
|
{
|
2011-02-15 07:31:12 -05:00
|
|
|
CFURLRef url;
|
|
|
|
CGImageSourceRef image_source;
|
|
|
|
CGImageRef image;
|
|
|
|
int save_errno;
|
|
|
|
CFStringRef type;
|
Switch use of primitive glib types to c99 equivalents
The coding style has for a long time said to avoid using redundant glib
data types such as gint or gchar etc because we feel that they make the
code look unnecessarily foreign to developers coming from outside of the
Gnome developer community.
Note: When we tried to find the historical rationale for the types we
just found that they were apparently only added for consistent syntax
highlighting which didn't seem that compelling.
Up until now we have been continuing to use some of the platform
specific type such as gint{8,16,32,64} and gsize but this patch switches
us over to using the standard c99 equivalents instead so we can further
ensure that our code looks familiar to the widest range of C developers
who might potentially contribute to Cogl.
So instead of using the gint{8,16,32,64} and guint{8,16,32,64} types this
switches all Cogl code to instead use the int{8,16,32,64}_t and
uint{8,16,32,64}_t c99 types instead.
Instead of gsize we now use size_t
For now we are not going to use the c99 _Bool type and instead we have
introduced a new CoglBool type to use instead of gboolean.
Reviewed-by: Neil Roberts <neil@linux.intel.com>
(cherry picked from commit 5967dad2400d32ca6319cef6cb572e81bf2c15f0)
2012-04-16 16:56:40 -04:00
|
|
|
size_t width, height, rowstride;
|
|
|
|
uint8_t *out_data;
|
2011-02-15 07:31:12 -05:00
|
|
|
CGColorSpaceRef color_space;
|
|
|
|
CGContextRef bitmap_context;
|
2012-03-21 09:34:32 -04:00
|
|
|
CoglBitmap *bmp;
|
|
|
|
|
2011-02-15 07:31:12 -05:00
|
|
|
url = CFURLCreateFromFileSystemRepresentation (NULL,
|
|
|
|
(guchar *) filename,
|
|
|
|
strlen (filename),
|
|
|
|
false);
|
|
|
|
image_source = CGImageSourceCreateWithURL (url, NULL);
|
|
|
|
save_errno = errno;
|
2008-06-09 17:15:12 -04:00
|
|
|
CFRelease (url);
|
2011-02-15 07:31:12 -05:00
|
|
|
|
2008-06-09 17:15:12 -04:00
|
|
|
if (image_source == NULL)
|
|
|
|
{
|
|
|
|
/* doesn't exist, not readable, etc. */
|
2012-08-31 14:28:27 -04:00
|
|
|
_cogl_set_error_literal (error,
|
|
|
|
COGL_BITMAP_ERROR,
|
|
|
|
COGL_BITMAP_ERROR_FAILED,
|
|
|
|
g_strerror (save_errno));
|
2010-07-07 13:44:16 -04:00
|
|
|
return NULL;
|
2008-06-09 17:15:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Unknown images would be cleanly caught as zero width/height below, but try
|
|
|
|
* to provide better error message
|
|
|
|
*/
|
2011-02-15 07:31:12 -05:00
|
|
|
type = CGImageSourceGetType (image_source);
|
2008-06-09 17:15:12 -04:00
|
|
|
if (type == NULL)
|
|
|
|
{
|
|
|
|
CFRelease (image_source);
|
2012-08-31 14:28:27 -04:00
|
|
|
_cogl_set_error_literal (error,
|
|
|
|
COGL_BITMAP_ERROR,
|
|
|
|
COGL_BITMAP_ERROR_UNKNOWN_TYPE,
|
|
|
|
"Unknown image type");
|
2010-07-07 13:44:16 -04:00
|
|
|
return NULL;
|
2008-06-09 17:15:12 -04:00
|
|
|
}
|
2011-02-15 07:31:12 -05:00
|
|
|
|
2008-06-09 17:15:12 -04:00
|
|
|
CFRelease (type);
|
|
|
|
|
2011-02-15 07:31:12 -05:00
|
|
|
image = CGImageSourceCreateImageAtIndex (image_source, 0, NULL);
|
2008-06-09 17:15:12 -04:00
|
|
|
CFRelease (image_source);
|
|
|
|
|
2011-02-15 07:31:12 -05:00
|
|
|
width = CGImageGetWidth (image);
|
|
|
|
height = CGImageGetHeight (image);
|
2008-06-09 17:15:12 -04:00
|
|
|
if (width == 0 || height == 0)
|
|
|
|
{
|
|
|
|
/* incomplete or corrupt */
|
|
|
|
CFRelease (image);
|
2012-08-31 14:28:27 -04:00
|
|
|
_cogl_set_error_literal (error,
|
|
|
|
COGL_BITMAP_ERROR,
|
|
|
|
COGL_BITMAP_ERROR_CORRUPT_IMAGE,
|
|
|
|
"Image has zero width or height");
|
2010-07-07 13:44:16 -04:00
|
|
|
return NULL;
|
2008-06-09 17:15:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* allocate buffer big enough to hold pixel data */
|
2012-03-21 09:34:32 -04:00
|
|
|
bmp = _cogl_bitmap_new_with_malloc_buffer (ctx,
|
|
|
|
width, height,
|
2013-03-28 09:57:11 -04:00
|
|
|
COGL_PIXEL_FORMAT_ARGB_8888,
|
|
|
|
error);
|
|
|
|
if (bmp == NULL)
|
|
|
|
{
|
|
|
|
CFRelease (image);
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-03-21 09:34:32 -04:00
|
|
|
rowstride = cogl_bitmap_get_rowstride (bmp);
|
|
|
|
out_data = _cogl_bitmap_map (bmp,
|
|
|
|
COGL_BUFFER_ACCESS_WRITE,
|
2013-03-28 09:57:11 -04:00
|
|
|
COGL_BUFFER_MAP_HINT_DISCARD,
|
|
|
|
error);
|
|
|
|
if (out_data == NULL)
|
|
|
|
{
|
|
|
|
cogl_object_unref (bmp);
|
|
|
|
CFRelease (image);
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-06-09 17:15:12 -04:00
|
|
|
|
|
|
|
/* render to buffer */
|
2011-02-15 07:31:12 -05:00
|
|
|
color_space = CGColorSpaceCreateWithName (kCGColorSpaceGenericRGB);
|
|
|
|
bitmap_context = CGBitmapContextCreate (out_data,
|
|
|
|
width, height, 8,
|
|
|
|
rowstride, color_space,
|
|
|
|
kCGImageAlphaPremultipliedFirst);
|
2008-06-09 17:15:12 -04:00
|
|
|
CGColorSpaceRelease (color_space);
|
|
|
|
|
2012-07-18 06:28:13 -04:00
|
|
|
CGContextSetBlendMode (bitmap_context, kCGBlendModeCopy);
|
|
|
|
|
2011-02-15 07:31:12 -05:00
|
|
|
{
|
|
|
|
const CGRect rect = {{0, 0}, {width, height}};
|
|
|
|
|
|
|
|
CGContextDrawImage (bitmap_context, rect, image);
|
|
|
|
}
|
2008-06-09 17:15:12 -04:00
|
|
|
|
|
|
|
CGImageRelease (image);
|
|
|
|
CGContextRelease (bitmap_context);
|
|
|
|
|
2012-03-21 09:34:32 -04:00
|
|
|
_cogl_bitmap_unmap (bmp);
|
|
|
|
|
2008-06-09 17:15:12 -04:00
|
|
|
/* store bitmap info */
|
2012-03-21 09:34:32 -04:00
|
|
|
return bmp;
|
2008-06-09 17:15:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(USE_GDKPIXBUF)
|
2008-04-25 09:37:36 -04:00
|
|
|
|
Switch use of primitive glib types to c99 equivalents
The coding style has for a long time said to avoid using redundant glib
data types such as gint or gchar etc because we feel that they make the
code look unnecessarily foreign to developers coming from outside of the
Gnome developer community.
Note: When we tried to find the historical rationale for the types we
just found that they were apparently only added for consistent syntax
highlighting which didn't seem that compelling.
Up until now we have been continuing to use some of the platform
specific type such as gint{8,16,32,64} and gsize but this patch switches
us over to using the standard c99 equivalents instead so we can further
ensure that our code looks familiar to the widest range of C developers
who might potentially contribute to Cogl.
So instead of using the gint{8,16,32,64} and guint{8,16,32,64} types this
switches all Cogl code to instead use the int{8,16,32,64}_t and
uint{8,16,32,64}_t c99 types instead.
Instead of gsize we now use size_t
For now we are not going to use the c99 _Bool type and instead we have
introduced a new CoglBool type to use instead of gboolean.
Reviewed-by: Neil Roberts <neil@linux.intel.com>
(cherry picked from commit 5967dad2400d32ca6319cef6cb572e81bf2c15f0)
2012-04-16 16:56:40 -04:00
|
|
|
CoglBool
|
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-09 20:57:32 -05:00
|
|
|
_cogl_bitmap_get_size_from_file (const char *filename,
|
|
|
|
int *width,
|
|
|
|
int *height)
|
2009-01-12 11:52:20 -05:00
|
|
|
{
|
2011-10-13 17:34:30 -04:00
|
|
|
_COGL_RETURN_VAL_IF_FAIL (filename != NULL, FALSE);
|
2009-01-12 11:52:20 -05:00
|
|
|
|
|
|
|
if (gdk_pixbuf_get_file_info (filename, width, height) != NULL)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2010-07-07 13:44:16 -04:00
|
|
|
CoglBitmap *
|
2012-05-09 14:43:06 -04:00
|
|
|
_cogl_bitmap_from_file (CoglContext *ctx,
|
|
|
|
const char *filename,
|
2012-08-31 14:28:27 -04:00
|
|
|
CoglError **error)
|
2008-04-25 09:37:36 -04:00
|
|
|
{
|
2012-03-13 10:46:18 -04:00
|
|
|
static CoglUserDataKey pixbuf_key;
|
2012-08-31 14:28:27 -04:00
|
|
|
GdkPixbuf *pixbuf;
|
|
|
|
CoglBool has_alpha;
|
|
|
|
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 10:46:18 -04:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Load from file using GdkPixbuf */
|
2012-08-31 14:28:27 -04:00
|
|
|
pixbuf = gdk_pixbuf_new_from_file (filename, &glib_error);
|
2009-01-12 11:52:20 -05:00
|
|
|
if (pixbuf == NULL)
|
2012-08-31 14:28:27 -04:00
|
|
|
{
|
2012-11-19 15:14:55 -05:00
|
|
|
_cogl_propagate_gerror (error, glib_error);
|
2012-08-31 14:28:27 -04:00
|
|
|
return FALSE;
|
|
|
|
}
|
2009-04-27 10:48:12 -04:00
|
|
|
|
2008-04-25 09:37:36 -04: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 10:48:12 -04:00
|
|
|
|
2008-04-25 09:37:36 -04: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 10:48:12 -04:00
|
|
|
|
2008-04-25 09:37:36 -04: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 10:48:12 -04:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
default:
|
|
|
|
/* Ouch, spec changed! */
|
|
|
|
g_object_unref (pixbuf);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2009-04-27 10:48:12 -04:00
|
|
|
|
2010-12-17 11:30:23 -05: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 10:46:18 -04: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 09:37:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#include "stb_image.c"
|
|
|
|
|
Switch use of primitive glib types to c99 equivalents
The coding style has for a long time said to avoid using redundant glib
data types such as gint or gchar etc because we feel that they make the
code look unnecessarily foreign to developers coming from outside of the
Gnome developer community.
Note: When we tried to find the historical rationale for the types we
just found that they were apparently only added for consistent syntax
highlighting which didn't seem that compelling.
Up until now we have been continuing to use some of the platform
specific type such as gint{8,16,32,64} and gsize but this patch switches
us over to using the standard c99 equivalents instead so we can further
ensure that our code looks familiar to the widest range of C developers
who might potentially contribute to Cogl.
So instead of using the gint{8,16,32,64} and guint{8,16,32,64} types this
switches all Cogl code to instead use the int{8,16,32,64}_t and
uint{8,16,32,64}_t c99 types instead.
Instead of gsize we now use size_t
For now we are not going to use the c99 _Bool type and instead we have
introduced a new CoglBool type to use instead of gboolean.
Reviewed-by: Neil Roberts <neil@linux.intel.com>
(cherry picked from commit 5967dad2400d32ca6319cef6cb572e81bf2c15f0)
2012-04-16 16:56:40 -04:00
|
|
|
CoglBool
|
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-09 20:57:32 -05:00
|
|
|
_cogl_bitmap_get_size_from_file (const char *filename,
|
|
|
|
int *width,
|
|
|
|
int *height)
|
2009-01-12 11:52:20 -05:00
|
|
|
{
|
|
|
|
if (width)
|
|
|
|
*width = 0;
|
|
|
|
|
|
|
|
if (height)
|
|
|
|
*height = 0;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2012-05-09 14:43:06 -04:00
|
|
|
/* stb_image.c supports an STBI_grey_alpha format which we don't have
|
|
|
|
* a corresponding CoglPixelFormat for so as a special case we
|
|
|
|
* convert this to rgba8888.
|
|
|
|
*
|
|
|
|
* If we have a use case where this is an important format to consider
|
|
|
|
* then it could be worth adding a corresponding CoglPixelFormat
|
|
|
|
* instead.
|
|
|
|
*/
|
|
|
|
static uint8_t *
|
|
|
|
convert_ra_88_to_rgba_8888 (uint8_t *pixels,
|
|
|
|
int width,
|
|
|
|
int height)
|
|
|
|
{
|
|
|
|
int x, y;
|
|
|
|
uint8_t *buf;
|
|
|
|
size_t in_stride = width * 2;
|
|
|
|
size_t out_stride = width * 4;
|
|
|
|
|
|
|
|
buf = malloc (width * height * 4);
|
|
|
|
if (buf)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for (y = 0; y < height; y++)
|
|
|
|
for (x = 0; x < width; x++)
|
|
|
|
{
|
|
|
|
uint8_t *src = pixels + in_stride * y + 2 * x;
|
|
|
|
uint8_t *dst = buf + out_stride * y + 4 * x;
|
|
|
|
|
|
|
|
dst[0] = src[0];
|
|
|
|
dst[1] = src[0];
|
|
|
|
dst[2] = src[0];
|
|
|
|
dst[3] = src[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CoglBitmap *
|
|
|
|
_cogl_bitmap_new_from_stb_pixels (CoglContext *ctx,
|
|
|
|
uint8_t *pixels,
|
|
|
|
int stb_pixel_format,
|
|
|
|
int width,
|
|
|
|
int height,
|
2012-08-31 14:28:27 -04:00
|
|
|
CoglError **error)
|
2008-04-25 09:37:36 -04:00
|
|
|
{
|
2012-03-20 09:53:50 -04:00
|
|
|
static CoglUserDataKey bitmap_data_key;
|
2010-07-07 13:44:16 -04:00
|
|
|
CoglBitmap *bmp;
|
2012-05-09 14:43:06 -04:00
|
|
|
CoglPixelFormat cogl_format;
|
|
|
|
size_t stride;
|
2008-04-25 09:37:36 -04:00
|
|
|
|
2012-05-09 14:43:06 -04:00
|
|
|
if (pixels == NULL)
|
|
|
|
{
|
2012-08-31 14:28:27 -04:00
|
|
|
_cogl_set_error_literal (error,
|
|
|
|
COGL_BITMAP_ERROR,
|
|
|
|
COGL_BITMAP_ERROR_FAILED,
|
|
|
|
"Failed to load image with stb image library");
|
2012-05-09 14:43:06 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
2012-03-20 09:53:50 -04:00
|
|
|
|
2012-05-09 14:43:06 -04:00
|
|
|
switch (stb_pixel_format)
|
|
|
|
{
|
|
|
|
case STBI_grey:
|
|
|
|
cogl_format = COGL_PIXEL_FORMAT_A_8;
|
|
|
|
break;
|
|
|
|
case STBI_grey_alpha:
|
|
|
|
{
|
|
|
|
uint8_t *tmp = pixels;
|
|
|
|
|
|
|
|
pixels = convert_ra_88_to_rgba_8888 (pixels, width, height);
|
|
|
|
free (tmp);
|
|
|
|
|
|
|
|
if (!pixels)
|
|
|
|
{
|
2012-08-31 14:28:27 -04:00
|
|
|
_cogl_set_error_literal (error,
|
|
|
|
COGL_BITMAP_ERROR,
|
|
|
|
COGL_BITMAP_ERROR_FAILED,
|
|
|
|
"Failed to alloc memory to convert "
|
|
|
|
"gray_alpha to rgba8888");
|
2012-05-09 14:43:06 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
cogl_format = COGL_PIXEL_FORMAT_RGBA_8888;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case STBI_rgb:
|
|
|
|
cogl_format = COGL_PIXEL_FORMAT_RGB_888;
|
|
|
|
break;
|
|
|
|
case STBI_rgb_alpha:
|
|
|
|
cogl_format = COGL_PIXEL_FORMAT_RGBA_8888;
|
|
|
|
break;
|
2008-04-25 09:37:36 -04:00
|
|
|
|
2012-05-09 14:43:06 -04:00
|
|
|
default:
|
|
|
|
g_warn_if_reached ();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
stride = width * _cogl_pixel_format_get_bytes_per_pixel (cogl_format);
|
2009-04-27 10:48:12 -04:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Store bitmap info */
|
2012-03-20 09:53:50 -04:00
|
|
|
bmp = cogl_bitmap_new_for_data (ctx,
|
|
|
|
width, height,
|
2012-05-09 14:43:06 -04:00
|
|
|
cogl_format,
|
|
|
|
stride,
|
2012-03-20 09:53:50 -04:00
|
|
|
pixels);
|
2012-05-09 14:43:06 -04:00
|
|
|
|
2012-03-20 09:53:50 -04:00
|
|
|
/* Register a destroy function so the pixel data will be freed
|
|
|
|
automatically when the bitmap object is destroyed */
|
|
|
|
cogl_object_set_user_data (COGL_OBJECT (bmp), &bitmap_data_key, pixels, free);
|
2008-08-27 08:56:54 -04:00
|
|
|
|
2010-07-07 13:44:16 -04:00
|
|
|
return bmp;
|
2008-04-25 09:37:36 -04:00
|
|
|
}
|
2012-05-09 14:43:06 -04:00
|
|
|
|
|
|
|
CoglBitmap *
|
|
|
|
_cogl_bitmap_from_file (CoglContext *ctx,
|
|
|
|
const char *filename,
|
2012-08-31 14:28:27 -04:00
|
|
|
CoglError **error)
|
2012-05-09 14:43:06 -04:00
|
|
|
{
|
|
|
|
int stb_pixel_format;
|
|
|
|
int width;
|
|
|
|
int height;
|
|
|
|
uint8_t *pixels;
|
|
|
|
|
|
|
|
pixels = stbi_load (filename,
|
|
|
|
&width, &height, &stb_pixel_format,
|
|
|
|
STBI_default);
|
|
|
|
|
|
|
|
return _cogl_bitmap_new_from_stb_pixels (ctx, pixels, stb_pixel_format,
|
|
|
|
width, height,
|
|
|
|
error);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef COGL_HAS_ANDROID_SUPPORT
|
|
|
|
CoglBitmap *
|
|
|
|
_cogl_android_bitmap_new_from_asset (CoglContext *ctx,
|
|
|
|
AAssetManager *manager,
|
|
|
|
const char *filename,
|
2012-08-31 14:28:27 -04:00
|
|
|
CoglError **error)
|
2012-05-09 14:43:06 -04:00
|
|
|
{
|
|
|
|
AAsset *asset;
|
|
|
|
const void *data;
|
|
|
|
off_t len;
|
|
|
|
int stb_pixel_format;
|
|
|
|
int width;
|
|
|
|
int height;
|
|
|
|
uint8_t *pixels;
|
|
|
|
CoglBitmap *bmp;
|
|
|
|
|
|
|
|
asset = AAssetManager_open (manager, filename, AASSET_MODE_BUFFER);
|
|
|
|
if (!asset)
|
|
|
|
{
|
2012-08-31 14:28:27 -04:00
|
|
|
_cogl_set_error_literal (error,
|
|
|
|
COGL_BITMAP_ERROR,
|
|
|
|
COGL_BITMAP_ERROR_FAILED,
|
|
|
|
"Failed to open asset");
|
2012-05-09 14:43:06 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
data = AAsset_getBuffer (asset);
|
|
|
|
if (!data)
|
|
|
|
{
|
2012-08-31 14:28:27 -04:00
|
|
|
_cogl_set_error_literal (error,
|
|
|
|
COGL_BITMAP_ERROR,
|
|
|
|
COGL_BITMAP_ERROR_FAILED,
|
|
|
|
"Failed to ::getBuffer from asset");
|
2012-05-09 14:43:06 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = AAsset_getLength (asset);
|
|
|
|
|
|
|
|
pixels = stbi_load_from_memory (data, len,
|
|
|
|
&width, &height,
|
|
|
|
&stb_pixel_format, STBI_default);
|
|
|
|
|
|
|
|
bmp = _cogl_bitmap_new_from_stb_pixels (ctx, pixels, stb_pixel_format,
|
|
|
|
width, height,
|
|
|
|
error);
|
|
|
|
|
|
|
|
AAsset_close (asset);
|
|
|
|
|
|
|
|
return bmp;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
#endif
|