replaced loader_gzbz2 with loader_zlib and loader_bz2. removed empty save() in some loaders

SVN revision: 8932
This commit is contained in:
Tilman Sauerbeck 2004-02-10 18:32:22 +00:00
parent 8caeeb9b99
commit 0f30f8b88e
8 changed files with 342 additions and 160 deletions

View File

@ -288,15 +288,63 @@ if test "$gif_ok" = no; then
fi
fi
# Test for zlib
AC_CHECK_LIB(z, uncompress,
zlib_ok=yes,
zlib_ok=no
AC_MSG_WARN(*** Native zlib support will not be built (zlib not found) ***), $EXTRA_LIBS -lz)
if test "$zlib_ok" = yes; then
AC_MSG_CHECKING([for zlib.h])
AC_TRY_CPP(
[#include <stdio.h>
#undef PACKAGE
#undef VERSION
#include <zlib.h>],
zlib_ok=yes,
zlib_ok=no)
AC_MSG_RESULT($zlib_ok)
if test "$zlib_ok" = yes; then
ZLIBLIBS="-lz"
else
AC_MSG_WARN(*** Native zlib support will not be built (zlib header file not found) ***)
fi
fi
# Test for libbzip2
AC_CHECK_LIB(bz2, BZ2_bzRead,
bz2_ok=yes,
bz2_ok=no
AC_MSG_WARN(*** Native bz2 support will not be built (bzip2 library not found) ***), $EXTRA_LIBS -lbz2)
if test "$bz2_ok" = yes; then
AC_MSG_CHECKING([for bzlib.h])
AC_TRY_CPP(
[#include <stdio.h>
#undef PACKAGE
#undef VERSION
#include <bzlib.h>],
bz2_ok=yes,
bz2_ok=no)
AC_MSG_RESULT($bz2_ok)
if test "$bz2_ok" = yes; then
BZ2LIBS="-lbz2"
else
AC_MSG_WARN(*** Native bz2 support will not be built (bzip2 header file not found) ***)
fi
fi
AM_CONDITIONAL(JPEG_LOADER, test "$jpeg_ok" = yes)
AM_CONDITIONAL(PNG_LOADER, test "$png_ok" = yes)
AM_CONDITIONAL(TIFF_LOADER, test "$tiff_ok" = yes)
AM_CONDITIONAL(GIF_LOADER, test "$gif_ok" = yes)
AM_CONDITIONAL(ZLIB_LOADER, test "$zlib_ok" = yes)
AM_CONDITIONAL(BZ2_LOADER, test "$bz2_ok" = yes)
AC_SUBST(JPEGLIBS)
AC_SUBST(PNGLIBS)
AC_SUBST(TIFFLIBS)
AC_SUBST(GIFLIBS)
AC_SUBST(ZLIBLIBS)
AC_SUBST(BZ2LIBS)
AC_SUBST(REQUIREMENTS)
AC_SUBST(x_cflags)

View File

@ -271,15 +271,63 @@ if test "$gif_ok" = no; then
fi
fi
# Test for zlib
AC_CHECK_LIB(z, uncompress,
zlib_ok=yes,
zlib_ok=no
AC_MSG_WARN(*** Native zlib support will not be built (zlib not found) ***), $EXTRA_LIBS -lz)
if test "$zlib_ok" = yes; then
AC_MSG_CHECKING([for zlib.h])
AC_TRY_CPP(
[#include <stdio.h>
#undef PACKAGE
#undef VERSION
#include <zlib.h>],
zlib_ok=yes,
zlib_ok=no)
AC_MSG_RESULT($zlib_ok)
if test "$zlib_ok" = yes; then
ZLIBLIBS="-lz"
else
AC_MSG_WARN(*** Native zlib support will not be built (zlib header file not found) ***)
fi
fi
# Test for libbzip2
AC_CHECK_LIB(bz2, BZ2_bzRead,
bz2_ok=yes,
bz2_ok=no
AC_MSG_WARN(*** Native bz2 support will not be built (bzip2 library not found) ***), $EXTRA_LIBS -lbz2)
if test "$bz2_ok" = yes; then
AC_MSG_CHECKING([for bzlib.h])
AC_TRY_CPP(
[#include <stdio.h>
#undef PACKAGE
#undef VERSION
#include <bzlib.h>],
bz2_ok=yes,
bz2_ok=no)
AC_MSG_RESULT($bz2_ok)
if test "$bz2_ok" = yes; then
BZ2LIBS="-lbz2"
else
AC_MSG_WARN(*** Native bz2 support will not be built (bzip2 header file not found) ***)
fi
fi
AM_CONDITIONAL(JPEG_LOADER, test "$jpeg_ok" = yes)
AM_CONDITIONAL(PNG_LOADER, test "$png_ok" = yes)
AM_CONDITIONAL(TIFF_LOADER, test "$tiff_ok" = yes)
AM_CONDITIONAL(GIF_LOADER, test "$gif_ok" = yes)
AM_CONDITIONAL(ZLIB_LOADER, test "$zlib_ok" = yes)
AM_CONDITIONAL(BZ2_LOADER, test "$bz2_ok" = yes)
AC_SUBST(JPEGLIBS)
AC_SUBST(PNGLIBS)
AC_SUBST(TIFFLIBS)
AC_SUBST(GIFLIBS)
AC_SUBST(ZLIBLIBS)
AC_SUBST(BZ2LIBS)
AC_SUBST(REQUIREMENTS)
AC_SUBST(x_cflags)

View File

@ -25,13 +25,21 @@ endif
if GIF_LOADER
GIF_L = gif.la
endif
if ZLIB_LOADER
ZLIB_L = zlib.la
endif
if BZ2_LOADER
BZ2_L = bz2.la
endif
pkg_LTLIBRARIES = \
$(JPEG_L) \
$(PNG_L) \
$(TIFF_L) \
$(GIF_L) \
pnm.la argb.la bmp.la xpm.la tga.la gzbz2.la
$(ZLIB_L) \
$(BZ2_L) \
pnm.la argb.la bmp.la xpm.la tga.la
jpeg_la_SOURCES = loader_jpeg.c
jpeg_la_LDFLAGS = -no-undefined -module -avoid-version -L$(top_builddir)/src -L$(top_builddir)/src/.libs
@ -69,6 +77,10 @@ tga_la_SOURCES = loader_tga.c
tga_la_LDFLAGS = -no-undefined -module -avoid-version -L$(top_builddir)/src -L$(top_builddir)/src/.libs
tga_la_LIBADD = -lImlib2
gzbz2_la_SOURCES = loader_gzbz2.c
gzbz2_la_LDFLAGS = -no-undefined -module -avoid-version -L$(top_builddir)/src -L$(top_builddir)/src/.libs
gzbz2_la_LIBADD = -lImlib2
zlib_la_SOURCES = loader_zlib.c
zlib_la_LDFLAGS = -no-undefined -module -avoid-version -L$(top_builddir)/src -L$(top_builddir)/src/.libs
zlib_la_LIBADD = @ZLIBLIBS@ -lImlib2
bz2_la_SOURCES = loader_bz2.c
bz2_la_LDFLAGS = -no-undefined -module -avoid-version -L$(top_builddir)/src -L$(top_builddir)/src/.libs
bz2_la_LIBADD = @BZ2LIBS@ -lImlib2

106
loaders/loader_bz2.c Normal file
View File

@ -0,0 +1,106 @@
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <bzlib.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <assert.h>
#include "common.h"
#include "image.h"
#define OUTBUF_SIZE 16384
#define INBUF_SIZE 1024
static int uncompress_file (FILE *fp, int dest)
{
BZFILE *bf;
DATA8 outbuf[OUTBUF_SIZE];
int cont = 1, bytes, error;
bf = BZ2_bzReadOpen (&error, fp, 0, 0, NULL, 0);
if (error != BZ_OK) {
BZ2_bzReadClose (NULL, bf);
return 0;
}
error = BZ_OK;
while (error == BZ_OK) {
bytes = BZ2_bzRead (&error, bf, &outbuf, OUTBUF_SIZE);
if (error == BZ_OK)
write (dest, outbuf, bytes);
}
BZ2_bzReadClose (&error, bf);
return 1;
}
char load (ImlibImage *im, ImlibProgressFunction progress,
char progress_granularity, char immediate_load)
{
ImlibLoader *loader;
FILE *fp;
int dest, res;
char *file, tmp[] = "/tmp/imlib2_loader_bz2-XXXXXX";
assert (im);
/* we'll need a copy of it later */
file = im->real_file;
if ((dest = mkstemp (tmp)) < 0)
return 0;
if (!(fp = fopen (im->real_file, "rb"))) {
unlink (tmp);
return 0;
}
res = uncompress_file (fp, dest);
fclose (fp);
close (dest);
if (!res) {
unlink (tmp);
return 0;
}
if (!(loader = __imlib_FindBestLoaderForFile (tmp, 0))) {
unlink (tmp);
return 0;
}
free (im->real_file);
im->real_file = strdup (tmp);
loader->load (im, progress, progress_granularity, immediate_load);
free (im->real_file);
im->real_file = strdup (file);
unlink (tmp);
return 1;
}
void formats (ImlibLoader *l)
{
/* this is the only bit you have to change... */
char *list_formats[] = {"bz2"};
int i;
/* don't bother changing any of this - it just reads this in
* and sets the struct values and makes copies
*/
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

@ -221,15 +221,6 @@ load(ImlibImage * im, ImlibProgressFunction progress, char progress_granularity,
return 1;
}
char
save(ImlibImage * im, ImlibProgressFunction progress, char progress_granularity)
{
return 0;
im = NULL;
progress = NULL;
progress_granularity = 0;
}
/* fills the ImlibLoader struct with a strign array of format file */
/* extensions this loader can load. eg: */
/* loader->formats = { "jpeg", "jpg"}; */

View File

@ -1,138 +0,0 @@
/* Team members of IMLIB2 plz have a look at first scratch of .gz & .bz2
* loader - I need it in my common work for lots of reasons, it is more
* or less UNIX ready, but I used OS/2 for testing, also I am not sure
* about temp directory for example gimp loader use tempnames for some
* reason (??) If you think this loader is not needed for Imlib2 feel
* free to wipe it entirely. Also if it will be ok, wipe this comment off,
* and add to makefiles
* Stauff (pla@cland.ru)
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "common.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <ctype.h>
#include <errno.h>
#include <string.h>
#include <errno.h>
#include "image.h"
char load(ImlibImage * im, ImlibProgressFunction progress,
char progress_granularity, char immediate_load);
char save(ImlibImage * im, ImlibProgressFunction progress,
char progress_granularity);
void formats(ImlibLoader * l);
static int
exists(char *file)
{
struct stat st;
if (stat(file, &st) < 0)
return 0;
return 1;
}
char *
pure_filename(char *path_filename)
{
char *ptr;
ptr = strrchr(path_filename, '/');
#ifdef __EMX__
/* Care about \\ */
#endif
if (ptr)
ptr++;
else
ptr = path_filename;
return ptr;
}
char
load(ImlibImage * im, ImlibProgressFunction progress,
char progress_granularity, char immediate_load)
{
char file[4096], *ptr;
char *str_gz;
ImlibLoader *sub_loader;
char *tmpdir;
str_gz = malloc(4096);
if (im->data)
return 0;
if (!im->file)
return 0;
strncpy(file, im->real_file, sizeof(file));
file[sizeof(file) - 1] = 0;
if (!exists(file))
return 0;
ptr = strrchr(file, '.');
if (!ptr)
return 0;
*ptr = 0;
if (!(tmpdir = getenv("TMPDIR")))
tmpdir = "/tmp";
if ((strlen(ptr + 1) >= 2) && (!strcmp(ptr + 1, "gz")))
{
sprintf(str_gz, "gzip -d %s -c > %s/%s", im->real_file, tmpdir,
pure_filename(file));
}
else if ((strlen(ptr + 1) >= 3) && (!strcmp(ptr + 1, "bz2")))
{
sprintf(str_gz, "bzip2 -d %s -c > %s/%s", im->real_file, tmpdir,
pure_filename(file));
}
else
return 0; /* Eeek why we are here? */
if (system(str_gz) < 0)
return 0;
free(im->real_file);
sprintf(str_gz, "%s/%s", tmpdir, pure_filename(file));
im->real_file = strdup(str_gz);
free(str_gz);
im->format = strdup(++ptr);
sub_loader = __imlib_FindBestLoaderForFile(im->real_file);
if (sub_loader)
sub_loader->load(im, progress, progress_granularity, 1);
else
{
unlink(im->real_file);
return 0;
}
unlink(im->real_file);
free(im->real_file);
*--ptr = '.';
im->real_file = strdup(file);
return 1;
}
char
save(ImlibImage * im, ImlibProgressFunction progress, char progress_granularity)
{
/* TODO */
return 0;
}
void
formats(ImlibLoader * l)
{
char *list_formats[] = { "gz", "bz2" };
{
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

@ -665,15 +665,6 @@ load(ImlibImage * im, ImlibProgressFunction progress, char progress_granularity,
return 1;
}
char
save(ImlibImage * im, ImlibProgressFunction progress, char progress_granularity)
{
return 0;
im = NULL;
progress = NULL;
progress_granularity = 0;
}
/* fills the ImlibLoader struct with a strign array of format file */
/* extensions this loader can load. eg: */
/* loader->formats = { "jpeg", "jpg"}; */

124
loaders/loader_zlib.c Normal file
View File

@ -0,0 +1,124 @@
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <zlib.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <assert.h>
#include "common.h"
#include "image.h"
#define OUTBUF_SIZE 16484
#define INBUF_SIZE 1024
static int handle_buffer (DATA8 *src, unsigned long src_len,
DATA8 **dest, unsigned long *dest_len)
{
static DATA8 outbuf[OUTBUF_SIZE];
uLongf outbuf_len = OUTBUF_SIZE;
int res;
assert (src);
assert (src_len);
assert (dest);
assert (dest_len);
res = uncompress (outbuf, &outbuf_len, src, src_len);
switch (res) {
case Z_OK:
*dest = outbuf;
*dest_len = (unsigned long) outbuf_len;
return 1;
case Z_BUF_ERROR:
return 0;
default:
*dest = NULL;
*dest_len = 0;
return 0;
}
}
static void uncompress_file (int src, int dest, off_t size)
{
DATA8 inbuf[INBUF_SIZE], *outbuf;
off_t left;
ssize_t inlen;
unsigned long outlen = 0;
for (left = size; left; left -= inlen) {
inlen = read (src, inbuf, MIN (left, INBUF_SIZE));
if (inlen <= 0)
break;
if (handle_buffer (inbuf, inlen, &outbuf, &outlen))
write (dest, outbuf, outlen);
}
}
char load (ImlibImage *im, ImlibProgressFunction progress,
char progress_granularity, char immediate_load)
{
ImlibLoader *loader;
int src, dest;
char *file, tmp[] = "/tmp/imlib2_loader_zlib-XXXXXX";
struct stat st;
assert (im);
/* we'll need a copy of it later */
file = im->real_file;
if (stat (im->real_file, &st) < 0)
return 0;
if ((dest = mkstemp (tmp)) < 0)
return 0;
if ((src = open (im->real_file, O_RDONLY)) < 0) {
unlink (tmp);
return 0;
}
uncompress_file (src, dest, st.st_size);
close (src);
close (dest);
if (!(loader = __imlib_FindBestLoaderForFile (tmp, 0))) {
unlink (tmp);
return 0;
}
free (im->real_file);
im->real_file = strdup (tmp);
loader->load (im, progress, progress_granularity, immediate_load);
free (im->real_file);
im->real_file = strdup (file);
unlink (tmp);
return 1;
}
void formats (ImlibLoader *l)
{
/* this is the only bit you have to change... */
char *list_formats[] = {"gz"};
int i;
/* don't bother changing any of this - it just reads this in
* and sets the struct values and makes copies
*/
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]);
}