Page 1 of 1

Copy arbitrary rect from one image to another

Posted: Sun Jan 30, 2011 8:38 pm
by couchpotato
I have spent more time that I'll admit trying to accomplish what appears to be a simple task.
Via script I need to copy a rectangle of one image object and paste it into the same-sized rectangle of another image object.
Ideally this needs to be done with neither of the images visible.
I'm developing custom transitions so I am floating one image obj over another (larger) image obj, doing the transition and then needing to
copy the resulting obj, selecting a rect in the underlying obj and pasting in the results.

In a perfect world, one would think that this might be accomplished with one statement like:

copy image "hover" to rect "100,100,200,300" of image "canvas" -- (bad syntax)

MORE PERFECT API

copy rect "50,50,150,250" of image "hover" to rect "100,100,200,300" of image "canvas" -- (bad syntax)

OR MAYBE

copy image "hover"
select image "canvas"
choose select tool
drag from "100,100" to "200,300"
paste
-- this "paste" creates a duplicate of image "hover", not at all what was intended
-- since choosing the select tool negates the image selection

OR MAYBE

export image "hover" to rect "100,100,200,300" of image "canvas"


Problematic is the fact that when an image is selected (select image "canvas") that the size/location handles appear (not cool)
and unless and image is visible it doesn't seem to always participate in operations like paste. Similarly, using "import snapshot"
(which allows rect to define the region) creates a new floating image which is visible - cosmetically unacceptable.

I've scoured the user guide/dictionary for a rigorous description of "the right way" of doing this.

export looked like a possibility, but you cannot export to rect of an object...

Rather than exploiting some undocumented quirk I'd prefer knowing what's "sanctioned" since anything less might
change in subsequent releases of the product. It's been triply frustrating that some of the alternative techniques
I've tried might work in Runrev 4.5.0-dp-3 but fail when built as standalone for Mac and hang when built for web.

So,

Is there a platform-independent approved syntax for copying the contents of a rectangle of one image object and
replacing an identical-dimensioned rectangle (at different location) within a second image object - invisibly,
"behind-the-scenes" with possibly neither of these image objects visible at the time?

Re: Copy arbitrary rect from one image to another

Posted: Sun Jan 30, 2011 10:39 pm
by BvG
No there isn't.

There are workarounds tho:

For selectionHandles to not be visible, deselect the Image, and reselect later on.
For invisibility, set the location to negative values. a card always starts at 0,0 so stuff that is topleft of that, won't be seen.

For copying an image into another there's a quick way, and a slow way. Slow first: Parse the imageData yourself, and modify the imageData of the target image.

Quick:
Lock screen, then make a screenshot of the rectangle that is part of the first image. then position the resulting temporary image over the target image. Now make a screenshot of the target image, with the temporary image showing on top of it. Now delete the temporary image, and the target image. rename the latest image to the same name as the target image had.

Often, when there's no built in way to do stuff, there's weird workarounds that do work, but are certainly less elegant. It is both a blessing and a curse ;)

Re: Copy arbitrary rect from one image to another

Posted: Sun Jan 30, 2011 10:53 pm
by bn
Hi couchpotato,
Welcome to the forum.


You could try this

Code: Select all

on mouseUp
   set the text of image "iIntermediate" to the text of image "iSource"
   set the topleft of image "iIntermediate" to 0,0
   crop image "iIntermediate" to 100,100,200,200
   set the text of image "iTarget" to the text of image "iIntermediate"
end mouseUp
3 images, you copy the source image to an intermediate image which you then crop to your liking and set the destination image to the cropped image.

Tested and works even if all 3 images are hidden. I see no reason why this should not work cross-platform. And it is pretty fast too.

Kind regards

Bernd

Re: Copy arbitrary rect from one image to another

Posted: Tue Feb 01, 2011 5:32 pm
by couchpotato
Thanks for the suggestions.

The intuitive solution actually works in LiveCode (RunRev), it just fails once standalone or web app is built.
Solution is

export snapshot from rect snapRect of this card to image "Canvas" as JPEG

with image "Canvas" visible when executed this effectively updates it to include
anything and everything that is visible (suspended) over it - just what I wanted.

However building standalone Mac application the result of this export is to have
image "Canvas" contain grey - I will consult with the QA database once my developer
registration has been processed (sound of thumbs twiddling).