ImageEn for Delphi and C++ Builder ImageEn for Delphi and C++ Builder

 

ImageEn Forum
Profile    Join    Active Topics    Forum FAQ    Search this forumSearch
Forum membership is Free!  Click Join to sign-up
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 ImageEn Library for Delphi, C++ and .Net
 ImageEn and IEvolution Support Forum
 Tiff Color JPEG to Grayscale
 New Topic  Reply to Topic
Author Previous Topic Topic Next Topic  

kandc

29 Posts

Posted - Mar 05 2014 :  09:03:23  Show Profile  Reply
I have the following code:

MView1.Proc.ConvertToGray;
MView1.MIO.Params [PageIndex].BitsPerSample := 8;
MView1.MIO.Params [PageIndex].SamplesPerPixel := 1;

There is only 1 page in this particular file. The conversion to gray works, however, the SamplesPerPixel does not remain at 1, it goes to 3. I have seen other posts with a similar problem, but there is not clear resolution.

Thanks.

w2m

USA
1990 Posts

Posted - Mar 05 2014 :  10:18:33  Show Profile  Reply
Convert to gray results in a 24-bit image.

Maybe this will help:
var
  iColorMap256: array [0 .. 255] of TRGB;
begin
  MView1.Proc.ConvertToGray;
  MView1.Proc.ConvertToPalette(256, @iColorMap256, ieOrdered);
  MView1.MIO.Params[PageIndex].BitsPerSample := 8;
  MView1.MIO.Params[PageIndex].SamplesPerPixel := 1;
end;

By rights, when you set the SamplePerPixel to 1, and then save the image, the value should be retained and the resulting image should be 8-Bit, so perhaps the problem is elsewhere in your project. The fact that you set SamplePerPixel to 1 but after saving the image SamplesPerPixel is 3 as a result of the image probably being 24-bit (SamplesPerPixel = 3) as returned by ConvertToGray. I can not explain why the set SamplePerPixel value is changing on your project; however, because there could be other code you have not shown that causes the problem.

I might add that I usually do not change the MIO.Params, I change the IO.Params with ImageEnView, then copy the image to the ImageEnMView item because for me it is easier to do it that way... so I could be wrong.

William Miller
Adirondack Software & Graphics
Email: w2m@frontiernet.net
EBook: http://www.imageen.com/ebook/
Apprehend: http://www.frontiernet.net/~w2m/index.html
Custom ImageEn Development
Go to Top of Page

kandc

29 Posts

Posted - Mar 05 2014 :  11:23:41  Show Profile  Reply
William, thanks for the assistance.

I have linearized the code as follows and stepped through it. It shows 3 on the debug line.

The purpose of this code is to allow the user to convert images that may have been scanned in high resolution full color, and potentially uncompressed, to compress them down and store in grayscale. The only code missing below is the loop that goes through each tiff image, although in this example there is only 1 page. The original image is color jpeg.

Thanks,

Kevin

MView1.Proc.ConvertToGray;
MView1.Proc.ConvertToPalette (256,@iColorMap256,ieOrdered);
MView1.MIO.Params [PageIndex].BitsPerSample := 8;
MView1.MIO.Params [PageIndex].SamplesPerPixel := 1;

ImageStream.Clear;
MView1.MIO.SaveToStreamTiff (ImageStream);
MView1.Clear;
ImageStream.Position := 0;
MView1.MIO.LoadFromStream(ImageStream);

if (MView1.mio.params [0].samplesperpixel = 12345) then halt; // for debugging
Go to Top of Page

w2m

USA
1990 Posts

Posted - Mar 05 2014 :  11:44:34  Show Profile  Reply
Where do you get PageIndex from? You still are not showing all your code. Is it possible that page index is not correct and you are setting the bitdepth of a different image?

Also why are you saving the image to a stream and then just reloading it from the same stream? When you do this params could be reset....

William Miller
Go to Top of Page

kandc

29 Posts

Posted - Mar 05 2014 :  11:59:27  Show Profile  Reply
Okay.... I know you don't me from Adam, but I think I got that part handled. Pageindex = 0. But...

this is now all the code (the loop was commented out)

To be honest, I think there is something loose in the imageen code. Others too have posted strange issues with this process. And, in separate debug sessions, it did show a value of 1 one time. I could not repeat it tho. Tells me something is not stable.

MView1.Proc.ConvertToGray;
MView1.Proc.ConvertToPalette (256,@iColorMap256,ieOrdered);
MView1.MIO.Params [0].BitsPerSample := 8;
MView1.MIO.Params [0].SamplesPerPixel := 1;

Image.ImageStream.Clear;
MView1.MIO.SaveToStreamTiff (Image.ImageStream);

MView1.Clear;
Image.ImageStream.Position := 0;
MView1.MIO.LoadFromStream(Image.ImageStream);
if (MView1.mio.params [0].samplesperpixel = 12345) then halt;
Go to Top of Page

w2m

USA
1990 Posts

Posted - Mar 05 2014 :  12:10:14  Show Profile  Reply
I just did the same thing you are doing here with version 5.0.6, but I suspect the version does not matter and when I set the samples per pixel it retains the correct value:
procedure TForm1.GrayScale1Click(Sender: TObject);
begin
  ImageEnMView1.Proc.ConvertToGray;
  ImageEnMView1.MIO.Params[ImageEnMView1.SelectedImage].BitsPerSample := 8;
  ImageEnMView1.MIO.Params[ImageEnMView1.SelectedImage].SamplesPerPixel := 1;
  StatusBar1.Panels[3].Text := 'BitsPerSample: ' +
    IntToStr(ImageEnMView1.MIO.Params[ImageEnMView1.SelectedImage]
    .BitsPerSample);
  StatusBar1.Panels[4].Text := 'SamplesPerPixel: ' +
    IntToStr(ImageEnMView1.MIO.Params[ImageEnMView1.SelectedImage]
    .SamplesPerPixel);
