Q: Can I use only a part of an Image File and do stuff with it ?
BNig
niggemann at uni-wh.de
Tue Oct 6 10:33:51 EDT 2009
Just for the record and to correct a piece of code I posted:
As it turns out Rolf wanted to preserve the transparency of a PNG when
slicing it up. Aditionally there was a bug in my previously posted code.
So here is a version that preserves transparency for PNG (no harm if it is a
JPG that has no transparency) and works for any number of slices, the width
of the original image does not have to be an even multiple of a slice.
Leftover pixels are put into an additional slice.
watch out for linewraps
----------------------------------------
on mouseUp
put 12 into tNoOfImages -- change the number of 'slices' here
put the name of image "myLongImage" into myImage -- Name of image here
put the imagedata of myImage into tImageData
put the rect of myImage into tTemplateRect
put the alphadata of myImage into iAlphaD
put the length of tImageData into tLengthImageData
put the length of iAlphaD into tAlphaLength
put the height of myImage into tHeight
put the width of myImage into tWidth
put tWidth* 4 into tByteWidth
put tWidth div tNoOfImages * 4 into tChunkSizeinBytes
put tWidth div tNoOfImages into tNormalImageWidth
put tWidth * tHeight into tAlphaLength
put tNormalImageWidth into tChunkSizeAlpha
put item 1 of tTemplateRect + tNormalImageWidth into item 3 of
tTemplateRect
put item 2 of tTemplateRect - tHeight - 20 into item 2 of tTemplateRect
put item 4 of tTemplateRect - tHeight - 20 into item 4 of tTemplateRect
set the rect of the templateImage to tTemplateRect
put 1 into tStartChar
put 1 into tAlphaStartChar
put "" into tCollectImageData
put "" into tAlphaCollect
-- check if width of original is an even multiple of the 'slices', if not
-- add an image for the rest of the original image
put tWidth mod tNoOfImages into tRest
if tRest <> 0 then
put true into tHasARest
add 1 to tNoOfImages
end if
lock screen
repeat with i = 1 to tNoOfImages
-- extract the imageData
repeat with j = tStartChar to tLengthImageData step tByteWidth
put char j to j + tChunkSizeInBytes-1 of tImageData after
tCollectImageData
end repeat
-- extract the alphaData
repeat with k = tAlphaStartChar to tAlphaLength step tWidth
put char k to k + tChunkSizeAlpha-1 of iAlphaD after tAlphaCollect
end repeat
create image
set the name of last image to "i" & i -- change name of new images
(the slices) here
set the imageData of last image to tCollectImageData
set the alphaData of last image to tAlphaCollect
add tChunkSizeInBytes to tStartChar
add tChunkSizeAlpha to tAlphaStartChar
-- check for additional image in case width is not an even multiple of
'slice'
if not (tHasARest and i = (tNoOfImages - 1)) then
-- no additional image
add tNormalImageWidth to item 1 of tTemplateRect
add tNormalImageWidth to item 3 of tTemplateRect
set the rect of the templateImage to tTemplateRect
else
-- additional image (will be last image) of a different width
put tRest * 4 into tChunkSizeInBytes
put tRest into tChunkSizeAlpha
add tNormalImageWidth to item 1 of tTemplateRect
put item 1 of tTemplateRect + tRest into item 3 of tTemplateRect
set the rect of the templateImage to tTemplateRect
end if
put empty into tCollectImageData
put empty into tAlphaCollect
end repeat
end mouseUp
----------------------------------------
regards
Bernd
--
View this message in context: http://www.nabble.com/Q%3A-Can-I-use-only-a-part-of-an-Image-File-and-do-stuff-with-it---tp25682504p25769925.html
Sent from the Revolution - User mailing list archive at Nabble.com.
More information about the use-livecode
mailing list