ByteArray - getPixels() & setPixels()

From Director Online Wiki
Jump to: navigation, search

Description

getPixels(imageFormat)
setPixels(bytearray {, imageFormat} )

imageFormat can be one of these values:

#RGBA8888, #ARGB8888, #RGB888, #BGR888

Byte array method; creates (or reads from) a byte array that is initialized with 24bit or 32bit image data.

Example

bArray = image.getPixels(#RGBA8888)
image.setPixels(bArray,#RGBA8888)

More Info

If you want to make your code as speedy as you can then do a test against the image member's .useAlpha() property. If a 32-bit image with an actual alpha channel is imported then the .useAlpha() = 1 = #RGBA8888 = 4 bytes per pixel and if not (ie. it's a 24-bit image, which Director doesn't distinguish with the .depth property) then .useAlpha() = 0 = #RGB888 = 3 bytes per pixel.

Speed Comparison With getPixel()

on multiplyBlendMode (image1, image2)
 theStart = the milliseconds
 newWidth = max(image1.width, image2.width)
 newHeight = max(image1.height, image2.height)
 newImage = image(newWidth, newHeight, 32)
 baNew = newImage.getPixels(#RGBA8888)
 baImg1 = image1.getPixels(#RGBA8888)
 baImg2 = image2.getPixels(#RGBA8888)
 len = baNew.length
 repeat with i = 1 to len
   baNew[i] = baImg1[i] * baImg2[i] / 255
 end repeat
 newImage.setPixels(baNew, #RGBA8888)
 put the milliseconds - theStart
 return newImage
end

which is faster than the old getPixel() method:

on multiplyBlendMode (image1, image2)
 theWidth = image1.width - 1
 theHeight = image1.height - 1
 theImage = image(theWidth + 1, theHeight + 1, 32)
 repeat with x = 0 to theWidth
   repeat with y = 0 to theHeight
     theColour1 = image1.getPixel(point(x,y))
     theColour2 = image2.getPixel(point(x,y))
     theRed = theColour1.red * theColour2.red / 255.0
     theGreen = theColour1.green * theColour2.green / 255.0
     theBlue = theColour1.blue * theColour2.blue / 255.0
     theNewColour = color(theRed, theGreen, theBlue)
     theImage.setPixel(point(x, y), theNewColour)
   end repeat
 end repeat
 return theImage
end

.... using two 1024x768 images (no alpha channel) here are the averaged results (of 3 tests each):

  • 1768 ms - byteArray getPixels() - 24bit, ~48% faster
  • 2351 ms - byteArray getPixels() - 32bit, ~30% faster (without testing for the alpha channel)
  • 3378 ms - getPixel()