Recent

Author Topic: fcl-image, how to control cfRGB24 or cfRGBA32 for TFPWriterTIFF  (Read 1729 times)

han

  • Jr. Member
  • **
  • Posts: 63
fcl-image, how to control cfRGB24 or cfRGBA32 for TFPWriterTIFF
« on: February 12, 2019, 09:44:24 pm »
For my program I have to write an arrray to TIFF files in RGB48. Using fcl-image, I have a routine which works, but the TPFwriterTIFF writes in  format cfRGBA32 and the reader only reads correctly format cfRGB24. Reading the cfrgb32 it shows strange vertical stripes without much color. Other programs can read the produced cfrgb32 format correctly.

How can I control the TfpwriterTIFF  format from cfRGBA32 to cfRGB24 or better cfpRGB48 or why can't the TfpreaderTIFF read format cfRGBA32? Any suggestion?

My write code:
Code: [Select]
uses FPImage,  fpwriteTIFF;
...
var
  image: TFPCustomImage;
  writer: TFPCustomImageWriter;
  thecolor  :Tfpcolor;
  img_loaded = array of array of array of Single;       

begin
....
....
    Image := TFPMemoryImage.Create(width2, height2);
    Writer := TFPWriterTIFF.Create;
    Image.Extra['TiffCompression']:= '5'; // compression LZW

      For i:=0 to height2-1 do
        for j:=0 to width2-1 do
        begin
          thecolor.red:=min(round(img_loaded[0,j,height2-1-i]), $FFFF);
          thecolor.green:=min(round(img_loaded[1,j,height2-1-i]), $FFFF);
          thecolor.blue:=min(round(img_loaded[2,j,height2-1-i]), $FFFF);
          thecolor.alpha:=65535;
          image.Colors[j,i]:=thecolor;
        end;
      Image.SaveToFile(savedialog1.filename, Writer)


My reader:
Code: [Select]
uses FPImage,  fpreadTIFF;
.....
var
  i,j  : integer;
  image: TFPCustomImage;
  reader: TFPCustomImageReader;
  img_loaded2 = array of array of array of Single;       

begin
  .......
  .......
  Image := TFPMemoryImage.Create(10, 10);
  Reader :=  TFPReaderTIFF.Create;
  Image.LoadFromFile(filen, Reader);
  width2:=image.width;
  height2:=image.height;

  setlength(img_loaded2,naxis3,width2,height2);
  For i:=0 to height2-1 do
      for j:=0 to width2-1 do
      begin
        img_loaded2[0,j,height2-1-i]:=image.Colors[j,i].red;
        img_loaded2[1,j,height2-1-i]:=image.Colors[j,i].green;
        img_loaded2[2,j,height2-1-i]:=image.Colors[j,i].blue;
      end;


« Last Edit: February 12, 2019, 10:07:33 pm by han »

han

  • Jr. Member
  • **
  • Posts: 63
Re: fcl-image, how to control cfRGB24 or cfRGBA32 for TFPWriterTIFF
« Reply #1 on: February 12, 2019, 10:23:54 pm »
Problem partly solved. Writing RGB48 can be forced like this:

Code: [Select]
      Image.Extra['TiffAlphaBits']:='0';
      Image.Extra['TiffRedBits']:='16';
      Image.Extra['TiffGreenBits']:='16';
      Image.Extra['TiffBlueBits']:='16';

I think this is nowhere documented, should be in the Wiki.

Strange enough the reader can read RGB24, RGB48 and RGB64 but not RGB32? Is this Bug?

The other thing still unclear is how to write TIFF gray images. Setting TiffPhotometric to zero should do the trick but it doesn't work. Can't run the code in the debugger to find the problem. Anybody an idea?

  Image.Extra['TiffGrayBits']:='16';
  Image.Extra['TiffPhotoMetric']:='0';

Reported this as a bug:
https://bugs.freepascal.org/view.php?id=35081
« Last Edit: February 14, 2019, 12:29:04 am by han »

circular

  • Hero Member
  • *****
  • Posts: 3022
    • Personal webpage
Re: fcl-image, how to control cfRGB24 or cfRGBA32 for TFPWriterTIFF
« Reply #2 on: February 15, 2019, 12:51:50 pm »
Hi Han,

I think it is fixed in latest version of FPReadTiff. Though it may not released yet.

In the meantime, you can use BGRABitmap which includes latest TIFF reader.
Conscience is the debugger of the mind

han

  • Jr. Member
  • **
  • Posts: 63
Re: fcl-image, how to control cfRGB24 or cfRGBA32 for TFPWriterTIFF
« Reply #3 on: February 21, 2019, 08:06:19 pm »
Thank for the info.

Problem 1) Yes the read RGB32 is solved in the  BGRABitmap and the latest trunk.
Problem 2) writing Gray16 is still there. It can't write Gray16/Gray8. It always write the three colors.
« Last Edit: February 21, 2019, 09:02:20 pm by han »

circular

  • Hero Member
  • *****
  • Posts: 3022
    • Personal webpage
Re: fcl-image, how to control cfRGB24 or cfRGBA32 for TFPWriterTIFF
« Reply #4 on: February 21, 2019, 11:15:28 pm »
Ok.
Conscience is the debugger of the mind

han

  • Jr. Member
  • **
  • Posts: 63
Re: fcl-image, how to control cfRGB24 or cfRGBA32 for TFPWriterTIFF
« Reply #5 on: May 06, 2019, 06:03:28 pm »
For the record this is now solved. After adding the unit  ”fptiffcmn” you specify using const:

Code: [Select]
    Image.Extra[TiffGrayBits]:='16';   {Without Apostrophe!!}
    Image.Extra[TiffPhotoMetric]:='0'; 

This is the same as using strings:
Code: [Select]
   Image.Extra['TiffGrayBits']:='16';   
   Image.Extra['TiffPhotoMetricInterpretation']:='0';

Also this specified with strings:
Code: [Select]
      Image.Extra['TiffAlphaBits']:='0';
      Image.Extra['TiffRedBits']:='16';
      Image.Extra['TiffGreenBits']:='16';
      Image.Extra['TiffBlueBits']:='16';

can be be better written as:
Code: [Select]
    Image.Extra[TiffRedBits]:='16';
    Image.Extra[TiffGreenBits]:='16';
    Image.Extra[TiffBlueBits]:='16';
    Image.Extra[TiffGrayBits]:='16';

So the compiler will check the specified const

The only outlier is: TiffPhotoMetric = 'TiffPhotoMetricInterpretation',  see unit fptiffcmn.pas
« Last Edit: May 06, 2019, 06:10:48 pm by han »