#include #include #include "../include/imlibio.h" #define IMLIBIO_FORMAT_BW_BITS 0 #define IMLIBIO_FORMAT_BW_BYTES 1 #define IMLIBIO_FORMAT_GREY 2 #define IMLIBIO_FORMAT_GREY_ALPHA 3 #define IMLIBIO_FORMAT_RGB 4 #define IMLIBIO_FORMAT_BGR 5 #define IMLIBIO_FORMAT_RGBA 6 #define IMLIBIO_FORMAT_BGRA 7 static guint8 *ImlibIOImlibImageToData( ImlibImage *imlib_image, gint target_format, gint *width, gint *height, gint *bpl, gint *bpp ); guint8 *ImlibIOLoadXPMDataGrey( gpointer imlib_handle, const gchar **data, gint *width, gint *height, gint *bpl ); guint8 *ImlibIOLoadXPMDataGreyAlpha( gpointer imlib_handle, const gchar **data, gint *width, gint *height, gint *bpl ); guint8 *ImlibIOLoadXPMDataRGB( gpointer imlib_handle, const gchar **data, gint *width, gint *height, gint *bpl ); guint8 *ImlibIOLoadXPMDataBGR( gpointer imlib_handle, const gchar **data, gint *width, gint *height, gint *bpl ); guint8 *ImlibIOLoadXPMDataRGBA( gpointer imlib_handle, const gchar **data, gint *width, gint *height, gint *bpl ); guint8 *ImlibIOLoadXPMDataBGRA( gpointer imlib_handle, const gchar **data, gint *width, gint *height, gint *bpl ); guint8 *ImlibIOLoadImageFileBW( gpointer imlib_handle, const gchar *path, gint *width, gint *height, gint *bpl ); guint8 *ImlibIOLoadImageFileGrey( gpointer imlib_handle, const gchar *path, gint *width, gint *height, gint *bpl ); guint8 *ImlibIOLoadImageFileGreyAlpha( gpointer imlib_handle, const gchar *path, gint *width, gint *height, gint *bpl ); guint8 *ImlibIOLoadImageFileRGB( gpointer imlib_handle, const gchar *path, gint *width, gint *height, gint *bpl ); guint8 *ImlibIOLoadImageFileBGR( gpointer imlib_handle, const gchar *path, gint *width, gint *height, gint *bpl ); guint8 *ImlibIOLoadImageFileRGBA( gpointer imlib_handle, const gchar *path, gint *width, gint *height, gint *bpl ); guint8 *ImlibIOLoadImageFileBGRA( gpointer imlib_handle, const gchar *path, gint *width, gint *height, gint *bpl ); /* * Allocates a return buffer depending on the specified * target_format containing the formatted data from the * imlib_image. */ static guint8 *ImlibIOImlibImageToData( ImlibImage *imlib_image, gint target_format, gint *width, gint *height, gint *bpl, gint *bpp ) { const guint8 *src_rgb = (const guint8 *)imlib_image->rgb_data, *src_alpha = (const guint8 *)imlib_image->alpha_data; guint8 *tar_buf = NULL; gint tbpp = 0, tbpl = 0; gint swidth = imlib_image->rgb_width, sheight = imlib_image->rgb_height, srgb_bpp = 3, srgb_bpl = swidth * srgb_bpp; guint8 default_alpha_byte = 0xff; guint8 bw_threshold_byte = 0x80; /* Must have source image RGB data. */ if((src_rgb == NULL) || (swidth <= 0) || (sheight <= 0)) return(tar_buf); /* Handle by target format. */ switch(target_format) { case IMLIBIO_FORMAT_BW_BITS: /* Need to work on this. */ break; case IMLIBIO_FORMAT_BW_BYTES: tbpp = 1; tbpl = swidth * tbpp; tar_buf = (guint8 *)g_malloc( tbpl * sheight * sizeof(guint8) ); if(tar_buf != NULL) { gint x, y; guint8 *tar_ptr; const guint8 *src_rgb_ptr; guint8 g; for(y = 0; y < sheight; y++) { for(x = 0; x < swidth; x++) { tar_ptr = &tar_buf[ (y * tbpl) + (x * tbpp) ]; src_rgb_ptr = &src_rgb[ (y * srgb_bpl) + (x * srgb_bpp) ]; g = (guint8)( ((gint)src_rgb_ptr[0] + (gint)src_rgb_ptr[1] + (gint)src_rgb_ptr[2]) / 3 ); *tar_ptr = (g >= bw_threshold_byte) ? 0xff : 0x00; } } } break; case IMLIBIO_FORMAT_GREY: tbpp = 1; tbpl = swidth * tbpp; tar_buf = (guint8 *)g_malloc( tbpl * sheight * sizeof(guint8) ); if(tar_buf != NULL) { gint x, y; guint8 *tar_ptr; const guint8 *src_rgb_ptr; for(y = 0; y < sheight; y++) { for(x = 0; x < swidth; x++) { tar_ptr = &tar_buf[ (y * tbpl) + (x * tbpp) ]; src_rgb_ptr = &src_rgb[ (y * srgb_bpl) + (x * srgb_bpp) ]; *tar_ptr = (guint8)( ((gint)src_rgb_ptr[0] + (gint)src_rgb_ptr[1] + (gint)src_rgb_ptr[2]) / 3 ); } } } break; case IMLIBIO_FORMAT_GREY_ALPHA: tbpp = 2; tbpl = swidth * tbpp; tar_buf = (guint8 *)g_malloc( tbpl * sheight * sizeof(guint8) ); if(tar_buf != NULL) { gint x, y; guint8 *tar_ptr; const guint8 *src_rgb_ptr, *src_alpha_ptr; for(y = 0; y < sheight; y++) { for(x = 0; x < swidth; x++) { tar_ptr = &tar_buf[ (y * tbpl) + (x * tbpp) ]; src_rgb_ptr = &src_rgb[ (y * srgb_bpl) + (x * srgb_bpp) ]; src_alpha_ptr = ((src_alpha != NULL) ? &src_alpha[y * x] : &default_alpha_byte ); *tar_ptr++ = (guint8)( ((gint)src_rgb_ptr[0] + (gint)src_rgb_ptr[1] + (gint)src_rgb_ptr[2]) / 3 ); *tar_ptr = *src_alpha_ptr; } } } break; case IMLIBIO_FORMAT_RGB: tbpp = 3; tbpl = swidth * tbpp; tar_buf = (guint8 *)g_malloc( tbpl * sheight * sizeof(guint8) ); if(tar_buf != NULL) { gint x, y; guint8 *tar_ptr; const guint8 *src_rgb_ptr; for(y = 0; y < sheight; y++) { for(x = 0; x < swidth; x++) { tar_ptr = &tar_buf[ (y * tbpl) + (x * tbpp) ]; src_rgb_ptr = &src_rgb[ (y * srgb_bpl) + (x * srgb_bpp) ]; tar_ptr[0] = src_rgb_ptr[0]; /* Red. */ tar_ptr[1] = src_rgb_ptr[1]; /* Green. */ tar_ptr[2] = src_rgb_ptr[2]; /* Blue. */ } } } break; case IMLIBIO_FORMAT_BGR: tbpp = 3; tbpl = swidth * tbpp; tar_buf = (guint8 *)g_malloc( tbpl * sheight * sizeof(guint8) ); if(tar_buf != NULL) { gint x, y; guint8 *tar_ptr; const guint8 *src_rgb_ptr; for(y = 0; y < sheight; y++) { for(x = 0; x < swidth; x++) { tar_ptr = &tar_buf[ (y * tbpl) + (x * tbpp) ]; src_rgb_ptr = &src_rgb[ (y * srgb_bpl) + (x * srgb_bpp) ]; tar_ptr[0] = src_rgb_ptr[2]; /* Blue. */ tar_ptr[1] = src_rgb_ptr[1]; /* Green. */ tar_ptr[2] = src_rgb_ptr[0]; /* Red. */ } } } break; case IMLIBIO_FORMAT_RGBA: tbpp = 4; tbpl = swidth * tbpp; tar_buf = (guint8 *)g_malloc( tbpl * sheight * sizeof(guint8) ); if(tar_buf != NULL) { gint x, y; guint8 *tar_ptr; const guint8 *src_rgb_ptr, *src_alpha_ptr; for(y = 0; y < sheight; y++) { for(x = 0; x < swidth; x++) { tar_ptr = &tar_buf[ (y * tbpl) + (x * tbpp) ]; src_rgb_ptr = &src_rgb[ (y * srgb_bpl) + (x * srgb_bpp) ]; src_alpha_ptr = ((src_alpha != NULL) ? &src_alpha[y * x] : &default_alpha_byte ); tar_ptr[0] = src_rgb_ptr[0]; /* Red. */ tar_ptr[1] = src_rgb_ptr[1]; /* Green. */ tar_ptr[2] = src_rgb_ptr[2]; /* Blue. */ tar_ptr[3] = *src_alpha_ptr; /* Alpha. */ } } } break; case IMLIBIO_FORMAT_BGRA: tbpp = 4; tbpl = swidth * tbpp; tar_buf = (guint8 *)g_malloc( tbpl * sheight * sizeof(guint8) ); if(tar_buf != NULL) { gint x, y; guint8 *tar_ptr; const guint8 *src_rgb_ptr, *src_alpha_ptr; for(y = 0; y < sheight; y++) { for(x = 0; x < swidth; x++) { tar_ptr = &tar_buf[ (y * tbpl) + (x * tbpp) ]; src_rgb_ptr = &src_rgb[ (y * srgb_bpl) + (x * srgb_bpp) ]; src_alpha_ptr = ((src_alpha != NULL) ? &src_alpha[y * x] : &default_alpha_byte ); tar_ptr[0] = src_rgb_ptr[2]; /* Blue. */ tar_ptr[1] = src_rgb_ptr[1]; /* Green. */ tar_ptr[2] = src_rgb_ptr[0]; /* Red. */ tar_ptr[3] = *src_alpha_ptr; /* Alpha. */ } } } break; } /* Update returns. */ if(width != NULL) *width = swidth; if(height != NULL) *height = sheight; if(bpl != NULL) *bpl = tbpl; if(bpp != NULL) *bpp = tbpp; return(tar_buf); } /* * Loads an image from XPM data into greyscale data, the bytes per * pixel is 1. * * The calling function must deallocate the returned image data. */ guint8 *ImlibIOLoadXPMDataGrey( gpointer imlib_handle, const gchar **data, gint *width, gint *height, gint *bpl ) { guint8 *tar_buf; ImlibImage *imlib_image; gint bpp; if(data == NULL) return(NULL); /* Load Imlib image. */ imlib_image = Imlib_create_image_from_xpm_data( imlib_handle, (gchar **)data ); if(imlib_image == NULL) return(NULL); /* Need to realize changes. */ Imlib_changed_image(imlib_handle, imlib_image); /* Get image buffer containing the data in the format that we * want. */ tar_buf = ImlibIOImlibImageToData( imlib_image, IMLIBIO_FORMAT_GREY, width, height, bpl, &bpp ); /* Destroy Imlib image, it is no longer needed. */ Imlib_destroy_image(imlib_handle, imlib_image); return(tar_buf); } /* * Loads an image from XPM data into greyscale with alpha data, * the bytes per pixel is 2. * * The calling function must deallocate the returned image data. */ guint8 *ImlibIOLoadXPMDataGreyAlpha( gpointer imlib_handle, const gchar **data, gint *width, gint *height, gint *bpl ) { guint8 *tar_buf; ImlibImage *imlib_image; gint bpp; if(data == NULL) return(NULL); /* Load Imlib image. */ imlib_image = Imlib_create_image_from_xpm_data( imlib_handle, (gchar **)data ); if(imlib_image == NULL) return(NULL); /* Need to realize changes. */ Imlib_changed_image(imlib_handle, imlib_image); /* Get image buffer containing the data in the format that we * want. */ tar_buf = ImlibIOImlibImageToData( imlib_image, IMLIBIO_FORMAT_GREY_ALPHA, width, height, bpl, &bpp ); /* Destroy Imlib image, it is no longer needed. */ Imlib_destroy_image(imlib_handle, imlib_image); return(tar_buf); } /* * Loads an image from XPM data into RGB (0xBBGGRR) data, the * bytes per pixel is 3. * * Ideal for use with GDK RGB buffers. * * The calling function must deallocate the returned image data. */ guint8 *ImlibIOLoadXPMDataRGB( gpointer imlib_handle, const gchar **data, gint *width, gint *height, gint *bpl ) { guint8 *tar_buf; ImlibImage *imlib_image; gint bpp; if(data == NULL) return(NULL); /* Load Imlib image. */ imlib_image = Imlib_create_image_from_xpm_data( imlib_handle, (gchar **)data ); if(imlib_image == NULL) return(NULL); /* Need to realize changes. */ Imlib_changed_image(imlib_handle, imlib_image); /* Get image buffer containing the data in the format that we * want. */ tar_buf = ImlibIOImlibImageToData( imlib_image, IMLIBIO_FORMAT_RGB, width, height, bpl, &bpp ); /* Destroy Imlib image, it is no longer needed. */ Imlib_destroy_image(imlib_handle, imlib_image); return(tar_buf); } /* * Loads an image from XPM data into BGR (0xRRGGBB) data, the * bytes per pixel is 3. * * The calling function must deallocate the returned image data. */ guint8 *ImlibIOLoadXPMDataBGR( gpointer imlib_handle, const gchar **data, gint *width, gint *height, gint *bpl ) { guint8 *tar_buf; ImlibImage *imlib_image; gint bpp; if(data == NULL) return(NULL); /* Load Imlib image. */ imlib_image = Imlib_create_image_from_xpm_data( imlib_handle, (gchar **)data ); if(imlib_image == NULL) return(NULL); /* Need to realize changes. */ Imlib_changed_image(imlib_handle, imlib_image); /* Get image buffer containing the data in the format that we * want. */ tar_buf = ImlibIOImlibImageToData( imlib_image, IMLIBIO_FORMAT_BGR, width, height, bpl, &bpp ); /* Destroy Imlib image, it is no longer needed. */ Imlib_destroy_image(imlib_handle, imlib_image); return(tar_buf); } /* * Loads an image from XPM data into RGBA (0xAABBGGRR) data, the * bytes per pixel is 4. * * Ideal for use with GDK RGB buffers. * * The calling function must deallocate the returned image data. */ guint8 *ImlibIOLoadXPMDataRGBA( gpointer imlib_handle, const gchar **data, gint *width, gint *height, gint *bpl ) { guint8 *tar_buf; ImlibImage *imlib_image; gint bpp; if(data == NULL) return(NULL); /* Load Imlib image. */ imlib_image = Imlib_create_image_from_xpm_data( imlib_handle, (gchar **)data ); if(imlib_image == NULL) return(NULL); /* Need to realize changes. */ Imlib_changed_image(imlib_handle, imlib_image); /* Get image buffer containing the data in the format that we * want. */ tar_buf = ImlibIOImlibImageToData( imlib_image, IMLIBIO_FORMAT_RGBA, width, height, bpl, &bpp ); /* Destroy Imlib image, it is no longer needed. */ Imlib_destroy_image(imlib_handle, imlib_image); return(tar_buf); } /* * Loads an image from XPM data into BGRA (0xAARRGGBB) data, the * bytes per pixel is 4. * * The calling function must deallocate the returned image data. */ guint8 *ImlibIOLoadXPMDataBGRA( gpointer imlib_handle, const gchar **data, gint *width, gint *height, gint *bpl ) { guint8 *tar_buf; ImlibImage *imlib_image; gint bpp; if(data == NULL) return(NULL); /* Load Imlib image. */ imlib_image = Imlib_create_image_from_xpm_data( imlib_handle, (gchar **)data ); if(imlib_image == NULL) return(NULL); /* Need to realize changes. */ Imlib_changed_image(imlib_handle, imlib_image); /* Get image buffer containing the data in the format that we * want. */ tar_buf = ImlibIOImlibImageToData( imlib_image, IMLIBIO_FORMAT_BGRA, width, height, bpl, &bpp ); /* Destroy Imlib image, it is no longer needed. */ Imlib_destroy_image(imlib_handle, imlib_image); return(tar_buf); } /* * Loads an image into black and white data, the bytes per pixel is * 1. * * The calling function must deallocate the returned image data. */ guint8 *ImlibIOLoadImageFileBW( gpointer imlib_handle, const gchar *path, gint *width, gint *height, gint *bpl ) { guint8 *tar_buf; gchar *dpath; ImlibImage *imlib_image; gint bpp; if(path == NULL) return(NULL); /* Copy path and load Imlib image. */ dpath = g_strdup(path); imlib_image = Imlib_load_image(imlib_handle, dpath); g_free(dpath); dpath = NULL; if(imlib_image == NULL) return(NULL); /* Need to realize changes. */ Imlib_changed_image(imlib_handle, imlib_image); /* Get image buffer containing the data in the format that we * want. */ tar_buf = ImlibIOImlibImageToData( imlib_image, IMLIBIO_FORMAT_BW_BYTES, width, height, bpl, &bpp ); /* Destroy Imlib image, it is no longer needed. */ Imlib_destroy_image(imlib_handle, imlib_image); return(tar_buf); } /* * Loads an image into greyscale data, the bytes per pixel is 1. * * The calling function must deallocate the returned image data. */ guint8 *ImlibIOLoadImageFileGrey( gpointer imlib_handle, const gchar *path, gint *width, gint *height, gint *bpl ) { guint8 *tar_buf; gchar *dpath; ImlibImage *imlib_image; gint bpp; if(path == NULL) return(NULL); /* Copy path and load Imlib image. */ dpath = g_strdup(path); imlib_image = Imlib_load_image(imlib_handle, dpath); g_free(dpath); dpath = NULL; if(imlib_image == NULL) return(NULL); /* Need to realize changes. */ Imlib_changed_image(imlib_handle, imlib_image); /* Get image buffer containing the data in the format that we * want. */ tar_buf = ImlibIOImlibImageToData( imlib_image, IMLIBIO_FORMAT_GREY, width, height, bpl, &bpp ); /* Destroy Imlib image, it is no longer needed. */ Imlib_destroy_image(imlib_handle, imlib_image); return(tar_buf); } /* * Loads an image into greyscale data with alpha channel, the bytes * per pixel is 2. * * The calling function must deallocate the returned image data. */ guint8 *ImlibIOLoadImageFileGreyAlpha( gpointer imlib_handle, const gchar *path, gint *width, gint *height, gint *bpl ) { guint8 *tar_buf; gchar *dpath; ImlibImage *imlib_image; gint bpp; if(path == NULL) return(NULL); /* Copy path and load Imlib image. */ dpath = g_strdup(path); imlib_image = Imlib_load_image(imlib_handle, dpath); g_free(dpath); dpath = NULL; if(imlib_image == NULL) return(NULL); /* Need to realize changes. */ Imlib_changed_image(imlib_handle, imlib_image); /* Get image buffer containing the data in the format that we * want. */ tar_buf = ImlibIOImlibImageToData( imlib_image, IMLIBIO_FORMAT_GREY_ALPHA, width, height, bpl, &bpp ); /* Destroy Imlib image, it is no longer needed. */ Imlib_destroy_image(imlib_handle, imlib_image); return(tar_buf); } /* * Loads an image into RGB (0xBBGGRR) data, the bytes per pixel * is 3. * * Ideal for use with GDK RGB buffers. * * The calling function must deallocate the returned image data. */ guint8 *ImlibIOLoadImageFileRGB( gpointer imlib_handle, const gchar *path, gint *width, gint *height, gint *bpl ) { guint8 *tar_buf; gchar *dpath; ImlibImage *imlib_image; gint bpp; if(path == NULL) return(NULL); /* Copy path and load Imlib image. */ dpath = g_strdup(path); imlib_image = Imlib_load_image(imlib_handle, dpath); g_free(dpath); dpath = NULL; if(imlib_image == NULL) return(NULL); /* Need to realize changes. */ Imlib_changed_image(imlib_handle, imlib_image); /* Get image buffer containing the data in the format that we * want. */ tar_buf = ImlibIOImlibImageToData( imlib_image, IMLIBIO_FORMAT_RGB, width, height, bpl, &bpp ); /* Destroy Imlib image, it is no longer needed. */ Imlib_destroy_image(imlib_handle, imlib_image); return(tar_buf); } /* * Loads an image into BGR (0xRRGGBB) data, the bytes per pixel * is 3. * * The calling function must deallocate the returned image data. */ guint8 *ImlibIOLoadImageFileBGR( gpointer imlib_handle, const gchar *path, gint *width, gint *height, gint *bpl ) { guint8 *tar_buf; gchar *dpath; ImlibImage *imlib_image; gint bpp; if(path == NULL) return(NULL); /* Copy path and load Imlib image. */ dpath = g_strdup(path); imlib_image = Imlib_load_image(imlib_handle, dpath); g_free(dpath); dpath = NULL; if(imlib_image == NULL) return(NULL); /* Need to realize changes. */ Imlib_changed_image(imlib_handle, imlib_image); /* Get image buffer containing the data in the format that we * want. */ tar_buf = ImlibIOImlibImageToData( imlib_image, IMLIBIO_FORMAT_BGR, width, height, bpl, &bpp ); /* Destroy Imlib image, it is no longer needed. */ Imlib_destroy_image(imlib_handle, imlib_image); return(tar_buf); } /* * Loads an image into RGBA (0xAABBGGRR) data, the bytes per pixel * is 4. * * Ideal for use with GDK RGB buffers. * * The calling function must deallocate the returned image data. */ guint8 *ImlibIOLoadImageFileRGBA( gpointer imlib_handle, const gchar *path, gint *width, gint *height, gint *bpl ) { guint8 *tar_buf; gchar *dpath; ImlibImage *imlib_image; gint bpp; if(path == NULL) return(NULL); /* Copy path and load Imlib image. */ dpath = g_strdup(path); imlib_image = Imlib_load_image(imlib_handle, dpath); g_free(dpath); dpath = NULL; if(imlib_image == NULL) return(NULL); /* Need to realize changes. */ Imlib_changed_image(imlib_handle, imlib_image); /* Get image buffer containing the data in the format that we * want. */ tar_buf = ImlibIOImlibImageToData( imlib_image, IMLIBIO_FORMAT_RGBA, width, height, bpl, &bpp ); /* Destroy Imlib image, it is no longer needed. */ Imlib_destroy_image(imlib_handle, imlib_image); return(tar_buf); } /* * Loads an image into BGRA (0xAARRGGBB) data, the bytes per pixel * is 4. * * The calling function must deallocate the returned image data. */ guint8 *ImlibIOLoadImageFileBGRA( gpointer imlib_handle, const gchar *path, gint *width, gint *height, gint *bpl ) { guint8 *tar_buf; gchar *dpath; ImlibImage *imlib_image; gint bpp; if(path == NULL) return(NULL); /* Copy path and load Imlib image. */ dpath = g_strdup(path); imlib_image = Imlib_load_image(imlib_handle, dpath); g_free(dpath); dpath = NULL; if(imlib_image == NULL) return(NULL); /* Need to realize changes. */ Imlib_changed_image(imlib_handle, imlib_image); /* Get image buffer containing the data in the format that we * want. */ tar_buf = ImlibIOImlibImageToData( imlib_image, IMLIBIO_FORMAT_BGRA, width, height, bpl, &bpp ); /* Destroy Imlib image, it is no longer needed. */ Imlib_destroy_image(imlib_handle, imlib_image); return(tar_buf); }