end;

procedure TForm1.ImageEnMView1Click(Sender: TObject);
{ ImageEnMView Click. }
begin
  StatusBar1.Panels[3].Text := 'BitsPerSample: ' +
    IntToStr(ImageEnMView1.MIO.Params[ImageEnMView1.SelectedImage]
    .BitsPerSample);
  StatusBar1.Panels[4].Text := 'SamplesPerPixel: ' +
    IntToStr(ImageEnMView1.MIO.Params[ImageEnMView1.SelectedImage]
    .SamplesPerPixel);
end;

After I convert the selected image to grayscale, then select another 24-Bit jpg image in ImageEnMView1 the SamplesPerPixel is 3 which is what is expected. When I select the image converted to grayscale the SamplesPerPixel is 1 as it was set for the image. Again... what you would expect.

What this means is you are not showing all your code and something else you have coded is changing the SamplesPerPixel value.

With respect to ImageEn being unstable in regard to samplespepixel, I believe you are wrong. I can not duplicate the problem and I do not ever see this problem here.

William Miller
Adirondack Software & Graphics
Email: w2m@frontiernet.net
EBook: http://www.imageen.com/ebook/
Apprehend: http://www.frontiernet.net/~w2m/index.html
Custom ImageEn Development
Go to Top of Page

w2m

USA
1990 Posts

Posted - Mar 05 2014 :  12:17:06  Show Profile  Reply
Your stream code does not make any sense to me. Why are you using the stream like you show it?

Also MView1.Proc.ConvertToPalette (256,@iColorMap256,ieOrdered); is not necessary... the code works without it.

William Miller
Go to Top of Page

kandc

29 Posts

Posted - Mar 05 2014 :  12:43:58  Show Profile  Reply
Image is separate object that holds many properties including a stream. We're in the weeds on this issue.... I will simplify the code again...
Go to Top of Page

kandc

29 Posts

Posted - Mar 05 2014 :  12:51:30  Show Profile  Reply
Here is the actual byte for byte code. Just ran it, value is 3.

procedure TFormImageAttributes.OnClickOkay(Sender: TObject);
var
MS : TMemoryStream;

begin
MS := TMemoryStream.Create;
try
ImageViewThumbNails.Proc.ConvertToGray;
ImageViewThumbNails.MIO.Params [0].BitsPerSample := 8;
ImageViewThumbNails.MIO.Params [0].SamplesPerPixel := 1;

ImageViewThumbnails.MIO.SaveToStreamTiff (MS);

ImageViewThumbnails.Clear;
MS.Position := 0;
ImageViewThumbnails.MIO.LoadFromStream(MS);

// check value of samplesperpixel..... still 3!

if (ImageViewThumbnails.mio.params [0].samplesperpixel = 12345) then halt;

finally
MS.Free;
end;
end;
Go to Top of Page

kandc

29 Posts

Posted - Mar 05 2014 :  13:33:21  Show Profile  Reply
I debugged the ImageEn Code, specifically unit TiffITL.pas. I found the following code being executed:

WriteSingleShort(imed, 277, 3); // SamplesPerPixel: 3 sample x pixel

That seemed odd, so in debugging further, I found that the TIFF_JpegColorSpace property was set to ioJPEG_YCbCr. By changing it to ioJPEG_RGB or ioJPEG_GRAYLEV, then it "appears" the problem is resolved.

In the programmers introduction guide, I did not find this property discussed and the online help has very little to say about it either. How is this property normally set and controlled?

Thanks,

Kevin
Go to Top of Page

w2m

USA
1990 Posts

Posted - Mar 05 2014 :  14:04:56  Show Profile  Reply
If you want YCbCr encoding then do this:
ImageEnView1.IO.Params.JPEG_ColorSpace := ioJPEG_YCbCr;
ImageEnView1.IO.Params.JPEG_Quality := 90;
ImageEnView1.IO.SaveToFile('out.jpg');

If you want ioJPEG_RGB encoding then do this:
ImageEnView1.IO.Params.JPEG_ColorSpace := ioJPEG_RGB;
ImageEnView1.IO.Params.JPEG_Quality := 90;
ImageEnView1.IO.SaveToFile('out.jpg');

William Miller
Adirondack Software & Graphics
Email: w2m@frontiernet.net
EBook: http://www.imageen.com/ebook/
Apprehend: http://www.frontiernet.net/~w2m/index.html
Custom ImageEn Development
Go to Top of Page

kandc

29 Posts

Posted - Mar 05 2014 :  14:10:57  Show Profile  Reply
I think you meant ioJPEG_RGB.

Regardless, ConvertToGray along with setting the bits 1/8 does not work in itself. That might explain why others were experiencing a similar problem as found through a search of this forum.

I guess the question is how to know which one to choose since it does affect whether it goes to grayscale or not. In my case, I noticed it because I was trying to show that the image properties were grayscale as opposed to color, when it kept coming returning as color.

Is there additional documentation that shows how to use these values? How is one to know?
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
Jump To: