1
votes

I am trying to access files that are stored as Jpeg files, is there an easy way to display these image files without performance loss ?

5

5 Answers

2
votes

You can load the JPeg file using an instance of TJPEGImage and then assign it to a TBitmap to display. You find TJPEGImage in unit jpeg.

jpeg := TJPEGImage.Create;
jpeg.LoadFromFile('filename.jpg');
bitm := TBitmap.Create;
bitm.Assign(jpeg); 

Image1.Height := bitm.Height;
Image1.Width := bitm.Width;
Image1.Canvas.Draw(0, 0, bitm);

Alternatively, this should also work:

bitm := TBitmap.Create;
bitm.Assign('filename.jpg'); 

Image1.Height := bitm.Height;
Image1.Width := bitm.Width;
Image1.Canvas.Draw(0, 0, bitm);
2
votes

I found this page!

http://cc.embarcadero.com/Item/19723

Enhanced jpeg implementation

Author: Gabriel Corneanu

This unit contains a new jpeg implementation (based on Delphi original)

  • fixed bug accessing one pixel height picture
  • added lossless transformation support for jpeg images (based on Thomas G. Lane C library - visit jpegclub.org/jpegtran )
  • added CMYK support (read only)
  • compiled for D5-2010 AND BCB5-6-CMYK to RGB fast MMX conversion (not for Delphi5, lack of MMX ASM) (fallback to simple pascal implementation if not available)
  • fixed bug in Delphi 5 ASM (CMYK to RGB function)

You only need the jpeg.dcu file; it can be copied to program directory or to the LIB directory.I generated obj and hpp files for use with CBuilder 5 and 6 also.This is what you need to use it:

This is just an enum

TJpegTransform = ( 
     jt_FLIP_H,     { horizontal flip }
     jt_FLIP_V,     { vertical flip }
     jt_TRANSPOSE,  { transpose across UL-to-LR axis } 
     jt_TRANSVERSE, { transpose across UR-to-LL axis } 
     jt_ROT_90,     { 90-degree clockwise rotation } 
     jt_ROT_180,    { 180-degree rotation } 
     jt_ROT_270     { 270-degree clockwise (or 90 ccw) } 
);

procedure Crop(xoffs, yoffs, newwidth, newheight: integer); this method is cropping the image

procedure Transform(Operation: TJpegTransform);this method is applying the specified transformation; read the transupp.h comments about limitations(my code is using crop option)

property IsCMYK: boolean read FIsCMYK; this will indicate if the last jpeg image loaded is CMYK encoded

property InverseCMYK: boolean read FInverseCMYK write SetInverseCMYK;if set (default, because I could only find this kind of images), the CMYK image is decoded with inversed CMYK values (I read that Photoshop is doing this).

The jpegex is the same unit compiled with a different name. It can be used to avoid conflicts when you have other components without source code linking to the original jpeg unit. In this case you might need to use qualified class names to solve names conflict: jpegex.TJpegImage.xxx. Be carefull when you use both versions in one program: even though the classes have the same name, they are not identical and you can't cast or assign them directly. The only way to exchange data is saving to/loading from a stream.

Send comments to: gabrielcorneanuATyahooDOTcom

1
votes

I dont believe D7 can handle CMYK JPEG's.

If you cant open it using the JPEG unit as Ralph posted, you might consider using something like GDI+ to load the graphic file.

1
votes

Actually, I once modified Jpeg.pas unit to partial CMYK support. Basically after

jpeg_start_decompress(jc.d) 

you should check

if jc.d.out_color_space = JCS_CMYK then

and if true following jpeg_read_scanlines will get 4 bytes data instead of 3 bytes.

Also cinfo.saw_Adobe_marker indicates inverted values (probably Adobe was first who introduced CMYK jpeg variation).

But the most difficult part is CMYK-RGB conversion. Since there's no universal formula, in best systems it's always table approach. I tried to find some simple approximation, but there's always a picture that does not fit. Just as an example, don't use this formulas as a reference:

 R_:=Max(254 - (111*C +  2*M  +  7*Y  + 36*K) div 128, 0);
 G_:=Max(254 - (30*C  + 87*M  + 15*Y  + 30*K) div 128, 0);
 B_:=Max(254 - (15*C  + 44*M  + 80*Y  + 24*K) div 128, 0);
1
votes

Easy!

I implemented the CMYK conversion in the JPEG.PAS

Include it in your project to handle CMYK JPEG's

Get it here: http://delphi.andreotti.nl/