2003-06-30 16:44:03 +04:00
|
|
|
/*
|
|
|
|
* This file is part of NetSurf, http://netsurf.sourceforge.net/
|
|
|
|
* Licensed under the GNU General Public License,
|
|
|
|
* http://www.opensource.org/licenses/gpl-license
|
2004-03-24 01:17:08 +03:00
|
|
|
* Copyright 2004 James Bursa <bursa@users.sourceforge.net>
|
2004-01-25 23:13:51 +03:00
|
|
|
* Copyright 2004 John M Bell <jmb202@ecs.soton.ac.uk>
|
2003-02-26 00:00:27 +03:00
|
|
|
*/
|
|
|
|
|
2004-03-24 01:17:08 +03:00
|
|
|
/** \file
|
|
|
|
* Content for image/jpeg (implementation).
|
|
|
|
*
|
|
|
|
* This implementation uses the IJG JPEG library.
|
|
|
|
*/
|
|
|
|
|
2003-02-26 00:00:27 +03:00
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
2004-01-25 23:13:51 +03:00
|
|
|
#include <stdio.h>
|
2003-02-26 00:00:27 +03:00
|
|
|
#include <stdlib.h>
|
2004-01-25 23:39:35 +03:00
|
|
|
#include <setjmp.h>
|
2004-03-24 01:17:08 +03:00
|
|
|
#include <swis.h>
|
|
|
|
#define JPEG_INTERNAL_OPTIONS
|
2004-01-25 23:13:51 +03:00
|
|
|
#include "libjpeg/jpeglib.h"
|
|
|
|
#include "oslib/colourtrans.h"
|
|
|
|
#include "oslib/osspriteop.h"
|
2004-01-05 05:10:59 +03:00
|
|
|
#include "netsurf/utils/config.h"
|
2003-02-26 00:00:27 +03:00
|
|
|
#include "netsurf/content/content.h"
|
2004-01-25 23:13:51 +03:00
|
|
|
#include "netsurf/desktop/gui.h"
|
2004-05-10 01:05:24 +04:00
|
|
|
#include "netsurf/riscos/gui.h"
|
2003-02-26 00:00:27 +03:00
|
|
|
#include "netsurf/riscos/jpeg.h"
|
2004-03-24 01:17:08 +03:00
|
|
|
#include "netsurf/riscos/options.h"
|
|
|
|
#include "netsurf/riscos/tinct.h"
|
2004-01-25 23:13:51 +03:00
|
|
|
#include "netsurf/utils/log.h"
|
2004-02-27 20:45:19 +03:00
|
|
|
#include "netsurf/utils/messages.h"
|
2003-02-26 00:00:27 +03:00
|
|
|
#include "netsurf/utils/utils.h"
|
2004-01-25 23:13:51 +03:00
|
|
|
|
2003-02-26 00:00:27 +03:00
|
|
|
|
2004-03-24 01:17:08 +03:00
|
|
|
/* We require a the library to be configured with these options to save
|
|
|
|
* copying data during decoding. */
|
|
|
|
#if RGB_RED != 0 || RGB_GREEN != 1 || RGB_BLUE != 2 || RGB_PIXELSIZE != 4
|
|
|
|
#error JPEG library incorrectly configured.
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
struct nsjpeg_error_mgr {
|
|
|
|
struct jpeg_error_mgr pub;
|
|
|
|
jmp_buf setjmp_buffer;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static void nsjpeg_error_exit(j_common_ptr cinfo);
|
|
|
|
static void nsjpeg_init_source(j_decompress_ptr cinfo);
|
|
|
|
static boolean nsjpeg_fill_input_buffer(j_decompress_ptr cinfo);
|
|
|
|
static void nsjpeg_skip_input_data(j_decompress_ptr cinfo, long num_bytes);
|
|
|
|
static void nsjpeg_term_source(j_decompress_ptr cinfo);
|
|
|
|
|
|
|
|
|
2004-01-25 23:13:51 +03:00
|
|
|
/**
|
2004-03-24 01:17:08 +03:00
|
|
|
* Create a CONTENT_JPEG.
|
2004-01-25 23:13:51 +03:00
|
|
|
*/
|
2004-03-24 01:17:08 +03:00
|
|
|
|
|
|
|
void nsjpeg_create(struct content *c, const char *params[])
|
|
|
|
{
|
|
|
|
c->data.jpeg.sprite_area = 0;
|
2004-01-25 23:13:51 +03:00
|
|
|
}
|
|
|
|
|
2004-03-24 01:17:08 +03:00
|
|
|
|
2004-01-25 23:13:51 +03:00
|
|
|
/**
|
2004-03-24 01:17:08 +03:00
|
|
|
* Convert a CONTENT_JPEG for display.
|
2004-01-25 23:13:51 +03:00
|
|
|
*/
|
|
|
|
|
2004-03-24 01:17:08 +03:00
|
|
|
int nsjpeg_convert(struct content *c, unsigned int w, unsigned int h)
|
|
|
|
{
|
|
|
|
struct jpeg_decompress_struct cinfo;
|
|
|
|
struct nsjpeg_error_mgr jerr;
|
|
|
|
struct jpeg_source_mgr source_mgr = { 0, 0,
|
|
|
|
nsjpeg_init_source, nsjpeg_fill_input_buffer,
|
|
|
|
nsjpeg_skip_input_data, jpeg_resync_to_restart,
|
|
|
|
nsjpeg_term_source };
|
|
|
|
unsigned int height;
|
|
|
|
unsigned int width;
|
|
|
|
unsigned int area_size;
|
|
|
|
osspriteop_area *sprite_area = 0;
|
|
|
|
osspriteop_header *sprite;
|
|
|
|
|
|
|
|
cinfo.err = jpeg_std_error(&jerr.pub);
|
|
|
|
jerr.pub.error_exit = nsjpeg_error_exit;
|
|
|
|
if (setjmp(jerr.setjmp_buffer)) {
|
|
|
|
jpeg_destroy_decompress(&cinfo);
|
|
|
|
free(sprite_area);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
jpeg_create_decompress(&cinfo);
|
|
|
|
source_mgr.next_input_byte = c->source_data;
|
|
|
|
source_mgr.bytes_in_buffer = c->source_size;
|
|
|
|
cinfo.src = &source_mgr;
|
|
|
|
jpeg_read_header(&cinfo, TRUE);
|
|
|
|
cinfo.out_color_space = JCS_RGB;
|
|
|
|
cinfo.dct_method = JDCT_ISLOW;
|
|
|
|
jpeg_start_decompress(&cinfo);
|
|
|
|
|
|
|
|
width = cinfo.output_width;
|
|
|
|
height = cinfo.output_height;
|
|
|
|
|
|
|
|
area_size = 16 + 44 + width * height * 4;
|
|
|
|
sprite_area = malloc(area_size);
|
|
|
|
if (!sprite_area) {
|
|
|
|
LOG(("malloc failed"));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* area control block */
|
|
|
|
sprite_area->size = area_size;
|
|
|
|
sprite_area->sprite_count = 1;
|
|
|
|
sprite_area->first = 16;
|
|
|
|
sprite_area->used = area_size;
|
|
|
|
|
|
|
|
/* sprite control block */
|
|
|
|
sprite = (osspriteop_header *) (sprite_area + 1);
|
|
|
|
sprite->size = area_size - 16;
|
|
|
|
strncpy(sprite->name, "jpeg", 12);
|
|
|
|
sprite->width = width - 1;
|
|
|
|
sprite->height = height - 1;
|
|
|
|
sprite->left_bit = 0;
|
|
|
|
sprite->right_bit = 31;
|
|
|
|
sprite->image = sprite->mask = 44;
|
|
|
|
sprite->mode = (os_mode) 0x301680b5;
|
|
|
|
|
|
|
|
do {
|
|
|
|
JSAMPROW scanlines[1];
|
|
|
|
scanlines[0] = (JSAMPROW) ((char *) sprite + 44 +
|
|
|
|
width * cinfo.output_scanline * 4);
|
|
|
|
jpeg_read_scanlines(&cinfo, scanlines, 1);
|
|
|
|
} while (cinfo.output_scanline != cinfo.output_height);
|
|
|
|
|
|
|
|
jpeg_finish_decompress(&cinfo);
|
|
|
|
jpeg_destroy_decompress(&cinfo);
|
|
|
|
|
|
|
|
/*xosspriteop_save_sprite_file(osspriteop_USER_AREA,
|
|
|
|
sprite_area, "jpeg");*/
|
|
|
|
|
|
|
|
c->width = width;
|
|
|
|
c->height = height;
|
|
|
|
c->data.jpeg.sprite_area = sprite_area;
|
|
|
|
c->title = malloc(100);
|
|
|
|
if (c->title)
|
|
|
|
sprintf(c->title, messages_get("JPEGTitle"),
|
|
|
|
width, height, c->source_size);
|
|
|
|
c->status = CONTENT_STATUS_DONE;
|
|
|
|
return 0;
|
2004-01-25 23:13:51 +03:00
|
|
|
}
|
|
|
|
|
2004-03-24 01:17:08 +03:00
|
|
|
|
2004-01-25 23:13:51 +03:00
|
|
|
/**
|
2004-03-24 01:17:08 +03:00
|
|
|
* Fatal error handler for JPEG library.
|
|
|
|
*
|
|
|
|
* This prevents jpeglib calling exit() on a fatal error.
|
2004-01-25 23:13:51 +03:00
|
|
|
*/
|
2004-03-24 01:17:08 +03:00
|
|
|
|
|
|
|
void nsjpeg_error_exit(j_common_ptr cinfo)
|
|
|
|
{
|
|
|
|
struct nsjpeg_error_mgr *err = (struct nsjpeg_error_mgr *) cinfo->err;
|
|
|
|
(*cinfo->err->output_message) (cinfo);
|
|
|
|
longjmp(err->setjmp_buffer, 1);
|
2004-01-25 23:13:51 +03:00
|
|
|
}
|
|
|
|
|
2004-03-24 01:17:08 +03:00
|
|
|
|
2004-01-25 23:13:51 +03:00
|
|
|
/**
|
2004-03-24 01:17:08 +03:00
|
|
|
* JPEG data source manager: initialize source.
|
2004-01-25 23:13:51 +03:00
|
|
|
*/
|
2004-01-25 23:39:35 +03:00
|
|
|
|
2004-03-24 01:17:08 +03:00
|
|
|
void nsjpeg_init_source(j_decompress_ptr cinfo)
|
|
|
|
{
|
|
|
|
}
|
2004-01-25 23:39:35 +03:00
|
|
|
|
|
|
|
|
2004-03-24 01:17:08 +03:00
|
|
|
static char nsjpeg_eoi[] = { 0xff, JPEG_EOI };
|
2004-01-25 23:39:35 +03:00
|
|
|
|
2004-03-24 01:17:08 +03:00
|
|
|
/**
|
|
|
|
* JPEG data source manager: fill the input buffer.
|
|
|
|
*
|
|
|
|
* This can only occur if the JPEG data was truncated or corrupted. Insert a
|
|
|
|
* fake EOI marker to allow the decompressor to output as much as possible.
|
|
|
|
*/
|
2004-01-25 23:39:35 +03:00
|
|
|
|
2004-03-24 01:17:08 +03:00
|
|
|
boolean nsjpeg_fill_input_buffer(j_decompress_ptr cinfo)
|
|
|
|
{
|
|
|
|
cinfo->src->next_input_byte = nsjpeg_eoi;
|
|
|
|
cinfo->src->bytes_in_buffer = 2;
|
|
|
|
return TRUE;
|
2004-01-25 23:39:35 +03:00
|
|
|
}
|
|
|
|
|
2004-01-25 23:13:51 +03:00
|
|
|
|
|
|
|
/**
|
2004-03-24 01:17:08 +03:00
|
|
|
* JPEG data source manager: skip num_bytes worth of data.
|
2004-01-25 23:13:51 +03:00
|
|
|
*/
|
|
|
|
|
2004-03-24 01:17:08 +03:00
|
|
|
void nsjpeg_skip_input_data(j_decompress_ptr cinfo, long num_bytes)
|
2003-02-26 00:00:27 +03:00
|
|
|
{
|
2004-03-24 01:17:08 +03:00
|
|
|
if ((long) cinfo->src->bytes_in_buffer < num_bytes) {
|
|
|
|
cinfo->src->next_input_byte = 0;
|
|
|
|
cinfo->src->bytes_in_buffer = 0;
|
|
|
|
} else {
|
|
|
|
cinfo->src->next_input_byte += num_bytes;
|
|
|
|
cinfo->src->bytes_in_buffer -= num_bytes;
|
|
|
|
}
|
2003-02-26 00:00:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-03-24 01:17:08 +03:00
|
|
|
/**
|
|
|
|
* JPEG data source manager: terminate source.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void nsjpeg_term_source(j_decompress_ptr cinfo)
|
2003-02-26 00:00:27 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-03-24 01:17:08 +03:00
|
|
|
/**
|
|
|
|
* Destroy a CONTENT_JPEG and free all resources it owns.
|
|
|
|
*/
|
|
|
|
|
2004-01-25 23:13:51 +03:00
|
|
|
void nsjpeg_destroy(struct content *c)
|
2003-02-26 00:00:27 +03:00
|
|
|
{
|
2004-03-24 01:17:08 +03:00
|
|
|
free(c->data.jpeg.sprite_area);
|
|
|
|
free(c->title);
|
2003-02-26 00:00:27 +03:00
|
|
|
}
|
2003-05-10 15:15:49 +04:00
|
|
|
|
2004-03-24 01:17:08 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Redraw a CONTENT_JPEG.
|
|
|
|
*/
|
|
|
|
|
2004-01-25 23:13:51 +03:00
|
|
|
void nsjpeg_redraw(struct content *c, long x, long y,
|
2004-03-24 01:17:08 +03:00
|
|
|
unsigned long width, unsigned long height,
|
|
|
|
long clip_x0, long clip_y0, long clip_x1, long clip_y1,
|
|
|
|
float scale)
|
2003-05-10 15:15:49 +04:00
|
|
|
{
|
2004-05-10 01:05:24 +04:00
|
|
|
unsigned int tinct_options;
|
|
|
|
|
|
|
|
/* If we have a gui_window then we work from there, if not we use the global
|
|
|
|
settings as we are drawing a thumbnail.
|
|
|
|
*/
|
|
|
|
if (ro_gui_current_redraw_gui) {
|
|
|
|
tinct_options = (ro_gui_current_redraw_gui->option_filter_sprites?(1<<1):0) |
|
|
|
|
(ro_gui_current_redraw_gui->option_dither_sprites?(1<<2):0);
|
|
|
|
} else {
|
|
|
|
tinct_options = (option_filter_sprites?(1<<1):0) |
|
|
|
|
(option_dither_sprites?(1<<2):0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Tinct currently only handles 32bpp sprites that have an embedded alpha mask. Any
|
|
|
|
sprites not matching the required specifications are ignored. See the Tinct
|
|
|
|
documentation for further information.
|
|
|
|
*/
|
2004-03-24 01:17:08 +03:00
|
|
|
_swix(Tinct_PlotScaled,
|
|
|
|
_IN(2) | _IN(3) | _IN(4) | _IN(5) | _IN(6) | _IN(7),
|
|
|
|
(char *) c->data.jpeg.sprite_area +
|
|
|
|
c->data.jpeg.sprite_area->first,
|
|
|
|
x, (int) (y - height),
|
|
|
|
width, height,
|
2004-05-10 01:05:24 +04:00
|
|
|
tinct_options);
|
2003-05-10 15:15:49 +04:00
|
|
|
}
|