?? example.pas
字號:
{ ERROR HANDLING:
The JPEG library's standard error handler (jerror.c) is divided into
several "methods" which you can override individually. This lets you
adjust the behavior without duplicating a lot of code, which you might
have to update with each future release.
Our example here shows how to override the "error_exit" method so that
control is returned to the library's caller when a fatal error occurs,
rather than calling exit() as the standard error_exit method does.
We use C's setjmp/longjmp facility to return control. This means that the
routine which calls the JPEG library must first execute a setjmp() call to
establish the return point. We want the replacement error_exit to do a
longjmp(). But we need to make the setjmp buffer accessible to the
error_exit routine. To do this, we make a private extension of the
standard JPEG error handler object. (If we were using C++, we'd say we
were making a subclass of the regular error handler.) }
{$IFDEF TEST ---------------------------------------------------------------}
{extern}
type
jmp_buf = pointer;
{ This routine does the output }
procedure put_scanline_someplace(buffer : JSAMPROW; row_stride : int);
forward;
{ define an error recovery point. Return 0 when OK }
function setjmp(setjmp_buffer : jmp_buf) : int;
forward;
{ Return control to the setjmp point }
procedure longjmp(setjmp_buffer : jmp_buf; flag : int);
forward;
{$ENDIF --------------------------------------------------------------------}
{ Here's the extended error handler struct: }
type
my_error_ptr = ^my_error_mgr;
my_error_mgr = record
pub : jpeg_error_mgr; { "public" fields }
setjmp_buffer : jmp_buf; { for return to caller }
end;
{ Here's the routine that will replace the standard error_exit method: }
{METHODDEF}
procedure my_error_exit (cinfo : j_common_ptr); far;
var
myerr : my_error_ptr;
begin
{ cinfo^.err really points to a my_error_mgr struct, so coerce pointer }
myerr := my_error_ptr (cinfo^.err);
{ Always display the message. }
{ We could postpone this until after returning, if we chose. }
cinfo^.err^.output_message (cinfo);
{ Return control to the setjmp point }
longjmp(myerr^.setjmp_buffer, 1);
end;
{ Sample routine for JPEG decompression. We assume that the source file name
is passed in. We want to return 1 on success, 0 on error. }
{GLOBAL}
function read_JPEG_file (filename : string) : boolean;
var
{ This struct contains the JPEG decompression parameters and pointers to
working space (which is allocated as needed by the JPEG library). }
cinfo : jpeg_decompress_struct;
{ We use our private extension JPEG error handler.
Note that this struct must live as long as the main JPEG parameter
struct, to avoid dangling-pointer problems. }
jerr : my_error_mgr;
{ More stuff }
infile : FILE; { source file }
buffer : JSAMPARRAY; { Output row buffer }
row_stride : int; { physical row width in output buffer }
begin
{ In this example we want to open the input file before doing anything else,
so that the setjmp() error recovery below can assume the file is open.
VERY IMPORTANT: use "b" option to fopen() if you are on a machine that
requires it in order to read binary files. }
Assign(infile, filename);
{$I-}
Reset(infile, 1);
{$IFDEF IoCheck} {$I+} {$ENDIF}
if (IOresult <> 0) then
begin
WriteLn(output, 'can''t open ', filename);
read_JPEG_file := FALSE;
exit;
end;
{ Step 1: allocate and initialize JPEG decompression object }
{ We set up the normal JPEG error routines, then override error_exit. }
cinfo.err := jpeg_std_error(jerr.pub);
jerr.pub.error_exit := my_error_exit;
jerr.pub.trace_level := 3; { I'm debbuging a lot (Nomssi) }
{ Establish the setjmp return context for my_error_exit to use. }
if (setjmp(jerr.setjmp_buffer)<>0) then
begin
{ If we get here, the JPEG code has signaled an error.
We need to clean up the JPEG object, close the input file, and return. }
{ Nomssi: if we get here, we are in trouble, because e.g. cinfo.mem
is not guaranted to be NIL }
jpeg_destroy_decompress(@cinfo);
system.close(infile);
read_JPEG_file := FALSE;
exit;
end;
{ Now we can initialize the JPEG decompression object. }
jpeg_create_decompress(@cinfo);
{ Step 2: specify data source (eg, a file) }
jpeg_stdio_src(@cinfo, @infile);
{ Step 3: read file parameters with jpeg_read_header() }
jpeg_read_header(@cinfo, TRUE);
{ We can ignore the return value from jpeg_read_header since
(a) suspension is not possible with the stdio data source, and
(b) we passed TRUE to reject a tables-only JPEG file as an error.
See libjpeg.doc for more info. }
{ Step 4: set parameters for decompression }
{ the defaults are set by jpeg_read_header(),
we could choose to do nothing here. }
cinfo.scale_num := 1;
cinfo.scale_denom := 1; { 1:1 scaling }
cinfo.dct_method := JDCT_IFAST;
cinfo.quantize_colors := TRUE;
cinfo.two_pass_quantize := TRUE;
cinfo.dither_mode := JDITHER_FS; { Floyd-Steinberg error diffusion dither }
{ Step 5: Start decompressor }
jpeg_start_decompress(@cinfo);
{ We can ignore the return value since suspension is not possible
with the stdio data source. }
{ We may need to do some setup of our own at this point before reading
the data. After jpeg_start_decompress() we have the correct scaled
output image dimensions available, as well as the output colormap
if we asked for color quantization.
In this example, we need to make an output work buffer of the right size. }
{ JSAMPLEs per row in output buffer }
row_stride := cinfo.output_width * cinfo.output_components;
{ Make a one-row-high sample array that will go away when done with image }
buffer := cinfo.mem^.alloc_sarray
(j_common_ptr(@cinfo), JPOOL_IMAGE, row_stride, 1);
{ Step 6: while (scan lines remain to be read) }
{ jpeg_read_scanlines(...); }
{ Here we use the library's state variable cinfo.output_scanline as the
loop counter, so that we don't have to keep track ourselves. }
while (cinfo.output_scanline < cinfo.output_height) do
begin
{ jpeg_read_scanlines expects an array of pointers to scanlines.
Here the array is only one element long, but you could ask for
more than one scanline at a time if that's more convenient. }
jpeg_read_scanlines(@cinfo, buffer, 1);
{ Assume put_scanline_someplace wants a pointer and sample count. }
put_scanline_someplace(buffer^[0], row_stride);
end;
{ Nomssi }
save_color_map(@cinfo);
{ Step 7: Finish decompression }
jpeg_finish_decompress(@cinfo);
{ We can ignore the return value since suspension is not possible
with the stdio data source. }
{ Step 8: Release JPEG decompression object }
{ This is an important step since it will release a good deal of memory. }
jpeg_destroy_decompress(@cinfo);
{ After finish_decompress, we can close the input file.
Here we postpone it until after no more JPEG errors are possible,
so as to simplify the setjmp error logic above. (Actually, I don't
think that jpeg_destroy can do an error exit, but why assume anything...) }
system.close(infile);
{ At this point you may want to check to see whether any corrupt-data
warnings occurred (test whether jerr.pub.num_warnings is nonzero). }
{ And we're done! }
read_JPEG_file := TRUE;
end;
{ SOME FINE POINTS:
In the above code, we ignored the return value of jpeg_read_scanlines,
which is the number of scanlines actually read. We could get away with
this because we asked for only one line at a time and we weren't using
a suspending data source. See libjpeg.doc for more info.
We cheated a bit by calling alloc_sarray() after jpeg_start_decompress();
we should have done it beforehand to ensure that the space would be
counted against the JPEG max_memory setting. In some systems the above
code would risk an out-of-memory error. However, in general we don't
know the output image dimensions before jpeg_start_decompress(), unless we
call jpeg_calc_output_dimensions(). See libjpeg.doc for more about this.
Scanlines are returned in the same order as they appear in the JPEG file,
which is standardly top-to-bottom. If you must emit data bottom-to-top,
you can use one of the virtual arrays provided by the JPEG memory manager
to invert the data. See wrbmp.c for an example.
As with compression, some operating modes may require temporary files.
On some systems you may need to set up a signal handler to ensure that
temporary files are deleted if the program is interrupted. See libjpeg.doc. }
end.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -