// Addon to Bitmap pixel access blocks extension // Mark Guzdial, November 2015 addExtension (authoringSpecs) 'Pixels' (array //(array 'r' 'self_pixelCount' 'pixels count : from _' 'menu.imageMenu' nil) (array 'r' 'getX' 'get X-position from _' 'pixel' nil) (array 'r' 'getY' 'get Y-position from _' 'pixel' nil) (array ' ' 'self_saveCostume' 'save costume as image named _' 'string' 'image1') (array 'r' 'self_getPixelNum' 'pixel color at _ : from _' 'num menu.imageMenu' 1 nil) (array ' ' 'self_setPixelNum' 'set color at _ to _ : from _' 'num color bitmap' 1 0 nil) (array 'r' 'self_getPixelXY' 'pixel at x _ y _ : from _ ' 'num num menu.imageMenu' 1 1 nil) (array 'r' 'getColor' 'color from pixel _' 'pixel' nil) (array 'r' 'sumColors' 'add color _ and _' 'color color' nil nil) (array 'r' 'aveColors' 'blend color _ and _' 'color color' nil nil) (array 'r' 'self_getPixels' 'pixels : from _' 'menu.imageMenu') ) method getRed Color { return r } method getGreen Color { return g } method getBlue Color { return b } method getAlpha Color { return a } to self_saveCostume imageName { m = (morph (implicitReceiver)) bm = (costumeData m) proj = (projectForMorph m) setName bm imageName add (images proj) bm } method getX Pixel { //m = (morph (implicitReceiver)) bm = (costumeData targetMorph) indexless = (index - 1) if (index < (width bm)) {return 1} i = (toInteger (indexless % (width bm))) return (i + 1) } method getY Pixel { //m = (morph (implicitReceiver)) bm = (costumeData targetMorph) indexless = (index - 1) i = (toInteger (index / (width bm))) if ((i + 1) > (height bm)) {return (height bm)} return (i + 1) } to self_getPixelXY x y bm { // Return the pixel at x,y in the given bitmap or the current costume. m = (morph (implicitReceiver)) if (isNil bm) { bm = (costumeData m) } pixels = (pixelData bm) i = (toInteger ((((y - 1) * (width bm)) + (x - 1)) + 1)) if (or (i < 1) (i > (count pixels))) { error 'bad pixel number' } return (new 'Pixel' pixels i m) } to self_getPixelNum i bm { // Return the color of the numbered pixel in the given bitmap or the current costume. if (isNil bm) { bm = (costumeData (morph (implicitReceiver))) } pixels = (pixelData bm) if (or (i < 1) (i > (count pixels))) { error 'bad pixel number' } alpha = (getPixelAlpha pixels i) rgb = (getPixelRGB pixels i) return (color ((rgb >> 16) & 255) ((rgb >> 8) & 255) (rgb & 255) alpha) } to self_setPixelNum i color bm { // Set the color of the numbered pixel in the given bitmap or the current costume. m = (morph (implicitReceiver)) if (isNil bm) { bm = (costumeData m) } pixels = (pixelData bm) if (or (i < 1) (i > (count pixels))) { error 'bad pixel number' } setPixelRGBA pixels i (red color) (green color) (blue color) (alpha color) costumeChanged m } method getColor Pixel { alpha = (getPixelAlpha pixels index) rgb = (getPixelRGB pixels index) red = ((rgb >> 16) & 255) green = ((rgb >> 8) & 255) blue = (rgb & 255) return (color red green blue alpha) } to sumColors color1 color2 { r1 = (getRed color1) r2 = (getRed color2) g1 = (getGreen color1) g2 = (getGreen color2) b1 = (getBlue color1) b2 = (getBlue color2) a1 = (getAlpha color1) a2 = (getAlpha color2) return (color (r1 + r2) (g1 + g2) (b1 + b2) (a1 + a2)) } to aveColors color1 color2 { r1 = (getRed color1) r2 = (getRed color2) g1 = (getGreen color1) g2 = (getGreen color2) b1 = (getBlue color1) b2 = (getBlue color2) a1 = (getAlpha color1) a2 = (getAlpha color2) return (color (toInteger ((r1 + r2) / 2)) (toInteger ((g1 + g2) / 2)) (toInteger ((b1 + b2) / 2)) (toInteger ((a1 + a2) / 2))) }