AppGameKit Classic

AppGameKit Classic

评价数不足
Using MemBlocks (with an example that turns a color photo into grayscale)
由 Jammy 制作
This is a guide to using MemBlocks (with an example that turns a color photo into grayscale).
   
奖励
收藏
已收藏
取消收藏
Introduction
In this guide I will show the power of memblocks and how you can manipulate images with them. You can change Images any way imaginable. However as I was asked for an example of how to change a colour image into greyscale here]. I thought I would create a Guide.

If you like this guide, give it the thumbs up, to let me know if future guides would be useful. Any comments would be appreciated.

With this guide you will be able to choose an image from your computer.
for example this



and change it to this.

Screen, Variables and Buttons
First, as always we need to set the screen, define a couple of variables for later use and create a control method.

Screen settings are just left at the default.

The variables are Global - which means they can be used in any part of the program, even inside functions and are Integers (Whole numbers). These variables will be used to Identify images.

The control method is 3 buttons which
1) Exit the program.
2) Make an image greyscale.
3) Choose an image

// Project: ImageManipulation // Created: 2015-08-30 // set window properties SetWindowTitle( "Image Manipulation" ) SetWindowSize( 1024, 768, 0 ) // set display properties SetVirtualResolution( 1024, 768 ) SetOrientationAllowed( 1, 1, 1, 1 ) ` set a couple of global integers global image as integer global averageimage as integer `create a few buttons bntend=1 AddVirtualButton( bntend,40,40,60 ) SetVirtualButtonText( bntend, "Exit" ) bntGray=2 AddVirtualButton( bntGray,110,40,60 ) SetVirtualButtonText( bntGray, "Gray" ) bntChoose=3 AddVirtualButton( bntChoose,180,40,60 ) SetVirtualButtonText( bntChoose, "Choose" )
The Main Loop
The main loop consists of some instructions for the user and what to do when a button is pressed.

Instructions

This starts the main loop and displays some user instructions.

if a sprite has not yet been created then the user should choose an image. however if there is a sprite then the user should make a greyscale image of it.

do `some instructions if GetSpriteExists(sprite)=0 Print("") print("") print ("Click choose to choose an image.") else Print("") print("") print ("Click 'gray' to make it grayscale ") print ("or 'choose' for another image.") endif

Exit Button

If the button is pressed exit the main loop where the program will end. This is quite important if you have a full screen app.

` the exit button if GetVirtualButtonPressed( bntend ) then exit

Choose Button

When this button is pressed the program checks if the user has previously chosen an image by looking for the existence of a sprite. If found it will delete the sprite ready for a fresh start.

It will then call the 'Select Image' Function which i will explain later.
If the Image value is 0 then the user cancelled the procedure and we have no image to create a sprite. Otherwise a sprite is created with the chosen image and displayed to cover the screen.

` the choose button if GetVirtualButtonPressed( bntChoose ) if GetSpriteExists(sprite)=1 then DeleteSprite(sprite) image=SelectImage() if image=0 //was canceled else //make a Background sprite sprite = CreateSprite( image ) SetSpritesize( sprite, -1,1024 ) setspritepositionbyoffset(sprite,512,384) endif endif

Grey Button

This button calls the 'Average' function, which i will explain later. Greyscales can be created in different ways and i have chosen average for this example.

The screen then updates and starts the main loop again, If the loop is exited the program ends.

`the gray button if GetVirtualButtonPressed( bntGray ) averageimage=Average() SetSpriteImage(sprite,averageimage) endif Sync() loop end
Functions
There are two functions used in this program,

1) SelectImage - Allows you to choose a photo from your computer and is pretty much taken directly from the example in the help text [www.appgamekit.com], I'm sure the author 'Waitmessage' wont mind it getting used.

It opens a 'choose image' dialog box. Waits for the user to choose an Image and assigns the chosen image to the variable 'image'

function SelectImage() if ShowChooseImageScreen()=1 //Ok while IsChoosingImage()=1 //Dialog is Open, Wait sync() endwhile image = GetChosenImage() //Get the image endif endfunction image

2) Average - This changes the image from colour to greyscale.

First it displays a message - saying please wait because if the user is on a slow device or the photo is very large the conversion may take some time.

Secondly It creates a memblock from the image. Memblocks are a breakdown of all the information in the image (or sound, or file) stored in a block of memory. This memory can be accessed directly and changed and as it is memory it is accessed in bytes.

With an image the first four bytes contain the image width, the second four contain the image height. As these consist of four bytes we use the 'GetMemblockInt' command, which reads the four bytes together. These values are not used in the program but are very handy to know.

We then need to know the memblock size as different images will produce different memblock sizes.

As the image pixel data starts at memory address 12, a loop is created to go thorough the whole image. In steps of 4, as each four byte section contains the Red, Green, Blue and Alpha data for every pixel. You can change each value individually.

In this example we add the red, green and blue values together and divide by three to get a mean value. We then assign this mean value to each red, green and blue component of the pixel. This makes a greyscale image based on the mean values. You could make a greyscale based on the red or green or blue rather than the mean for different effects.

