Page 1 of 2

Imported image is distorted

Posted: Sun Dec 06, 2015 11:40 pm
by quailcreek
Hi,
I use this code to pull an image from the iPhone. The code is in an image object of a card. The problem is that if the image chosen is not square then it appears distorted in the image object. Is there a way to have the chosen image scaled only along its longest axis? Or do I not want to use an image object in this situation?

Code: Select all

on mouseUp
   lock screen
   ## Image size is 240,240
   answer "Where is the photo coming from?" with "Camera" or "Library" or "Album" or "Cancel"
   put it into tMethod
   
   switch tMethod
      case "Library"
         mobilePickPhoto "library"
         if the result is "Cancel" then exit mouseUp
         set the text of me to the text of the last image of this card
         delete the last image of this card
         set the uPhotoChanged of this cd to true
         break
         
      case "Album"
         mobilePickPhoto "Album"
         if the result is "Cancel" then exit mouseUp
         set the text of me to the text of the last image of this card
         delete the last image of this card
         set the uPhotoChanged of this cd to true
         break
         
      case "Camera"
         mobilePickPhoto "Camera", 240,240
         if the result is "Cancel" then exit mouseUp
         set the text of me to the text of the last image of this card
         delete the last image of this card
         set the uPhotoChanged of this cd to true
         break
   end switch
end mouseUp

Re: Imported image is distorted

Posted: Mon Dec 07, 2015 1:27 pm
by jmburnod
Hi,
Is there a way to have the chosen image scaled only along its longest axis?
This function return the max width and height.
edit: for with and height sources :D

Code: Select all

function PourCrop pWidthSource,pHeightSource,pWidthDest,pHeightDest,pMarge 
   if pWidthSource <= pWidthDest-pMarge and pHeightSource <= pHeightDest-pMarge then
      put pWidthSource into LaWdef
      put pHeightSource into LaHdef
   else
      if pWidthSource = pHeightSource then -- that is a square
         put min(pWidthDest,pHeightDest) into tDimSide
         put tDimSide-pMarge into LaWdef
         put tDimSide-pMarge into LaHdef
         return LaWdef,LaHdef
         exit PourCrop
      end if
      subtract pMarge from pWidthDest
      subtract pMarge from pHeightDest
      put (pWidthDest/pWidthSource) into LeDivW
      put (pHeightDest/pHeightSource) into LeDivH
      put min(LeDivW,LeDivH) into LeDiv
      put trunc(pWidthSource*LeDiv) into LaWdef
      put trunc(pHeightSource*LeDiv) into LaHdef 
   end if
   return LaWdef,LaHdef
end PourCrop
Best regards
Jean-Marc

Re: Imported image is distorted

Posted: Tue Dec 08, 2015 5:12 am
by quailcreek
Thank you, Jean-Marc.
I'm a little confused. I understand that ,pWidthDest,pHeightDest,pMarge is the width, height and margin of the destination image but how do I get the pWidthSource,pHeightSource of the source image. Please excuse my ignorance.

Re: Imported image is distorted

Posted: Tue Dec 08, 2015 9:43 am
by jmburnod
Welcome Tom,
how do I get the pWidthSource,pHeightSource of the source image
Something like that :

Code: Select all

put the width of last image into pWidthS
put the height of last image into pHeightS
Best regards
Jean-Marc

Re: Imported image is distorted

Posted: Tue Dec 08, 2015 12:25 pm
by Klaus
Hi Tom,

to get the actual dimensions (in pixels) of an image file use:
...
the formattedheight of img X
the formattedwidth of img X
...
If an ímage has ist lockloc set to true, height/width
will only give you the current widht/height!
Know what I mean?


Best

Klaus

Re: Imported image is distorted

Posted: Tue Dec 08, 2015 5:31 pm
by quailcreek
Thanks again, Jean-Marc.
That makes perfect sense.

Yes, Klaus. I know what you mean, Thanks.

Re: Imported image is distorted

Posted: Wed Dec 09, 2015 12:11 am
by quailcreek
Ok, so I guess I don't understand what I thought I knew about LC.
All I did to your code, Jean-Marc was rename it to imageCrop.
It appears that LaWdef and LaHdef are being populated but when

Code: Select all

send imageCrop (tWidthSource, tHeightSource, tWidthDest, tHeightDest, tMarge)
is issued it throws an error. I'm sure I've done something wrong... and I'm also sure Klaus will enjoy pointing it out. :)

The answer shows LaWdef and LaHdef are being populated

Code: Select all

   answer "LaWdef =" && LaWdef & cr & "LaHdef=" && LaHdef
   return LaWdef,LaHdef
end imageCrop

Code: Select all

on mouseUp
   local tWidthSource, tHeightSource, tWidthDest, tHeightDest, tMarge 
   
   if environment() is not "mobile" then exit mouseUp
   lock screen

   answer "Where is the photo coming from?" with "Camera" or "Library" or "Album" or "Cancel"
   put it into tMethod

   switch tMethod
      case "Library"
         mobilePickPhoto "library"
         if the result is "Cancel" then exit mouseUp
         
         put the width of last image into tWidthSource
         put the height of last image into tHeightSource
         put the width of me into tWidthDest
         put the height of me into tHeightDest
         put 1 into tMarge
         
         send imageCrop (tWidthSource, tHeightSource, tWidthDest, tHeightDest, tMarge)
         
         set the width of last image of this card to item 1 of the result
         set the height of last image of this card to item 2 of the result
         
         set the text of me to the text of the last image of this card
         delete the last image of this card
         set the uPhotoChanged of this cd to true
         break
         
      case "Album"
         mobilePickPhoto "Album"
         if the result is "Cancel" then exit mouseUp
         set the text of me to the text of the last image of this card
         delete the last image of this card
         set the uPhotoChanged of this cd to true
         break
         
      case "Camera"
         mobilePickPhoto "Camera", 100,100
         if the result is "Cancel" then exit mouseUp
         set the text of me to the text of the last image of this card
         delete the last image of this card
         set the uPhotoChanged of this cd to true
         break
   end switch
end mouseUp

Re: Imported image is distorted

Posted: Wed Dec 09, 2015 12:27 am
by bn
Hi Tom,

you are calling a function, functions return their result in it, only if you call a command and the command returns something it is in the result

try

Code: Select all

get imageCrop (tWidthSource, tHeightSource, tWidthDest, tHeightDest, tMarge)
set the width of last image of this card to item 1 of it
 set the height of last image of this card to item 2 of it
or I prefer to put what the function returns into a variable

Code: Select all

put imageCrop (tWidthSource, tHeightSource, tWidthDest, tHeightDest, tMarge) into tMyWidthAndHeight
set the width of last image of this card to item 1 of tMyWidthAndHeight
 set the height of last image of this card to item 2 of tMyWidthAndHeight
if the rest is ok this should work, of course you have to adapt every call to imageCrop this way.

Kind regards
Bernd

Re: Imported image is distorted

Posted: Wed Dec 09, 2015 12:41 am
by quailcreek
Thanks Bernd. I was just going to post that I figured it out. Yep I was look at "put" as well.

Thanks everybody!

Code: Select all

  put imageCrop (tWidthSource, tHeightSource, tWidthDest, tHeightDest, tMarge) into theDimns
         
         set the width of last image of this card to item 1 theDimns
         set the height of last image of this card to item 2 theDimns

Re: Imported image is distorted

Posted: Thu Dec 10, 2015 4:15 am
by quailcreek
Well I thought I had this handled but not so. Library pics are still showing up distorted in the image object. Is this because the image object always stretches the pics to the size of itself? If so is there a way around this or should I use something other than an image object to display pics? Thanks again for the help.

Re: Imported image is distorted

Posted: Thu Dec 10, 2015 10:50 am
by bn
Hi Tom,

did you use the formattedWidth and formattedHeight when determining the dimensions of the image you want to change?

When you set the text of an image object to the text of an image and the image object is locked then the image will be put into the image object and use the dimension and aspect ratio of the image object. BUT the formattedWidth and formattedHeight of the image in the image object are still those of the original image. That is what Klaus was referring to.

Could you explain in more detail what does not work and what your current code is?

Kind regards
Bernd

Re: Imported image is distorted

Posted: Thu Dec 10, 2015 6:56 pm
by quailcreek
Hi Bernd,
Thank you for helping me out. The mouseUp code is in the image object and the imageCrop code is in the stack. The image object is locked and its size is 240x240. Things appear to work in the sim but it's hard to tell what the original pics look like so I'm not sure. On the device, the pics in the image look like they're width is stretched so they look wider or fatter than they are. If the pic is square then I comes in correctly.

Here is a capture from my device:
IMG_3605.jpg

Code: Select all

