jpeg loader added that does everything RIGHT - needto mapk the png loader

do the same. :)


SVN revision: 228
This commit is contained in:
Carsten Haitzler 1999-09-10 18:11:47 +00:00
parent bc86f38d1a
commit 0fec460b2a
6 changed files with 322 additions and 28 deletions

View File

@ -89,11 +89,16 @@ LDFLAGS = -L/usr/X11R6/lib
INCLUDES = -I/usr/X11R6/include -I$(top_srcdir)/libltdl
pkgdir = $(HOME)/.loaders/image
pkg_LTLIBRARIES = png.la
pkg_LTLIBRARIES = png.la jpeg.la
png_la_SOURCES = loader_png.c
png_la_LDFLAGS = -no-undefined -module -avoid-version
png_la_LIBADD = -lpng -lz -lX11 -lXext
jpeg_la_SOURCES = loader_jpeg.c
jpeg_la_LDFLAGS = -no-undefined -module -avoid-version
jpeg_la_LIBADD = -ljpeg
bin_PROGRAMS = imlib2
imlib2_SOURCES = rend.c ximage.c scale.c main.c rgba.c image.c color.c grab.c blend.c file.c rgbadraw.c api.c draw.c context.c
@ -112,6 +117,8 @@ CPPFLAGS =
LIBS =
png_la_DEPENDENCIES =
png_la_OBJECTS = loader_png.lo
jpeg_la_DEPENDENCIES =
jpeg_la_OBJECTS = loader_jpeg.lo
PROGRAMS = $(bin_PROGRAMS)
imlib2_OBJECTS = rend.o ximage.o scale.o main.o rgba.o image.o color.o \
@ -133,11 +140,11 @@ DISTFILES = $(DIST_COMMON) $(SOURCES) $(HEADERS) $(TEXINFOS) $(EXTRA_DIST)
TAR = gtar
GZIP_ENV = --best
DEP_FILES = .deps/api.P .deps/blend.P .deps/color.P .deps/context.P \
.deps/draw.P .deps/file.P .deps/grab.P .deps/image.P .deps/loader_png.P \
.deps/main.P .deps/rend.P .deps/rgba.P .deps/rgbadraw.P .deps/scale.P \
.deps/ximage.P
SOURCES = $(png_la_SOURCES) $(imlib2_SOURCES)
OBJECTS = $(png_la_OBJECTS) $(imlib2_OBJECTS)
.deps/draw.P .deps/file.P .deps/grab.P .deps/image.P \
.deps/loader_jpeg.P .deps/loader_png.P .deps/main.P .deps/rend.P \
.deps/rgba.P .deps/rgbadraw.P .deps/scale.P .deps/ximage.P
SOURCES = $(png_la_SOURCES) $(jpeg_la_SOURCES) $(imlib2_SOURCES)
OBJECTS = $(png_la_OBJECTS) $(jpeg_la_OBJECTS) $(imlib2_OBJECTS)
all: all-redirect
.SUFFIXES:
@ -245,6 +252,9 @@ maintainer-clean-libtool:
png.la: $(png_la_OBJECTS) $(png_la_DEPENDENCIES)
$(LINK) -rpath $(pkgdir) $(png_la_LDFLAGS) $(png_la_OBJECTS) $(png_la_LIBADD) $(LIBS)
jpeg.la: $(jpeg_la_OBJECTS) $(jpeg_la_DEPENDENCIES)
$(LINK) -rpath $(pkgdir) $(jpeg_la_LDFLAGS) $(jpeg_la_OBJECTS) $(jpeg_la_LIBADD) $(LIBS)
mostlyclean-binPROGRAMS:
clean-binPROGRAMS:

View File

@ -14,11 +14,16 @@ LDFLAGS = -L/usr/X11R6/lib
INCLUDES = -I/usr/X11R6/include -I$(top_srcdir)/libltdl
pkgdir = $(HOME)/.loaders/image
pkg_LTLIBRARIES = png.la
pkg_LTLIBRARIES = png.la jpeg.la
png_la_SOURCES = loader_png.c
png_la_LDFLAGS = -no-undefined -module -avoid-version
png_la_LIBADD = -lpng -lz -lX11 -lXext
jpeg_la_SOURCES = loader_jpeg.c
jpeg_la_LDFLAGS = -no-undefined -module -avoid-version
jpeg_la_LIBADD = -ljpeg
bin_PROGRAMS = imlib2
imlib2_SOURCES = rend.c ximage.c scale.c main.c rgba.c image.c \
color.c grab.c blend.c file.c rgbadraw.c api.c draw.c \

45
api.c
View File