We then make an Image from the memblock and assign it to 'averageimage' and return this to the main program.

function Average() `just a wee message as if the image is very big it could take a while. Print("") print("") print ("processing please wait.....") sync() ` make a memory block from the image memblock=CreateMemblockFromImage(image) `get some atributes width = GetMemblockInt(memblock, 0) height = GetMemblockInt(memblock, 4) size = GetMemblockSize(memblock) for c = 12 to size - 1 step 4 // each color byte is retrieved r = GetMemblockByte(memblock, c) g = GetMemblockByte(memblock, c + 1) b = GetMemblockByte(memblock, c + 2) // the alpha (position c + 3) is ignored because we don't want to change that `calculate the average mean=(r+g+b)/3 `change each RGB pixel to the average SetMemblockByte(memblock, c, mean) SetMemblockByte(memblock, c + 1, mean) SetMemblockByte(memblock, c + 2, mean) next c `save the result as an image averageimage=CreateImageFromMemblock(memblock) endfunction averageimage
The Full Program - just copy and paste this into AGK2 and press F5.
// Project: ImageManipulation // Created: 2015-08-30 // set window properties SetWindowTitle( "Image Manipulation" ) SetWindowSize( 1024, 768, 0 ) // set display properties SetVirtualResolution( 1024, 768 ) SetOrientationAllowed( 1, 1, 1, 1 ) ` set a couple of global integers global image as integer global averageimage as integer `create a few buttons bntend=1 AddVirtualButton( bntend,40,40,60 ) SetVirtualButtonText( bntend, "Exit" ) bntGray=2 AddVirtualButton( bntGray,110,40,60 ) SetVirtualButtonText( bntGray, "Gray" ) bntChoose=3 AddVirtualButton( bntChoose,180,40,60 ) SetVirtualButtonText( bntChoose, "Choose" ) `MainLoop do `some instructions if GetSpriteExists(sprite)=0 Print("") print("") print ("Click choose to choose an image.") else Print("") print("") print ("Click 'gray' to make it grayscale ") print ("or 'choose' for another image.") endif ` the exit button if GetVirtualButtonPressed( bntend ) then exit ` the choose button if GetVirtualButtonPressed( bntChoose ) if GetSpriteExists(sprite)=1 then DeleteSprite(sprite) image=SelectImage() if image=0 //was canceled else //make a Background sprite sprite = CreateSprite( image ) SetSpritesize( sprite, -1,1024 ) setspritepositionbyoffset(sprite,512,384) endif endif `the gray button if GetVirtualButtonPressed( bntGray ) averageimage=Average() SetSpriteImage(sprite,averageimage) endif Sync() loop end function Average() `just a wee message as if the image is very big it could take a while. Print("") print("") print ("processing please wait.....") sync() ` make a memory block from the image memblock=CreateMemblockFromImage(image) `get some atributes width = GetMemblockInt(memblock, 0) height = GetMemblockInt(memblock, 4) size = GetMemblockSize(memblock) for c = 12 to size - 1 step 4 // each color byte is retrieved r = GetMemblockByte(memblock, c) g = GetMemblockByte(memblock, c + 1) b = GetMemblockByte(memblock, c + 2) // the alpha (position c + 3) is ignored because we don't want to change that `calculate the average mean=(r+g+b)/3 `change each RGB pixel to the average SetMemblockByte(memblock, c, mean) SetMemblockByte(memblock, c + 1, mean) SetMemblockByte(memblock, c + 2, mean) next c `save the result as an image averageimage=CreateImageFromMemblock(memblock) endfunction averageimage function SelectImage() if ShowChooseImageScreen()=1 //Ok while IsChoosingImage()=1 //Dialog is Open, Wait sync() endwhile image = GetChosenImage() //Get the image endif endfunction image
2 条留言
Jammy  [作者] 2019 年 6 月 17 日 上午 8:00 
HI Mariosal71

After a quick "Google Translate" I get


"Hi, is there a future implementation of a function like GetmemblockDOUBLE that allows you to store and manipulate 64 bits simultaneously? This would allow you to use typical bitboard techniques to generate very efficient AI on games based on standard boards."

I am a user, not a developer of AGK (though I did make this tutorial), so I cant answer this question for sure.

MemBlocks are powerful and if I remember properly, were added to AGK due to user demand. I have found that the developers listen to the community and try very hard to implement useful and powerful features which is great, but I have not heard about a GetmemblockDOUBLE being on the road map.

Perhaps if you ask on the main forum you will get a more definitive answer.
mariosal71 2019 年 6 月 16 日 上午 8:49 
Salve, è previsto in futuro l'implementazione di una funzione tipo GetmemblockDOUBLE che permetta di memorizzare e manipolare 64 bit contemporaneamente? Ciò permetterebbe di utilizzare tecniche tipiche del bitboard per generare AI molto efficienti su giochi basati su scacchiere standard.