on mouseUp
   if environment() is not "mobile" then exit mouseUp
   lock screen
   ## Image size is 240,240
   answer "Where is the photo coming from?" with "Camera" or "Library" or "Album" or "Cancel"
   put it into tMethod
   
   switch tMethod
      case "Library"
         mobilePickPhoto "library"
         if the result is "Cancel" then exit mouseUp
         
         put the formattedwidth of last image of this card into tWidthSource
         put the formattedheight of last image of this card into tHeightSource
         --         answer "tWidthSource =" && tWidthSource & cr & "tHeightSource =" && tHeightSource
         put the width of me into tWidthDest
         put the height of me into tHeightDest
         put 0 into tMarge
         
         put imageCrop (tWidthSource, tHeightSource, tWidthDest, tHeightDest, tMarge) into theDimns
         --         answer theDimns
         set the width of last image of this card to item 1 theDimns
         set the height of last image of this card to item 2 theDimns
         --         answer "W" && the width of last image of this card & cr & "H" && the height of last image of this card 
         set the text of me to the text of the last image of this card
         delete the last image of this card
         set the uPhotoChanged of this cd to true
         break
         
      case "Album"
         mobilePickPhoto "Album"
         if the result is "Cancel" then exit mouseUp
         
         put the formattedwidth of last image of this card into tWidthSource
         put the formattedheight of last image of this card into tHeightSource
         put the height of me into tWidthDest
         put the width of me into tHeightDest
         put 0 into tMarge
         
         put imageCrop (tWidthSource, tHeightSource, tWidthDest, tHeightDest, tMarge) into theDimns
         set the width of last image of this card to item 1 theDimns
         set the height of last image of this card to item 2 theDimns
         
         set the text of me to the text of the last image of this card
         delete the last image of this card
         set the uPhotoChanged of this cd to true
         break
         
      case "Camera"
         mobilePickPhoto "Camera", 240,240
         if the result is "Cancel" then exit mouseUp
         set the text of me to the text of the last image of this card
         delete the last image of this card
         set the uPhotoChanged of this cd to true
         break
   end switch
   unlock screen
end mouseUp

Code: Select all

function imageCrop pWidthSource, pHeightSource, pWidthDest, pHeightDest, pMarge
   local theWdef,theHdef
   --   answer "pWidthSource =" && pWidthSource & cr & "pHeightSource =" && pHeightSource & cr & "pWidthDest =" && pWidthDest & cr & "pHeightDest =" && pHeightDest
   
   if pWidthSource <= (pWidthDest-pMarge) and pHeightSource <= (pHeightDest-pMarge) then
      put pWidthSource into theWdef
      put pHeightSource into theHdef
      --      answer " Both Smaller"
   else
      if pWidthSource = pHeightSource then -- that is a square and bigger than the image rect
         put min(pWidthDest,pHeightDest) into tSmallSide
         put (tSmallSide-pMarge) into theWdef
         put (tSmallSide-pMarge) into theHdef
         return theWdef,theHdef
         --         answer "Square and bigger"
         exit imageCrop
      end if
      subtract pMarge from pWidthDest
      subtract pMarge from pHeightDest
      put (pWidthDest/pWidthSource) into theDivW
      put (pHeightDest/pHeightSource) into theDivH
      put min(theDivW,theDivH) into theDiv
      put trunc(pWidthSource*theDiv) into theWdef
      put trunc(pHeightSource*theDiv) into theHdef
      
      --      answer "Larger"
   end if
   return theWdef,theHdef
end imageCrop

Re: Imported image is distorted

Posted: Thu Dec 10, 2015 10:16 pm
by bn
Hi Tom,

could you try to add

Code: Select all

set the imageData of me to the imageData of me
although this sounds a bit silly

after each

Code: Select all

set the text of me to the text of the last image of this card
this forces the image to take on the dimensions you see in you image object and not just restrain the image to the dimensions of the image object.

This is a lossy operation, this means after you issue "set the imageData of me to the imageData of me" that will be the new "formattedWidth"/"formattedHeight" of the image.

Kind regards
Bernd

Re: Imported image is distorted

Posted: Thu Dec 10, 2015 11:01 pm
by quailcreek
Hi Bernd,
Nope, same results. I build and test in iSO 9.1 but my device is running iSO 7.1.1. That shouldn't make any difference.

Regards,
Tom

Re: Imported image is distorted

Posted: Thu Dec 10, 2015 11:07 pm
by bn
Hi Tom,

unfortunately I am running out of ideas and maybe someone comes up with a solution.

On top I am on my way out for the weekend. If there is no solution to your problem then I will try next week on an iOS device and see how far I get.

Kind regards
Bernd