TGC Codebase Backup



Get RGBA values from image by tboy

25th Sep 2016 20:27
Summary

Extract RGBA values from an image



Description

Updated version, better code, much cleaner.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    SetErrorMode(2)
SetWindowTitle( "imagedata" )
SetWindowSize( 640, 480, 0 )
SetWindowAllowResize( 1 )
SetVirtualResolution( 640, 480 )
SetOrientationAllowed( 1, 1, 1, 1 )
SetSyncRate( 30, 0 )
SetScissor( 0,0,0,0 )
UseNewDefaultFonts( 1 )

Type imgPicData
  red as integer
  green as integer
  blue as integer
  alpha as integer
EndType

imgData as imgPicData[0]

imgPic = LoadImage("pic.png")
imgSprite = CreateSprite(imgPic)
imgMemID = CreateMemblockFromImage(imgPic)

imgPicSize = (GetImageWidth(imgPic) * GetImageHeight(imgPic) * 4)
imgLen = 0
startByte = 12
imgData.Length = imgPicSize

For i = startByte To imgPicSize Step 4
  imgData[imgLen].red = GetMemblockByte(imgMemID, i)
  imgData[imgLen].green = GetMemblockByte(imgMemID, i+1)
  imgData[imgLen].blue = GetMemblockByte(imgMemID, i+2)
  imgData[imgLen].alpha = GetMemblockByte(imgMemID, i+3)
  imgLen = imgLen + 1
Next i

imgWidth = GetMemblockInt(imgMemID, 0)
imgHeight = GetMemblockInt(imgMemID, 4)
imgBitDepth = GetMemblockInt(imgMemID, 8)

DeleteMemblock(imgMemID)

DrawSprite(imgSprite)

do
  print ("Image Width: " + str(imgWidth))
  print ("Image Height: " + str(imgHeight))
  print ("Image Bit Depth: " + str(imgBitDepth))

  pointerX = Trunc(GetPointerX())
  pointerY = Trunc(GetPointerY())

  pixelValue =  ((imgWidth*pointerY) + pointerX)

  print ("RGBA:" + "(" + str(imgData[pixelValue].red) + "," + str(imgData[pixelValue].green) + "," str(imgData[pixelValue].blue) + "," + str(imgData[pixelValue].alpha) + ")")

 sync()
loop