Lingo, LiveCode, and Differing Results

Want to move your code and projects to LiveCode but don't know where to start?

Moderators: heatherlaine, Klaus, FourthWorld, robinmiller

Luisa Klose
Posts: 48
Joined: Tue Aug 30, 2011 2:24 pm

Lingo, LiveCode, and Differing Results

Post by Luisa Klose » Wed Sep 14, 2011 12:12 am

Hello,

My handler doesn't work properly in LiveCode, so here I am in search of an answer that will save me from having to spend any more of my time looking for something that may not even be there.

The handler posterizes an image with results identical to Photoshop's posterize algorithm. Identical, but only in Director. In LiveCode, the results are just a little off and I don't know why. The handler is exactly the same.

At first, I thought that the difference was Lingo's "floatPrecision", but that only affects the way numbers are displayed, and it works the same with different values.

I can't get the handler to give me the same results in LiveCode.

Is there a difference in the way LiveCode does math calculations? Perhaps I'm overlooking something.

Regards,

Luisa Klose

WaltBrown
Posts: 466
Joined: Mon May 11, 2009 9:12 pm
Location: NE USA

Re: Lingo, LiveCode, and Differing Results

Post by WaltBrown » Wed Sep 14, 2011 3:00 am

Luisa,
How are the results "a little off"?
Walt
Walt Brown
Omnis traductor traditor

Luisa Klose
Posts: 48
Joined: Tue Aug 30, 2011 2:24 pm

Re: Lingo, LiveCode, and Differing Results

Post by Luisa Klose » Wed Sep 14, 2011 3:24 am

Walt,

It's close, visually, but apparently some of the values are being calculated differently from what I expect, and it's adding an extra level at some posterization values.

The algorithm calls for rounding the numbers down, so in the original script I had to create a couple of variables to compare. Lingo doesn't have trunc or div functions as far as I know.

Anyway, I wrote it in LiveCode both ways (with the original Lingo comparisons) and with the trunc function. Both ways return exactly the same close but no cigar result.

Searching around, I read a thread on the Runtime Revolution mailing list about decimal precision & how some applications correct for the errors. It's all a little beyond me.

Luisa

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3207
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Lingo, LiveCode, and Differing Results

Post by bn » Wed Sep 14, 2011 10:28 am

Hi Luisa,

without seeing what the difference is and without any code it is hard to guess what the difference could be.

You might also want to give the 'export with palette' command a try (dictionary)
The colorlist might also be a means of fine tuning your posterization


here are some variations of the syntax:

Code: Select all

on mouseUp
   set the dontDither of image 1 to true -- or false
   --export image 1 to myVar as png with standard palette
   --export image 1 to myVar as png with optimized  palette
   --export image 1 to myVar as png with 16 color palette
   
   repeat with i = 1 to 10 -- just creating any number between 0 and 255
      put 255 div i & return after tColorList
   end repeat
   delete last char of tColorList -- a trailing return
   
   export image 1 to myVar as png with palette tColorLIst
   
   put myVar into image 1
end mouseUp
Kind regards

Bernd

WaltBrown
Posts: 466
Joined: Mon May 11, 2009 9:12 pm
Location: NE USA

Re: Lingo, LiveCode, and Differing Results

Post by WaltBrown » Wed Sep 14, 2011 10:50 am

Luisa,
Is this the kind of variations you see? I played around with manually assisting the rounding/truncation, but in the end it seems LC does just fine :-)
Walt

Oops, forgot to lockloc one of the images, new zip file.
Attachments
posterize_walt_3.zip
(137.8 KiB) Downloaded 320 times
Walt Brown
Omnis traductor traditor

Luisa Klose
Posts: 48
Joined: Tue Aug 30, 2011 2:24 pm

Re: Lingo, LiveCode, and Differing Results

Post by Luisa Klose » Wed Sep 14, 2011 2:47 pm

Thanks for responding, guys, and thank you for your effort, Walt.

Here is the result I get from Photoshop and from Lingo:
sample_ps_lingo.gif
And here is the result I get from LiveCode:
sample_livecode.gif
Both images were done with a posterization value of 6.

The stack should be attached as well.

Why I should have to "manually assist" the rounding/truncation is what I want to know.

Regards,

Luisa

Edit to add: When I converted the images to indexed color, I learned that the PS/Lingo version has 62 colors. The LiveCode version has 70.
Attachments
Image_Effects.zip
(136.71 KiB) Downloaded 337 times

Luisa Klose
Posts: 48
Joined: Tue Aug 30, 2011 2:24 pm

Re: Lingo, LiveCode, and Differing Results

Post by Luisa Klose » Wed Sep 14, 2011 3:30 pm

One special request, please, Walt & Bernd.

Show me where, in my code, I need to "manually assist" LiveCode. Without rewriting the handler, please. It will be easier for me to understand what I have to do.

My LiveCode airbrush has some color issues and now I'm wondering if I have to manually assist that as well.

Thank you.

Luisa

WaltBrown
Posts: 466
Joined: Mon May 11, 2009 9:12 pm
Location: NE USA

Re: Lingo, LiveCode, and Differing Results

Post by WaltBrown » Wed Sep 14, 2011 4:38 pm

Luisa,
1) I added this to the stack script above applyPosterize preserve the original picture data so the stack could run multiple times:

Code: Select all

global gOriginalData

on openStack
   put the imageData of image "Bee and Tom" into gOriginalData
end openStack

on closeStack
   set the imageData of image "Bee and Tom" to gOriginalData
end closeStack
// All the above to preserve the original picture
2) For the same reason, changed this line in applyPosterize:

Code: Select all

// put the imageData of image "Bee and Tom" into picData
   put gOriginalData into picData        // To preserve the original
