1 {
2 $Id: ImagingJpeg.pas 168 2009-08-22 18:50:21Z galfar $
3 Vampyre Imaging Library
4 by Marek Mauder
5 http://imaginglib.sourceforge.net
7 The contents of this file are used with permission, subject to the Mozilla
8 Public License Version 1.1 (the "License"); you may not use this file except
9 in compliance with the License. You may obtain a copy of the License at
10 http://www.mozilla.org/MPL/MPL-1.1.html
12 Software distributed under the License is distributed on an "AS IS" basis,
13 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
14 the specific language governing rights and limitations under the License.
16 Alternatively, the contents of this file may be used under the terms of the
17 GNU Lesser General Public License (the "LGPL License"), in which case the
18 provisions of the LGPL License are applicable instead of those above.
19 If you wish to allow use of your version of this file only under the terms
20 of the LGPL License and not to allow others to use your version of this file
21 under the MPL, indicate your decision by deleting the provisions above and
22 replace them with the notice and other provisions required by the LGPL
23 License. If you do not delete the provisions above, a recipient may use
24 your version of this file under either the MPL or the LGPL License.
26 For more information about the LGPL: http://www.gnu.org/copyleft/lesser.html
27 }
29 { This unit contains image format loader/saver for Jpeg images.}
32 {$I ImagingOptions.inc}
34 { You can choose which Pascal JpegLib implementation will be used.
35 IMJPEGLIB is version bundled with Imaging which works with all supported
36 compilers and platforms.
37 PASJPEG is original JpegLib translation or version modified for FPC
38 (and shipped with it). You can use PASJPEG if this version is already
39 linked with another part of your program and you don't want to have
40 two quite large almost the same libraries linked to your exe.
41 This is the case with Lazarus applications for example.}
43 {$DEFINE IMJPEGLIB}
44 { $DEFINE PASJPEG}
46 { Automatically use FPC's PasJpeg when compiling with Lazarus. But not when
47 WINDOWS is defined. See http://galfar.vevb.net/imaging/smf/index.php/topic,90.0.html}
48 {$IF Defined(LCL) and not Defined(WINDOWS)}
49 {$UNDEF IMJPEGLIB}
50 {$DEFINE PASJPEG}
51 {$IFEND}
53 interface
55 uses
57 {$IF Defined(IMJPEGLIB)}
60 {$ELSEIF Defined(PASJPEG)}
63 {$IFEND}
64 ImagingUtility;
66 {$IF Defined(FPC) and Defined(PASJPEG)}
67 { When using FPC's pasjpeg in FPC the channel order is BGR instead of RGB}
68 {$DEFINE RGBSWAPPED}
69 {$IFEND}
71 type
72 { Class for loading/saving Jpeg images. Supports load/save of
73 8 bit grayscale and 24 bit RGB images. Jpegs can be saved with optional
74 progressive encoding.
75 Based on IJG's JpegLib so doesn't support alpha channels and lossless
76 coding.}
78 private
80 protected
90 public
94 published
95 { Controls Jpeg save compression quality. It is number in range 1..100.
96 1 means small/ugly file, 100 means large/nice file. Accessible trough
97 ImagingJpegQuality option.}
99 { If True Jpeg images are saved in progressive format. Accessible trough
100 ImagingJpegProgressive option.}
104 implementation
106 const
113 const
114 { Jpeg file identifiers.}
118 resourcestring
121 type
144 var
148 { Intenal unit jpeglib support functions }
151 var
153 begin
154 { Create the message and raise exception }
160 begin
164 begin
166 Exit;
174 begin
179 var
182 begin
187 begin
199 var
201 begin
204 begin
206 begin
211 //Inc(LongInt(Src.Pub.next_input_byte), num_bytes);
217 var
219 begin
221 // Move stream position back just after EOI marker so that more that one
222 // JPEG images can be loaded from one stream
227 TImagingHandle);
228 var
230 begin
232 begin
251 var
253 begin
260 var
262 begin
271 var
274 begin
282 TImagingHandle);
283 var
285 begin
299 begin
301 // Set standard error handlers and then override some
313 begin
321 begin
323 // Set standard error handlers and then override some
332 else
340 { TJpegFileFormat class implementation }
343 begin
360 begin
361 // Check if option values are valid
368 var
374 {$IFDEF RGBSWAPPED}
376 {$ENDIF}
377 begin
378 // Copy IO functions to global var used in JpegLib callbacks
384 try
390 else
391 Exit;
401 begin
403 {$IFDEF RGBSWAPPED}
405 begin
408 begin
413 {$ENDIF}
418 begin
420 // Translate from CMYK to RGB
422 begin
433 finally
440 var
447 {$IFDEF RGBSWAPPED}
450 {$ENDIF}
451 begin
453 // Copy IO functions to global var used in JpegLib callbacks
455 // Makes image to save compatible with Jpeg saving capabilities
458 try
465 begin
468 end
469 else
470 begin
478 {$IFDEF RGBSWAPPED}
480 {$ENDIF}
484 begin
485 {$IFDEF RGBSWAPPED}
487 begin
491 begin
496 {$ELSE}
498 {$ENDIF}
506 finally
510 {$IFDEF RGBSWAPPED}
512 {$ENDIF}
518 begin
521 else
526 var
529 begin
533 begin
543 begin
547 initialization
550 {
551 File Notes:
553 -- TODOS ----------------------------------------------------
554 - nothing now
556 -- 0.26.3 Changes/Bug Fixes ---------------------------------
557 - Changed the Jpeg error manager, messages were not properly formated.
559 -- 0.26.1 Changes/Bug Fixes ---------------------------------
560 - Fixed wrong color space setting in InitCompressor.
561 - Fixed problem with progressive Jpegs in FPC (modified JpegLib,
562 can't use FPC's PasJpeg in Windows).
564 -- 0.25.0 Changes/Bug Fixes ---------------------------------
565 - FPC's PasJpeg wasn't really used in last version, fixed.
567 -- 0.24.1 Changes/Bug Fixes ---------------------------------
568 - Fixed loading of CMYK jpeg images. Could cause heap corruption
569 and loaded image looked wrong.
571 -- 0.23 Changes/Bug Fixes -----------------------------------
572 - Removed JFIF/EXIF detection from TestFormat. Found JPEGs
573 with different headers (Lavc) which weren't recognized.
575 -- 0.21 Changes/Bug Fixes -----------------------------------
576 - MakeCompatible method moved to base class, put ConvertToSupported here.
577 GetSupportedFormats removed, it is now set in constructor.
578 - Made public properties for options registered to SetOption/GetOption
579 functions.
580 - Changed extensions to filename masks.
581 - Changed SaveData, LoadData, and MakeCompatible methods according
582 to changes in base class in Imaging unit.
583 - Changes in TestFormat, now reads JFIF and EXIF signatures too.
585 -- 0.19 Changes/Bug Fixes -----------------------------------
586 - input position is now set correctly to the end of the image
587 after loading is done. Loading of sequence of JPEG files stored in
588 single stream works now
589 - when loading and saving images in FPC with PASJPEG read and
590 blue channels are swapped to have the same chanel order as IMJPEGLIB
591 - you can now choose between IMJPEGLIB and PASJPEG implementations
593 -- 0.17 Changes/Bug Fixes -----------------------------------
594 - added SetJpegIO method which is used by JNG image format
595 }