argb format loader & saver. my own format just so i can load and save raw ARGB

data blindingly fast for imlib2 :)


SVN revision: 1136
This commit is contained in:
Carsten Haitzler 1999-11-02 10:16:50 +00:00
parent 72ed61340b
commit 3a0b11b007
5 changed files with 236 additions and 5 deletions

View File

@ -12,7 +12,7 @@ INCLUDES = -I/usr/X11R6/include -I$(top_srcdir)/libltdl \
-I$(top_srcdir)/loaders
pkgdir = $(libdir)/loaders/image
pkg_LTLIBRARIES = png.la jpeg.la pnm.la
pkg_LTLIBRARIES = png.la jpeg.la pnm.la argb.la
png_la_SOURCES = loader_png.c
png_la_LDFLAGS = -no-undefined -module -avoid-version
@ -25,3 +25,7 @@ jpeg_la_LIBADD = -ljpeg
pnm_la_SOURCES = loader_pnm.c
pnm_la_LDFLAGS = -no-undefined -module -avoid-version
pnm_la_LIBADD =
argb_la_SOURCES = loader_argb.c
argb_la_LDFLAGS = -no-undefined -module -avoid-version
argb_la_LIBADD =

199
loaders/loader_argb.c Normal file
View File

@ -0,0 +1,199 @@
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "common.h"
#include <string.h>
#include <X11/Xlib.h>
#include <X11/extensions/XShm.h>
#include <X11/Xutil.h>
#include "image.h"
char load (ImlibImage *im,
void (*progress)(ImlibImage *im, char percent,
int update_x, int update_y,
int update_w, int update_h),
char progress_granularity, char immediate_load);
char save (ImlibImage *im,
void (*progress)(ImlibImage *im, char percent,
int update_x, int update_y,
int update_w, int update_h),
char progress_granularity);
void formats (ImlibLoader *l);
#define SWAP32(x) (x) = \
((((x) & 0x000000ff ) << 24) |\
(((x) & 0x0000ff00 ) << 8) |\
(((x) & 0x00ff0000 ) >> 8) |\
(((x) & 0xff000000 ) >> 24))
char
load (ImlibImage *im,
void (*progress)(ImlibImage *im, char percent,
int update_x, int update_y,
int update_w, int update_h),
char progress_granularity, char immediate_load)
{
int w, h, alpha;
FILE *f;
if (im->data)
return 0;
f = fopen(im->file, "rb");
if (!f)
return 0;
/* header */
{
char buf[256], buf2[256];
if (!fgets(buf, 255, f))
{
fclose(f);
return 0;
}
sscanf(buf, "%s %i %i %i", buf2, &w, &h, &alpha);
if (strcmp(buf2, "ARGB"))
{
fclose(f);
return 0;
}
im->w = w;
im->h = h;
if (!im->format)
{
if (alpha)
SET_FLAG(im->flags, F_HAS_ALPHA);
else
UNSET_FLAG(im->flags, F_HAS_ALPHA);
im->format = strdup("argb");
}
}
if (((!im->data) && (im->loader)) || (immediate_load) || (progress))
{
DATA32 *ptr;
int y, pl = 0;
char pper = 0;
/* must set the im->data member before callign progress function */
ptr = im->data = malloc(w * h * sizeof(DATA32));
if (!im->data)
{
fclose(f);
return 0;
}
for (y = 0; y < h; y++)
{
#ifdef WORDS_BIGENDIAN
{
int x;
fread(ptr, im->w, 4, f);
for (x = 0; x < im->w; x++)
SWAP32(ptr[x]);
}
#else
fread(ptr, im->w, 4, f);
#endif
ptr += im->w;
if (progress)
{
char per;
int l;
per = (char)((100 * y) / im->h);
if ((per - pper) >= progress_granularity)
{
l = y - pl;
progress(im, per, 0, (y - l), im->w, l);
pper = per;
pl = y;
}
}
}
}
fclose(f);
return 1;
}
char
save (ImlibImage *im,
void (*progress)(ImlibImage *im, char percent,
int update_x, int update_y,
int update_w, int update_h),
char progress_granularity)
{
FILE *f;
DATA32 *ptr;
int y, pl = 0, alpha = 0;
char pper = 0;
#ifdef WORDS_BIGENDIAN
DATA32 *buf = NULL;
#endif
/* no image data? abort */
if (!im->data)
return 0;
f = fopen(im->file, "wb");
if (!f)
return 0;
if (im->flags & F_HAS_ALPHA)
alpha = 1;
fprintf(f, "ARGB %i %i %i\n",
im->w, im->h, alpha);
ptr = im->data;
for (y = 0; y < im->h; y++)
{
#ifdef WORDS_BIGENDIAN
{
int x;
memcpy(buf, ptr, im->w * 4);
for (x = 0; x < im->w; x++)
SWAP32(buf[x]);
fwrite(buf, im->w, 4, f);
}
#else
fwrite(ptr, im->w, 4, f);
#endif
ptr += im->w;
if (progress)
{
char per;
int l;
per = (char)((100 * y) / im->h);
if ((per - pper) >= progress_granularity)
{
l = y - pl;
progress(im, per, 0, (y - l), im->w, l);
pper = per;
pl = y;
}
}
}
/* finish off */
#ifdef WORDS_BIGENDIAN
if (buf)
free(buf);
#endif
fclose(f);
return 1;
}
void
formats (ImlibLoader *l)
{
char *list_formats[] =
{ "argb", "arg" };
{
int i;
l->num_formats = (sizeof(list_formats) / sizeof (char *));
l->formats = malloc(sizeof(char *) * l->num_formats);
for (i = 0; i < l->num_formats; i++)
l->formats[i] = strdup(list_formats[i]);
}
}