@ -248,6 +248,8 @@ imlib_render_pixmaps_for_whole_image(Imlib_Image image, Display *display,
CAST_IMAGE(im, image);
cm = (ImlibColorModifier *)color_modifier;
if ((!(im->data)) && (im->loader))
im->loader->load(im, NULL, 0, 1);
__imlib_CreatePixmapsForImage(display, drawable, visual, depth, colormap,
im, pixmap_return, mask_return, 0, 0,
im->w, im->h, im->w, im->h,
@ -274,6 +276,8 @@ imlib_render_pixmaps_for_whole_image_at_size(Imlib_Image image, Display *display
CAST_IMAGE(im, image);
cm = (ImlibColorModifier *)color_modifier;
if ((!(im->data)) && (im->loader))
im->loader->load(im, NULL, 0, 1);
__imlib_CreatePixmapsForImage(display, drawable, visual, depth, colormap,
im, pixmap_return, mask_return, 0, 0,
im->w, im->h, width, height,
@ -297,6 +301,8 @@ imlib_render_image_on_drawable(Imlib_Image image, Display *display,
CAST_IMAGE(im, image);
cm = (ImlibColorModifier *)color_modifier;
if ((!(im->data)) && (im->loader))
im->loader->load(im, NULL, 0, 1);
__imlib_RenderImage(display, im, drawable, 0, visual, colormap, depth,
0, 0, im->w, im->h, x, y, im->w, im->h,
0,
@ -320,6 +326,8 @@ imlib_render_image_on_drawable_at_size(Imlib_Image image, Display *display,
CAST_IMAGE(im, image);
cm = (ImlibColorModifier *)color_modifier;
if ((!(im->data)) && (im->loader))
im->loader->load(im, NULL, 0, 1);
__imlib_RenderImage(display, im, drawable, 0, visual, colormap, depth,
0, 0, im->w, im->h, x, y, width, height,
anti_aliased_scaling,
@ -328,6 +336,33 @@ imlib_render_image_on_drawable_at_size(Imlib_Image image, Display *display,
cm);
}
void imlib_render_image_part_on_drawable_at_size(Imlib_Image image, Display *display,
Drawable drawable, Visual *visual,
Colormap colormap, int depth,
char anti_aliased_scaling,
char dithered_rendering,
char alpha_blending,
int source_x, int source_y,
int source_width, int source_height,
int x, int y, int width, int height,
Imlib_Color_Modifier color_modifier)
{
ImlibImage *im;
ImlibColorModifier *cm;
CAST_IMAGE(im, image);
cm = (ImlibColorModifier *)color_modifier;
if ((!(im->data)) && (im->loader))
im->loader->load(im, NULL, 0, 1);
__imlib_RenderImage(display, im, drawable, 0, visual, colormap, depth,
source_x, source_y,
source_width, source_height, x, y, width, height,
anti_aliased_scaling,
dithered_rendering,
alpha_blending, 0,
cm);
}
#define LINESIZE 16
/* useful macro */
#define CLIP(x, y, w, h, xx, yy, ww, hh) \
@ -350,6 +385,10 @@ imlib_blend_image_onto_image(Imlib_Image source_image,
CAST_IMAGE(im_src, source_image);
CAST_IMAGE(im_dst, destination_image);
/* FIXME: doesnt do clipping in any way or form - must fix */
if ((!(im_src->data)) && (im_src->loader))
im_src->loader->load(im_src, NULL, 0, 1);
if ((!(im_dst->data)) && (im_dst->loader))
im_dst->loader->load(im_src, NULL, 0, 1);
if ((source_width == destination_width) &&
(source_height == destination_height))
@ -380,7 +419,7 @@ imlib_blend_image_onto_image(Imlib_Image source_image,
int y, h, hh;
sx = source_x;
sy = source_x;
sy = source_y;
sw = source_width;
sh = source_height;
dx = destination_x;
@ -431,7 +470,7 @@ imlib_blend_image_onto_image(Imlib_Image source_image,
dxx = dx - psx;
dyy = dy - psy;
dxx += (x2 * destination_width) / source_width;
dyy += (y2 * destination_width) / source_width;
dyy += (y2 * destination_height) / source_height;
/* do a second check to see if we now have invalid coords */
/* dont do anything if we have a 0 widht or height image to render */
@ -690,6 +729,8 @@ imlib_clone_image(Imlib_Image image)
ImlibImage *im, *im_old;
CAST_IMAGE(im_old, image);
if ((!(im_old->data)) && (im_old->loader))
im_old->loader->load(im_old, NULL, 0, 1);
im = __imlib_CreateImage(im_old->w, im_old->h, NULL);
im->data = malloc(im->w * im->h *sizeof(DATA32));
memcpy(im->data, im_old->data, im->w * im->h *sizeof(DATA32));

10
api.h
View File

@ -92,6 +92,16 @@ void imlib_render_image_on_drawable_at_size(Imlib_Image image, Display *display,
char alpha_blending,
int x, int y, int width, int height,
Imlib_Color_Modifier color_modifier);
void imlib_render_image_part_on_drawable_at_size(Imlib_Image image, Display *display,
Drawable drawable, Visual *visual,
Colormap colormap, int depth,
char anti_aliased_scaling,
char dithered_rendering,
char alpha_blending,
int source_x, int source_y,
int source_width, int source_height,
int x, int y, int width, int height,
Imlib_Color_Modifier color_modifier);
/* rgba space ops */
void imlib_blend_image_onto_image(Imlib_Image source_image,
Imlib_Image destination_image,

210
loader_jpeg.c Normal file
View File

@ -0,0 +1,210 @@
#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"
#include <jpeglib.h>
#include <setjmp.h>
struct ImLib_JPEG_error_mgr
{
struct jpeg_error_mgr pub;
sigjmp_buf setjmp_buffer;
};
typedef struct ImLib_JPEG_error_mgr *emptr;
void
_JPEGFatalErrorHandler(j_common_ptr cinfo)
{
emptr errmgr;
errmgr = (emptr) cinfo->err;
/* cinfo->err->output_message(cinfo);*/
siglongjmp(errmgr->setjmp_buffer, 1);
return;
}
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;
struct jpeg_decompress_struct cinfo;
struct ImLib_JPEG_error_mgr jerr;
FILE *f;
if (im->data)
return 0;
f = fopen(im->file, "rb");
if (!f)
return 0;
cinfo.err = jpeg_std_error(&(jerr.pub));
jerr.pub.error_exit = _JPEGFatalErrorHandler;
if (sigsetjmp(jerr.setjmp_buffer, 1))
{
jpeg_destroy_decompress(&cinfo);
fclose(f);
return 0;
}
jpeg_create_decompress(&cinfo);
jpeg_stdio_src(&cinfo, f);
jpeg_read_header(&cinfo, TRUE);
cinfo.do_fancy_upsampling = FALSE;
cinfo.do_block_smoothing = FALSE;
jpeg_start_decompress(&cinfo);
if ((!im->loader) && (!im->data))
{
im->w = w = cinfo.output_width;
im->h = h = cinfo.output_height;
UNSET_FLAG(im->flags, F_HAS_ALPHA);
im->format = strdup("jpeg");
}
if (((!im->data) && (im->loader)) || (immediate_load) || (progress))
{
DATA8 *ptr, *line[16], *data;
DATA32 *ptr2;
int x, y, l, i, scans, count, prevy;
im->w = w = cinfo.output_width;
im->h = h = cinfo.output_height;
if (cinfo.rec_outbuf_height > 16)
{
jpeg_destroy_decompress(&cinfo);
fclose(f);
return 0;
}
data = malloc(w * 16 * 3);
if (!data)
{
jpeg_destroy_decompress(&cinfo);
fclose(f);
return 0;
}
/* must set the im->data member before callign progress function */
ptr2 = im->data = malloc(w * h * sizeof(DATA32));
if (!im->data)
{
free(data);
jpeg_destroy_decompress(&cinfo);
fclose(f);
return 0;
}
count = 0;
prevy = 0;
if (cinfo.output_components == 3)
{
for (i = 0; i < cinfo.rec_outbuf_height; i++)
line[i] = data + (i * w * 3);
for (l = 0; l < h; l += cinfo.rec_outbuf_height)
{
jpeg_read_scanlines(&cinfo, line, cinfo.rec_outbuf_height);
scans = cinfo.rec_outbuf_height;
if ((h - l) < scans)
scans = h - l;
ptr = data;
for (y = 0; y < scans; y++)
{
for (x = 0; x < w; x++)
{
*ptr2 = (0xff000000) |
((ptr[0]) << 16) |
((ptr[1]) << 8) |
(ptr[2]);
ptr += 3;
ptr2 ++;
}
}
if (progress)
{
int per;
per = (l * 100) / h;
if ((per - count) >= progress_granularity)
{
count = per;
progress(im, per, 0, prevy, w, scans + l - prevy);
prevy = l + scans;
}
}
}
}
else if (cinfo.output_components == 1)
{
for (i = 0; i < cinfo.rec_outbuf_height; i++)
line[i] = data + (i * w);
for (l = 0; l < h; l += cinfo.rec_outbuf_height)
{
jpeg_read_scanlines(&cinfo, line, cinfo.rec_outbuf_height);
scans = cinfo.rec_outbuf_height;
if ((h - l) < scans)
scans = h - l;
ptr = data;
for (y = 0; y < scans; y++)
{
for (x = 0; x < w; x++)
{
*ptr2 = (0xff000000) |
((ptr[0]) << 16) |
((ptr[0]) << 8) |
(ptr[0]);
ptr ++;
ptr2 ++;
}
}
if (progress)
{
int per;
per = (l * 100) / h;
if ((per - count) >= progress_granularity)
{
count = per;
progress(im, per, 0, prevy, w, l + scans - prevy);
prevy = l + scans;
}
}
}
}
free(data);
}
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
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)
{
return 0;
}
void
formats (ImlibLoader *l)
{
char *list_formats[] =
{ "jpg", "jpeg", "jfif", "jfi" };
{
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]);
}
}

56
main.c
View File

@ -20,15 +20,31 @@
#include "api.h"
Display *disp;
Window win;
Visual *vis;
Colormap cm;
int depth;
progress(Imlib_Image *im, char percent,
int update_x, int update_y,
int update_w, int update_h)
{
printf("image laod for %3i%% done (%ix%i %i,%i)\n",
percent, update_w, update_h, update_x, update_y);
imlib_render_image_part_on_drawable_at_size(im, disp, win, vis, cm, depth,
0, 0, 0,
update_x, update_y,
update_w, update_h,
1700 + update_x, 100 + update_y,
update_w, update_h,
NULL);
}
#if 1
int main (int argc, char **argv)
{
Window win;
int i, j;
Imlib_Image *im;
Visual *vis;
int depth;
int sec1, usec1, sec2, usec2;
int pixels = 0;
struct timeval timev;
@ -75,8 +91,14 @@ int main (int argc, char **argv)
}
printf("init\n");
disp = XOpenDisplay(NULL);
printf("load\n");
im = imlib_load_image(file);
vis = DefaultVisual(disp, DefaultScreen(disp));
depth = DefaultDepth(disp, DefaultScreen(disp));
cm = DefaultColormap(disp, DefaultScreen(disp));
if (root)
win = DefaultRootWindow(disp);
else
win = XCreateSimpleWindow(disp, DefaultRootWindow(disp), 0, 0, 10, 10, 0, 0, 0);
im = imlib_load_image_with_progress_callback(file, progress, 1);
if (!im)
{
printf("load fialed\n");
@ -87,11 +109,9 @@ int main (int argc, char **argv)
w = imlib_image_get_width(im);
h = imlib_image_get_height(im);
}
if (root)
win = DefaultRootWindow(disp);
else
if (!root)
{
win = XCreateSimpleWindow(disp, DefaultRootWindow(disp), 0, 0, w, h, 0, 0, 0);
XResizeWindow(disp, win, w, h);
XMapWindow(disp, win);
}
if (scale)
@ -101,8 +121,6 @@ int main (int argc, char **argv)
XGetGeometry(disp, win, &d, &dd, &dd, &w, &h, &dd, &dd);
}
vis = DefaultVisual(disp, DefaultScreen(disp));
depth = DefaultDepth(disp, DefaultScreen(disp));
XSync(disp, False);
printf("rend\n");
gettimeofday(&timev,NULL);
@ -114,7 +132,7 @@ int main (int argc, char **argv)
for (i = 0; i < w; i++)
{
imlib_render_image_on_drawable_at_size(im, disp, win, vis,
DefaultColormap(disp, DefaultScreen(disp)),
cm,
depth,
aa, dith, blend,
0, 0,
@ -127,7 +145,7 @@ int main (int argc, char **argv)
{
Imlib_Image im2;
im2 = imlib_create_image(640, 480);
im2 = imlib_create_image(1024, 768);
while (1)
{
int x, y, dum;
@ -136,15 +154,15 @@ int main (int argc, char **argv)
XQueryPointer(disp, win, &rt, &rt, &x, &y,
&dum, &dum, &dui);
x -= 1700; y -= 100;
imlib_blend_image_onto_image(im, im2,
0, 0, w, h,
0, 0, w - 200, h - 200,
x - (w / 2), y - (h / 2),
x * 2, y * 2);
x - 32, y - 32,
-20 + x * 8, y * 8);
imlib_render_image_on_drawable(im2, disp, win, vis,
DefaultColormap(disp, DefaultScreen(disp)),
depth, dith, 1, 1700, 100, NULL);
cm, depth, dith, 1, 1700, 100, NULL);
}
}
else
@ -152,7 +170,7 @@ int main (int argc, char **argv)
for (i = 0; i < w; i++)
{
imlib_render_image_on_drawable_at_size(im, disp, win, vis,
DefaultColormap(disp, DefaultScreen(disp)),
cm,
depth,
aa, dith, blend,
0, 0,