3) I changed "trunc" to "statRound" to get a 'better' integer:

Code: Select all

      //put trunc(sourceRed / numOfAreas) into redArea
      put statRound(sourceRed / numOfAreas) into redArea
      put min(255, max(trunc(numOfValues * redArea), 0)) into newRed

      //put trunc(sourceGreen / numOfAreas) into greenArea
      put statRound(sourceGreen / numOfAreas) into greenArea
      put min(255, max(trunc(numOfValues * greenArea), 0)) into newGreen

      //put trunc(sourceBlue / numOfAreas) into blueArea
      put statRound(sourceBlue / numOfAreas) into blueArea
My reasoning - truncate returns just the integer component of a float. That means anything from 1.0001 to 1.9999 will return 1. statRound returns the statistically correct integer, so 1.49 will return 1 and 1.51 will return 2.

I'm not sure if that helps :-)
Walt
Walt Brown
Omnis traductor traditor

Luisa Klose
Posts: 48
Joined: Tue Aug 30, 2011 2:24 pm

Re: Lingo, LiveCode, and Differing Results

Post by Luisa Klose » Wed Sep 14, 2011 5:21 pm

Thanks, Walt, for your help. Using statRound makes it worse. The numbers are supposed to be truncated.

Both LiveCode and Lingo are being asked to perform the same calculations. For some reason, LiveCode returns a result that can be verified to be incorrect. Unless Photoshop and Lingo are returning incorrect results, but I doubt that.

Regards,

Luisa

WaltBrown
Posts: 466
Joined: Mon May 11, 2009 9:12 pm
Location: NE USA

Re: Lingo, LiveCode, and Differing Results

Post by WaltBrown » Wed Sep 14, 2011 8:04 pm

Interesting. I wonder if it's a specific platform issue - when I changed your code, the area you circled as incorrect appeared to have been corrected when I ran it here.

I'm curious - how did you verify that LiveCode was incorrect? I'd be interested in the code, I'd like to explore that myself.

Best,
Walt
Walt Brown
Omnis traductor traditor

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3207
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Lingo, LiveCode, and Differing Results

Post by bn » Wed Sep 14, 2011 9:25 pm

Hi Luisa,

are you shure that the original image did not have a color profile included? That could make a difference. Could you make shure there is not color profile included? I think there is an option in Photoshop to export and image without color profile.

And
Show me where, in my code, I need to "manually assist" LiveCode. Without rewriting the handler, please. It will be easier for me to understand what I have to do.
if you have a division of two numbers and that division produces a fractional part, I don't know how, without some sort of assistance/ or algorithm the program is supposed to know what to do.

round, statround trunc are all just conventions. You have to decide what the program is to do.

Maybe someone knows the algorithm Lingo/Photoshop is using?

Kind regards

Bernd

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3207
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Lingo, LiveCode, and Differing Results

Post by bn » Wed Sep 14, 2011 11:30 pm

Hi Luisa,

if I try to set

Code: Select all

put  (255 / (posterizeAmount - 1)) into numOfValues
to

Code: Select all

put  (255 / (posterizeAmount - 1.28)) into numOfValues
it gets closer.

This is by experimentation only but may give you a lead. If it turns out to be closer to the Photoshop version in other levels of posterization it might help.
Don't ask my why...

Kind regards

Bernd

Luisa Klose
Posts: 48
Joined: Tue Aug 30, 2011 2:24 pm

Re: Lingo, LiveCode, and Differing Results

Post by Luisa Klose » Wed Sep 14, 2011 11:39 pm

Walt,

I put each result on a separate layer in Photoshop, and check them visually by turning the layers on and off. When it appears that there is no change between images, I sample several areas in each image to make sure.

The results produced by the statRound approach are way off. If you want to write a routine that checks the images pixel for pixel, that's up to you.


Bernd,

Whether or not the original image has a profile does not make a difference to Photoshop or to my handler running in Director. And it shouldn't in LiveCode, where I'm using the image's imageData:
The form of the imageData property, unlike the content, does not depend on what format the image is recorded in; it's always in the same form, which specifies the color of each pixel in the image, four bytes per pixel.
Once again: both LiveCode and Lingo are being asked to perform the same calculations. For some reason, LiveCode returns a result that can be verified to be incorrect.

I didn't see your new post while I was composing this. Let me check your suggestion. But still...

Regards,

Luisa Klose

Luisa Klose
Posts: 48
Joined: Tue Aug 30, 2011 2:24 pm

Re: Lingo, LiveCode, and Differing Results

Post by Luisa Klose » Wed Sep 14, 2011 11:57 pm

Bernd,

It's still off. Thanks.

Luisa

WaltBrown
Posts: 466
Joined: Mon May 11, 2009 9:12 pm
Location: NE USA

Re: Lingo, LiveCode, and Differing Results

Post by WaltBrown » Thu Sep 15, 2011 10:14 am

I wonder if the difference is that the "imageData" from LC is LC's interpreted data for the object display on screen, not the original image data in the file. If I create four images with the same source file and make them different screen sizes, the image objects all have different imageData sizes. LC doesn't have a mechanism for returning the actual image data in the file bit for bit, except for reading and parsing the raw file chunks as binary data. I would think to replicate PhotoShop you would have to perform any manipulations on the actual data in the file.

From the LC dictionary:
The imageData property is related to the content of the image--changing either one changes what's displayed in the image--but they're not identical: the imageData property and the image content are in different forms, have different sizes, and include overlapping but not identical information about the picture.

The imageData, unlike the contents of the image container, is based on the picture as it's presented on the screen, not stored in the image object.
Walt
Walt Brown
Omnis traductor traditor

Post Reply

Return to “Converting to LiveCode”