View File

@ -39,7 +39,6 @@ load (ImlibImage *im,
if (!f)
return 0;
if (!im->data)
{
char buf[256], buf2[256];
/* read the header info */
@ -127,9 +126,14 @@ load (ImlibImage *im,
}
im->w = w;
im->h = h;
if (p == '8')
UNSET_FLAG(im->flags, F_HAS_ALPHA);
im->format = strdup("pnm");
if (!im->format)
{
if (p == '8')
SET_FLAG(im->flags, F_HAS_ALPHA);
else
UNSET_FLAG(im->flags, F_HAS_ALPHA);
im->format = strdup("pnm");
}
}
if (((!im->data) && (im->loader)) || (immediate_load) || (progress))

View File

@ -1807,6 +1807,8 @@ imlib_image_attach_data_value(Imlib_Image image, char *key,
{
ImlibImage *im;
CHECK_PARAM_POINTER("imlib_image_attach_data_value", "image", image);
CHECK_PARAM_POINTER("imlib_image_attach_data_value", "key", key);
CAST_IMAGE(im, image);
__imlib_AttachTag(im, key, value, data, destructor_function);
}
@ -1817,6 +1819,8 @@ imlib_image_get_attached_data(Imlib_Image image, char *key)
ImlibImageTag *t;
ImlibImage *im;
CHECK_PARAM_POINTER_RETURN("imlib_image_get_attached_data", "image", image, NULL);
CHECK_PARAM_POINTER_RETURN("imlib_image_get_attached_data", "key", key, NULL);
CAST_IMAGE(im, image);
t = __imlib_GetTag(im, key);
if (t)
@ -1830,6 +1834,8 @@ imlib_image_get_attached_value(Imlib_Image image, char *key)
ImlibImageTag *t;
ImlibImage *im;
CHECK_PARAM_POINTER_RETURN("imlib_image_get_attached_value", "image", image, 0);
CHECK_PARAM_POINTER_RETURN("imlib_image_get_attached_value", "key", key, 0);
CAST_IMAGE(im, image);
t = __imlib_GetTag(im, key);
if (t)
@ -1842,6 +1848,8 @@ imlib_image_remove_attached_data_value(Imlib_Image image, char *key)
{
ImlibImage *im;
CHECK_PARAM_POINTER("imlib_image_remove_attached_data_value", "image", image);
CHECK_PARAM_POINTER("imlib_image_remove_attached_data_value", "key", key);
CAST_IMAGE(im, image);
__imlib_RemoveTag(im, key);
}
@ -1852,6 +1860,8 @@ imlib_image_remove_and_free_attached_data_value(Imlib_Image image, char *key)
ImlibImageTag *t;
ImlibImage *im;
CHECK_PARAM_POINTER("imlib_image_remove_and_free_attached_data_value", "image", image);
CHECK_PARAM_POINTER("imlib_image_remove_and_free_attached_data_value", "key", key);
CAST_IMAGE(im, image);
t = __imlib_RemoveTag(im, key);
__imlib_FreeTag(im, t);
@ -1862,6 +1872,8 @@ imlib_save_image(Imlib_Image image, char *filename)
{
ImlibImage *im;
CHECK_PARAM_POINTER("imlib_save_image", "image", image);
CHECK_PARAM_POINTER("imlib_save_image", "filename", filename);
CAST_IMAGE(im, image);
if ((!(im->data)) && (im->loader) && (im->loader->load))
im->loader->load(im, NULL, 0, 1);
@ -1875,6 +1887,9 @@ imlib_save_image_with_progress_callback(Imlib_Image image, char *filename,
{
ImlibImage *im;
CHECK_PARAM_POINTER("imlib_save_image_with_progress_callback", "image", image);
CHECK_PARAM_POINTER("imlib_save_image_with_progress_callback", "filename", filename);
CHECK_PARAM_POINTER("imlib_save_image_with_progress_callback", "progress_function", progress_function);
CAST_IMAGE(im, image);
if ((!(im->data)) && (im->loader) && (im->loader->load))
im->loader->load(im, NULL, 0, 1);
@ -1888,6 +1903,9 @@ imlib_save_image_with_error_return(Imlib_Image image, char *filename,
{
ImlibImage *im;
CHECK_PARAM_POINTER("imlib_save_image_with_error_return", "image", image);
CHECK_PARAM_POINTER("imlib_save_image_with_error_return", "filename", filename);
CHECK_PARAM_POINTER("imlib_save_image_with_error_return", "error_return", error_return);
CAST_IMAGE(im, image);
if ((!(im->data)) && (im->loader) && (im->loader->load))
im->loader->load(im, NULL, 0, 1);
@ -1903,6 +1921,10 @@ imlib_save_image_with_progress_callback_and_error_return(Imlib_Image image,
{
ImlibImage *im;
CHECK_PARAM_POINTER("imlib_save_image_with_progress_callback_and_error_return", "image", image);
CHECK_PARAM_POINTER("imlib_save_image_with_progress_callback_and_error_return", "filename", filename);
CHECK_PARAM_POINTER("imlib_save_image_with_progress_callback_and_error_return", "progress_function", progress_function);
CHECK_PARAM_POINTER("imlib_save_image_with_progress_callback_and_error_return", "error_return", error_return);
CAST_IMAGE(im, image);
if ((!(im->data)) && (im->loader) && (im->loader->load))
im->loader->load(im, NULL, 0, 1);

View File

@ -412,6 +412,8 @@ int main (int argc, char **argv)
up = imlib_updates_merge_for_rendering(up, w, h);
imlib_render_image_updates_on_drawable(im, up, disp, win, vis, cm,
depth, dith, 0, 0, NULL);
imlib_image_set_has_alpha(im, 1);
imlib_save_image(im, "out.argb");
if ((px != x) || (py != y))
{
Imlib_Updates u;