jpeg and png should do the right thing with quality _or_ compression now

SVN revision: 3789
This commit is contained in:
Tom Gilbert 2000-10-30 00:58:02 +00:00
parent 490383f4d7
commit 2e758a7c94
2 changed files with 586 additions and 562 deletions

View File

@ -24,6 +24,7 @@ void _JPEGErrorHandler2(j_common_ptr cinfo, int msg_level);
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);
@ -146,9 +147,8 @@ load (ImlibImage *im, ImlibProgressFunction progress,
{
for (x = 0; x < w; x++)
{
*ptr2 = (0xff000000) |
((ptr[0]) << 16) |
((ptr[1]) << 8) |
*ptr2 =
(0xff000000) | ((ptr[0]) << 16) | ((ptr[1]) << 8) |
(ptr[2]);
ptr += 3;
ptr2++;
@ -159,8 +159,8 @@ load (ImlibImage *im, ImlibProgressFunction progress,
int per;
per = (l * 100) / h;
if (((per - count) >= progress_granularity) ||
((h - l) <= cinfo.rec_outbuf_height))
if (((per - count) >= progress_granularity)
|| ((h - l) <= cinfo.rec_outbuf_height))
{
count = per;
if (!progress(im, per, 0, prevy, w, scans + l - prevy))
@ -191,9 +191,8 @@ load (ImlibImage *im, ImlibProgressFunction progress,
{
for (x = 0; x < w; x++)
{
*ptr2 = (0xff000000) |
((ptr[0]) << 16) |
((ptr[0]) << 8) |
*ptr2 =
(0xff000000) | ((ptr[0]) << 16) | ((ptr[0]) << 8) |
(ptr[0]);
ptr++;
ptr2++;
@ -204,8 +203,8 @@ load (ImlibImage *im, ImlibProgressFunction progress,
int per;
per = (l * 100) / h;
if (((per - count) >= progress_granularity) ||
((h - l) <= cinfo.rec_outbuf_height))
if (((per - count) >= progress_granularity)
|| ((h - l) <= cinfo.rec_outbuf_height))
{
count = per;
if (!progress(im, per, 0, prevy, w, l + scans - prevy))
@ -239,7 +238,7 @@ save (ImlibImage *im, ImlibProgressFunction progress,
DATA8 *buf;
DATA32 *ptr;
JSAMPROW *jbuf;
int y = 0, quality = 75;
int y = 0, quality = 75, compression = 2;
ImlibImageTag *tag;
int i, j, pl = 0;
char pper = 0;
@ -281,6 +280,21 @@ save (ImlibImage *im, ImlibProgressFunction progress,
/* look for tags attached to image to get extra parameters liek quality */
/* settigns etc. - thsi si the "api" to hint for extra information for */
/* saver modules */
/* compression */
tag = __imlib_GetTag(im, "compression");
if (tag)
{
compression = tag->val;
if (compression < 0)
compression = 0;
if (compression > 9)
compression = 9;
}
/* convert to quality */
quality = (9 - compression) * 10;
quality = quality * 10 / 9;
/* quality */
tag = __imlib_GetTag(im, "quality");
if (tag)
quality = tag->val;
@ -316,8 +330,7 @@ save (ImlibImage *im, ImlibProgressFunction progress,
int l;
per = (char) ((100 * y) / im->h);
if (((per - pper) >= progress_granularity) ||
(y == (im->h - 1)))
if (((per - pper) >= progress_granularity) || (y == (im->h - 1)))
{
l = y - pl;
if (!progress(im, per, 0, (y - l), im->w, l))
@ -343,16 +356,15 @@ save (ImlibImage *im, ImlibProgressFunction progress,
void
formats(ImlibLoader * l)
{
char *list_formats[] =
{ "jpg", "jpeg", "jfif", "jfi" };
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]);
}
}

View File

@ -18,6 +18,7 @@
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);
@ -53,8 +54,8 @@ load (ImlibImage *im, ImlibProgressFunction progress,
return 0;
}
rewind(f);
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL,
NULL);
png_ptr =
png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr)
{
fclose(f);
@ -75,9 +76,9 @@ load (ImlibImage *im, ImlibProgressFunction progress,
}
png_init_io(png_ptr, f);
png_read_info(png_ptr, info_ptr);
png_get_IHDR(png_ptr, info_ptr,
(png_uint_32 *)(&w32), (png_uint_32 *)(&h32),
&bit_depth, &color_type, &interlace_type, NULL, NULL);
png_get_IHDR(png_ptr, info_ptr, (png_uint_32 *) (&w32),
(png_uint_32 *) (&h32), &bit_depth, &color_type,
&interlace_type, NULL, NULL);
im->w = (int) w32;
im->h = (int) h32;
if (color_type == PNG_COLOR_TYPE_PALETTE)
@ -142,6 +143,7 @@ load (ImlibImage *im, ImlibProgressFunction progress,
return 0;
}
lines = (unsigned char **) malloc(h * sizeof(unsigned char *));
if (!lines)
{
free(im->data);
@ -182,10 +184,8 @@ load (ImlibImage *im, ImlibProgressFunction progress,
for (i = 0; i < w; i++)
{
ptr[0] =
(line[i << 1] << 16) |
(line[i << 1] << 8) |
(line[i << 1]) |
(line[(i << 1) + 1] << 24);
(line[i << 1] << 16) | (line[i << 1] << 8) |
(line[i << 1]) | (line[(i << 1) + 1] << 24);
ptr++;
}
if (progress)
@ -199,11 +199,13 @@ load (ImlibImage *im, ImlibProgressFunction progress,
}
}
}
if ((progress) && (!progress(im, per, 0, prevy, w, y - prevy + 1)))
if ((progress)
&& (!progress(im, per, 0, prevy, w, y - prevy + 1)))
{
free(lines);
png_read_end(png_ptr, info_ptr);
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
png_destroy_read_struct(&png_ptr, &info_ptr,
(png_infopp) NULL);
fclose(f);
return 2;
}
@ -213,7 +215,8 @@ load (ImlibImage *im, ImlibProgressFunction progress,
else
{
for (i = 0; i < h; i++)
lines[i] = ((unsigned char *)(im->data)) + (i * w * sizeof(DATA32));
lines[i] =
((unsigned char *) (im->data)) + (i * w * sizeof(DATA32));
if (progress)
{
int y, count, prevy, pass, number_passes, per, nrows = 1;
@ -236,7 +239,8 @@ load (ImlibImage *im, ImlibProgressFunction progress,
{
free(lines);
png_read_end(png_ptr, info_ptr);
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
png_destroy_read_struct(&png_ptr, &info_ptr,
(png_infopp) NULL);
fclose(f);
return 2;
}
@ -247,7 +251,8 @@ load (ImlibImage *im, ImlibProgressFunction progress,
{
free(lines);
png_read_end(png_ptr, info_ptr);
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
png_destroy_read_struct(&png_ptr, &info_ptr,
(png_infopp) NULL);
fclose(f);
return 2;
}
@ -278,13 +283,12 @@ save (ImlibImage *im, ImlibProgressFunction progress,
int pl = 0;
char pper = 0;
ImlibImageTag *tag;
int quality = 75, compression;
int quality = 75, compression = 3;
f = fopen(im->file, "wb");
if (!f)
return 0;
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
NULL, NULL, NULL);
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr)
{
fclose(f);
@ -317,9 +321,9 @@ save (ImlibImage *im, ImlibProgressFunction progress,
}
else
{
png_set_IHDR(png_ptr, info_ptr, im->w, im->h, 8,
PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
png_set_IHDR(png_ptr, info_ptr, im->w, im->h, 8, PNG_COLOR_TYPE_RGB,
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE,
PNG_FILTER_TYPE_BASE);
data = malloc(im->w * 3 * sizeof(char));
}
sig_bit.red = 8;
@ -327,18 +331,27 @@ save (ImlibImage *im, ImlibProgressFunction progress,
sig_bit.blue = 8;
sig_bit.alpha = 8;
png_set_sBIT(png_ptr, info_ptr, &sig_bit);
/* compression */
/* quality */
tag = __imlib_GetTag(im, "quality");
if (tag)
{
quality = tag->val;
if (quality < 10)
quality = 10;
if (quality < 1)
quality = 1;
if (quality > 99)
quality = 99;
/* translate to png-relevant value */
}
/* convert to compression */
quality = quality / 10;
compression = 10 - quality;
/* should be 1-9 now */
compression = 9 - quality;
/* compression */
tag = __imlib_GetTag(im, "compression");
if (tag)
compression = tag->val;
if (compression < 0)
compression = 0;
if (compression > 9)
compression = 9;
png_set_compression_level(png_ptr, compression);
png_write_info(png_ptr, info_ptr);
png_set_shift(png_ptr, &sig_bit);
@ -404,8 +417,7 @@ void
formats(ImlibLoader * l)
{
/* this is the only bit you have to change... */
char *list_formats[] =
{ "png" };
char *list_formats[] = { "png" };
/* don't bother changing any of this - it just reads this in and sets */
/* the struct values and makes copies */
@ -414,8 +426,8 @@ formats (ImlibLoader *l)
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]);
}
}