CoglHandle cogl_texture_new_from_file (const char *filename, CoglTextureFlags flags, CoglPixelFormat internal_format, GError **error) { CoglBitmap *bmp; CoglHandle handle = COGL_INVALID_HANDLE; CoglPixelFormat src_format; g_return_val_if_fail (error == NULL || *error == NULL, COGL_INVALID_HANDLE); bmp = cogl_bitmap_new_from_file (filename, error); if (bmp == NULL) return COGL_INVALID_HANDLE; src_format = _cogl_bitmap_get_format (bmp); /* We know that the bitmap data is solely owned by this function so we can do the premult conversion in place. This avoids having to copy the bitmap which will otherwise happen in _cogl_texture_prepare_for_upload */ internal_format = _cogl_texture_determine_internal_format (src_format, internal_format); if (!_cogl_texture_needs_premult_conversion (src_format, internal_format) || _cogl_bitmap_convert_premult_status (bmp, src_format ^ COGL_PREMULT_BIT)) handle = cogl_texture_new_from_bitmap (bmp, flags, internal_format); cogl_object_unref (bmp); return handle; }
CoglTexture * cogl_texture_new_from_file (CoglContext *ctx, const char *filename, CoglTextureFlags flags, CoglPixelFormat internal_format, CoglError **error) { CoglBitmap *bmp; CoglTexture *texture = NULL; CoglPixelFormat src_format; _COGL_RETURN_VAL_IF_FAIL (error == NULL || *error == NULL, NULL); bmp = cogl_bitmap_new_from_file (ctx, filename, error); if (bmp == NULL) return NULL; src_format = cogl_bitmap_get_format (bmp); /* We know that the bitmap data is solely owned by this function so we can do the premult conversion in place. This avoids having to copy the bitmap which will otherwise happen in _cogl_texture_prepare_for_upload */ internal_format = _cogl_texture_determine_internal_format (src_format, internal_format); if (!_cogl_texture_needs_premult_conversion (src_format, internal_format) || _cogl_bitmap_convert_premult_status (bmp, src_format ^ COGL_PREMULT_BIT, error)) { texture = cogl_texture_new_from_bitmap (bmp, flags, internal_format, error); } cogl_object_unref (bmp); return texture; }
CoglBitmap * _cogl_texture_prepare_for_upload (CoglBitmap *src_bmp, CoglPixelFormat dst_format, CoglPixelFormat *dst_format_out, GLenum *out_glintformat, GLenum *out_glformat, GLenum *out_gltype) { CoglPixelFormat src_format = _cogl_bitmap_get_format (src_bmp); CoglBitmap *dst_bmp; dst_format = _cogl_texture_determine_internal_format (src_format, dst_format); /* OpenGL supports specifying a different format for the internal format when uploading texture data. We should use this to convert formats because it is likely to be faster and support more types than the Cogl bitmap code. However under GLES the internal format must be the same as the bitmap format and it only supports a limited number of formats so we must convert using the Cogl bitmap code instead */ #ifdef HAVE_COGL_GL /* If the source format does not have the same premult flag as the dst format then we need to copy and convert it */ if (_cogl_texture_needs_premult_conversion (src_format, dst_format)) { dst_bmp = _cogl_bitmap_copy (src_bmp); if (!_cogl_bitmap_convert_premult_status (dst_bmp, src_format ^ COGL_PREMULT_BIT)) { cogl_object_unref (dst_bmp); return NULL; } } else dst_bmp = cogl_object_ref (src_bmp); /* Use the source format from the src bitmap type and the internal format from the dst format type so that GL can do the conversion */ _cogl_pixel_format_to_gl (src_format, NULL, /* internal format */ out_glformat, out_gltype); _cogl_pixel_format_to_gl (dst_format, out_glintformat, NULL, NULL); #else /* HAVE_COGL_GL */ { CoglPixelFormat closest_format; closest_format = _cogl_pixel_format_to_gl (dst_format, out_glintformat, out_glformat, out_gltype); if (closest_format != src_format) dst_bmp = _cogl_bitmap_convert_format_and_premult (src_bmp, closest_format); else dst_bmp = cogl_object_ref (src_bmp); } #endif /* HAVE_COGL_GL */ if (dst_format_out) *dst_format_out = dst_format; return dst_bmp; }