SaveAsJPEG changes image

I am taking a JPEG produced by a basic image program (Serif Photoplus) and trying to use it within XOJO. It is significantly changed (at least in my book.

Here is the small image

I have tried the following things.

  1. I opened the picture and SaveAsJPEG without any changes to the picture. The new JPEG is smaller and has significantly different colors from the original when I compared the two in Photoplus.
  2. I opened the picture myPic. Then I created a new white picture, myPic2, in XOJO and copied the colors from mypic to mypic2 using myPic2.Graphics.pixel(x,y)=RGB(c.red,c.green,c.blue,255). Outcome is the same when I do this.
  3. The original 46x36 pixel picture starts with 1540 bytes at the start. Open and Save in Xojo and size goes to 1148 bytes. Rename file in Windows Explorer. Repeat XOJO program and size to 1143>>>1144>>1140.

The code is below

[code] Dim x, y,xmax,ymax as integer
Dim f1,f2 As FolderItem
Dim d1 as OpenDialog
Dim myPic as Picture
Dim length as integer
Dim newname, newname2 as string
Dim c as color

d1=New OpenDialog
f1=d1.ShowModal()
If f1<>nil then
myPic=Picture.Open(f1)
end if

'myPic.save(f1,151) 'saved as original name, should be the same 1540 pixels, but went to 1148

Dim myPic2 as new Picture(myPic.Width,myPic.Height,32) 'created white picture

xmax=myPic.width-1
ymax=myPic.height-1

'copy colors before save
For x=0 to xmax
For y=0 to ymax
c=mypic.Graphics.Pixel(x,y)
myPic2.Graphics.pixel(x,y)=RGB(c.red,c.green,c.blue,255)
Next
Next

length=len(f1.absolutepath)
newname2=left(f1.absolutepath,length-4)+" saved after colors copied.jpg"
f2=GetFolderItem(newname3)
myPic2.save(f2,Picture.SaveAsJPEG)
[/code]

did you try these?
JPEG Quality Constants
If you are using the JPEG format, then there is an optional parameter for the quality of the JPEG. You specify the quality using the following class constants.
Constant Value
QualityDefault 1
QualityHigh 80
QualityLow 25
QualityMax 100
QualityMedium 50
QualityMin 0

Jpeg is a lossy format, so things will get lossy’d in the tranformation

Check the dpi on the original. Xojo only works at 72dpi and it could be that you’re losing data.

Has the original JPEG maybe also got a color profile embedded ?

My guess is that the colour shift is due to an ICC Profile which is in the original file but not in the new file. Your graphics application use the ICC Profile to map the RGB pixel values from the input device (ie: camera / scanner) to the output device (screen / printer).
The MBS plug-ins (and possibly the Einhuger plug-ins) would help you extract the profile and embed it into a new JPEG file. It might also be possible to do this via declares. Alternatively, you could also read, parse and write the JPEG header data with some Xojo code as the JPEG header format is not that difficult to understand.

The difference in file size is partially due to the JPEG file format being lossy so every time you read and re-save a JPEG you have potentially lost quality. Another reason would be that the original file contains the ICC Profile + other header data such as EXIF & IPTC tags which are not in the new file.

I would be surprised if it is related to DPI as it is only meta-data within the file to say what the size of a pixel is.

Thanks for the replies.

Dave, I tried quality and that didn’t fix.

Greg,The original is 300 dpi.

Norman, how can you tell about an embedded color profile?

Kevin, I have thought about getting the MBS plug-ins. I am a newbie programmer. I wanted to load the JPG and save as a PNG. When it wasn’t accurate, I tried saving in the same JPG format and it still doesn’t work. So I have been beating my head against the wall, not being able to figure out how to do something that seems basic.

I intended to attach the image I was playing with using the image icon in the forum box, and replaced the example.com/image.jpg with my file name, but it didn’t attach. Now do you use that?

Another thing I found confusing on the forum is that when you start a thread and save a draft, you are only presented with ‘Post a Reply’, which doesn’t seem like good terminology when you go to post the first time and aren’t replying.

Thanks,

Derek

See kevins reply
Some software will tell you IF it is reading & using the profile or not
I dont know the software you mentioned so I dont know if it does or can tell you if it is using the profile
But, given the color changes you say you see I would suspect it is

[quote=169963:@Derek Hartzell]Thanks for the replies.

Dave, I tried quality and that didn’t fix.

Greg,The original is 300 dpi.

Norman, how can you tell about an embedded color profile?

Kevin, I have thought about getting the MBS plug-ins. I am a newbie programmer. I wanted to load the JPG and save as a PNG. When it wasn’t accurate, I tried saving in the same JPG format and it still doesn’t work. So I have been beating my head against the wall, not being able to figure out how to do something that seems basic.

I intended to attach the image I was playing with using the image icon in the forum box, and replaced the example.com/image.jpg with my file name, but it didn’t attach. Now do you use that?

Another thing I found confusing on the forum is that when you start a thread and save a draft, you are only presented with ‘Post a Reply’, which doesn’t seem like good terminology when you go to post the first time and aren’t replying.

Thanks,

Derek[/quote]

Hi Derek.

Can’t comment on the forum software as I don’t really post that often. However, i’m sure there are plenty of folks who can help with any difficulties you have with it.

The problems you are running into with reading / writing pictures are more related image processing / graphic file formats / colour management rather than Xojo problems. JPEG / TIFF / PNG files can contain quite a lot of meta-data in additional to the actual pixel data so you need to be prepared to deal with this if you want your application to do the ‘right thing’. The plug-ins I mentioned allow you to read and write the meta-data as well as the pixels whereas the Xojo functions just deal with the everyday reading and writing of pixels. The MBS plug-ins also have support for libraries that allow you to perform ICC Colour management within your application.

One way you might be able to simplify this is if your graphics package can convert the picture to the sRGB colour space rather than embedding an ICC Profile. You can then just open / save the pictures without worrying about colour management as quite often, applications treat pictures with no ICC Profile as being in the sRGB colour space. However, I don’t know what you are trying to achieve so this may or may not work depending on what your goal is.

Kev.

mabye load MBS Plugin and check examples?
I think we have a couple of examples for image files with ICC Profiles and matching image to new profile and saving.

Properly related to this: https://forum.xojo.com/15074-capture-rgb-values/p2#p123951