Recent

Author Topic: Strange behavior  (Read 5045 times)

apexcol

  • Jr. Member
  • **
  • Posts: 54
Strange behavior
« on: December 15, 2017, 03:03:40 am »
Trying to re-learn how to code in graphics has been interesting, I didn't know how to deal with FPGraphics, only the Delphi way... and it's really good the new improvement, more easy (my opinion).

but I found something really weird... the final file JPG has different colors than the BMP, any ideas what is wrong?

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button2Click(Sender: TObject);
  2. var
  3.   R: TRect;
  4.   imgFCL: TFPCustomImage;
  5.   canvasFCL: TFPCustomCanvas;
  6.   bmpLCL: TBitmap;
  7.   RGB1: TFPColor;
  8.   writerJPG: TFPCustomImageWriter;
  9.   writerBMP: TFPCustomImageWriter;
  10. begin
  11.   R:=Rect(10,20,130,140);
  12.   imgFCL:=TFPMemoryImage.create(100,100);
  13.   canvasFCL:=TFPImageCanvas.create(imgFCL);
  14.  
  15.   with rgb1 do begin
  16.     red:=$A000;
  17.     green:=$0;
  18.     blue:=$0;
  19.     alpha:=$ffff;
  20.   end;
  21.  
  22.   canvasFCL.Pen.FPColor := rgb1;
  23. //  canvasFCL.Pen.Mode := pmNotXor;
  24.  
  25.   with rgb1 do begin
  26.     red:=$FFFF;
  27.     green:=$FFFF;
  28.     blue:=$FFFF;
  29.     alpha:=$0;
  30.   end;
  31.   canvasFCL.Brush.FPColor := rgb1;
  32.   canvasFCL.Ellipse(Rect(10,10,90,90));
  33.  
  34.   bmpLCL := TBitmap.Create;
  35.   bmpLCL.SetSize(100,100);
  36.   TFPCustomCanvas(bmpLCL.Canvas).CopyRect(0, 0, canvasFCL, Rect(0,0,100,100));
  37.  
  38.   Canvas.Draw(0,0,bmpLCL);
  39.   bmpLCL.SaveToFile('c:\imagen.bmp');
  40.   writerJPG := TFPWriterJPEG.Create;
  41.   with writerJPG as TFPWriterJPEG do begin
  42.     CompressionQuality := 100;
  43.     ProgressiveEncoding := false;
  44.     GrayScale := false;
  45.   end;
  46.   imgFCL.SaveToFile('c:\imagen.jpeg', writerJPG);
  47.  
  48.   writerBMP := TFPWriterBMP.Create;
  49.   imgFCL.SaveToFile('c:\imagenB.bmp', writerBMP);
  50.  
  51.   writerJPG.Free;
  52.   writerBMP.Free;
  53.   bmpLCL.Free;
  54.   imgFCL.Free;
  55.   canvasFCL.Free;
  56. end;


molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Strange behavior
« Reply #1 on: December 15, 2017, 03:37:36 am »
Without seeing the result, i would suspect the alpha channel.

See color transparent bitmaps in the wiki. BMP does not do with alpha channel as you would expect.

Handoko

  • Hero Member
  • *****
  • Posts: 5130
  • My goal: build my own game engine using Lazarus
Re: Strange behavior
« Reply #2 on: December 15, 2017, 04:32:43 am »
I modified the TS' code to use paramstr(0) as the export path and make the code compilable. Anyone interested can use the test.zip.

I think the TS issue is the color changed after saved to jpeg format. JPEG is lossy compression format, so quality loss is common especially when converting image from bmp/png to jpeg.
https://en.wikipedia.org/wiki/Lossy_compression

Below I provide the result image in 300% zoom.

apexcol

  • Jr. Member
  • **
  • Posts: 54
Re: Strange behavior
« Reply #3 on: December 15, 2017, 04:49:54 am »
Using tools like Photoshop or Gimp you can save the same quality modifying options, as you can see in my code, alpha in both Pen and brush can be modified to $FFFF and the results are going to be the same... so something is wrong...
« Last Edit: December 15, 2017, 04:51:58 am by apexcol »

Handoko

  • Hero Member
  • *****
  • Posts: 5130
  • My goal: build my own game engine using Lazarus
Re: Strange behavior
« Reply #4 on: December 15, 2017, 05:23:09 am »
Based on my own experience about jpeg, the worst color changes are the pixels on the contrast area. The red circle is very thin and it is on the contrast area (black and white), so the whole red circle is the worst. If you want, you can test with much thicker red circles to see the different.

The right one on the picture below is the image saved to 100% quality jpeg using GIMP. To maintain its original quality, the picture format is PNG. Because GIMP also gives the same (/similar) result, I consider nothing is wrong. But I am open to hear if someone can provide more tests.
« Last Edit: December 15, 2017, 05:45:04 am by Handoko »

apexcol

  • Jr. Member
  • **
  • Posts: 54
Re: Strange behavior
« Reply #5 on: December 15, 2017, 06:01:40 am »
Sorry, but we have to look with more care... as I said, there's a problem... proof of that it was:

I opened the image.bmp and saved it as imagegimp.jpg with the same options as I put on the code, and guess what... it's exactly the same color!!!  so?

(of course I reopened the jpg file again! :) )
« Last Edit: December 15, 2017, 06:04:25 am by apexcol »

Handoko

  • Hero Member
  • *****
  • Posts: 5130
  • My goal: build my own game engine using Lazarus
Re: Strange behavior
« Reply #6 on: December 15, 2017, 06:30:19 am »
Interesting, but I don't fully understand. Please provide more tests, images or codes.

---edit---

and guess what... it's exactly the same color!!!  so?

There should be something wrong with your GIMP :o

What is the jpeg size? Is it smaller or bigger compare to its original png file? How did you save it to jpeg? I use save for web: Gimp main menu > File > Save for Web.

Maybe you can do more tests but using other programs, Ms Paint maybe.
« Last Edit: December 15, 2017, 06:43:16 am by Handoko »

apexcol

  • Jr. Member
  • **
  • Posts: 54
Re: Strange behavior
« Reply #7 on: December 15, 2017, 07:34:45 am »
hahahahaha... I just downloaded it on Windows!!!!  by the way, I've been designer also so many year ago, the same situation in photoshop...

by the way, I added this line, and it shows in the screen all OK, the problem is saving it... something is wrong inside the writer handle...

  Canvas.Draw(101,0,jpgLCL);

I tried it also with my mac, nothing is wrong with Gimp...

When you "Save for the Web" of course it downgrades the picture, I modified the options to 100 quality on Gimp... and I'll give you an image of the files and file sizes...
« Last Edit: December 15, 2017, 07:39:06 am by apexcol »

apexcol

  • Jr. Member
  • **
  • Posts: 54
Re: Strange behavior
« Reply #8 on: December 15, 2017, 07:39:37 am »
Here's the files comparison...

Handoko

  • Hero Member
  • *****
  • Posts: 5130
  • My goal: build my own game engine using Lazarus
Re: Strange behavior
« Reply #9 on: December 15, 2017, 08:05:48 am »
I rarely use "File > Export" to save to Jpeg in Gimp. Okay the result was similar to yours (the color unchanged). But I tried doing the same using Krita (100% Jpeg quality), the result color was the same as Gimp using Save for Web.

- Result of TFPWriterJPEG is similar to Gimp Save for Web and Krita Export.
- Result from Gimp Export and Save for Web are different (although both are 100% quality).

Because TFPWriterJPEG gives similar result as Gimp Save for Web and Krita, we cannot said it is wrong. We need more tests. I use Linux, I can only test it using Gimp and Krita.
« Last Edit: December 15, 2017, 08:10:38 am by Handoko »

bigeno

  • Sr. Member
  • ****
  • Posts: 266
Re: Strange behavior
« Reply #10 on: December 15, 2017, 09:37:18 am »
FPC based on the older version of jpeglib 6b. For sure there are some big changes on current 9b.

apexcol

  • Jr. Member
  • **
  • Posts: 54
Re: Strange behavior
« Reply #11 on: December 17, 2017, 02:35:03 am »
I can see also that Pen.Width on TFPImageCanvas does not work... :(
it only paints 1px wide...

 

TinyPortal © 2005-2018