Upload, Manipulate, and Display bmp back to user

New to FreeBASIC? Post your questions here.
Post Reply
CloneFB
Posts: 1
Joined: Jul 19, 2017 18:54

Upload, Manipulate, and Display bmp back to user

Post by CloneFB »

Hi, I've been trying to teach myself FreeBasic for about two weeks now. I'm used to using MatLab in which everything was more or less given to you (imread,imshow for ex.).

I'm trying to have the user upload an image and allow the user to change the color of a part of the image and then display that image back to the user.

From what I've gathered, it's the easiest to use bmp files. My test images consist of 24-bit bmp files generated from MS paint of a single color.
I do not understand why the values correspond to a color: -1 for white, 0 for red, -96 for blue... etc.

For now, I'm using arbitrary screen dimensions and small enough bmp files so that they do not extend past the user's screen.

This is what I have so far (with notes to help myself out):

Code: Select all

#include "fbgfx.bi"
WindowTitle "ImageEditor"
Declare Sub mainMenu 
Declare Sub changeColor


'To set the size of the screen
Dim As Integer DeskW, DeskH
ScreenControl fb.GET_DESKTOP_SIZE, DeskW, DeskH
ScreenRes DeskW+5,DeskH,32 
print "Desktop Res: " + Str(DeskW) + "x" + Str(DeskH)

Dim Shared As Integer ScreenW,ScreenH
ScreenInfo ScreenW,ScreenH
Print "Screen Res: " + Str(ScreenW) + "x" + Str(ScreenH)

'Code to have user choose a file
Dim Shared As String userImage
Input "Enter the file name of a bmp: ", userImage

'Get the image dimensions
Dim Shared As UInteger imageW,imageH

'Could use ImageInfo here...
Open userImage For Binary As #1
	'Note: Do not need quotes around "userImage" because the variable
			'IS already a string
Get #1, 19, imageW
Get #1, 23, imageH
Close #1


'Let user know about the file
Print "Selected File: " + Str(userImage)
Print "Image Dimensions: " + Str(imageW) + "x" + Str(imageH)
Print "Press a key to load the image"
Sleep
Cls
'================================================================================================================

'Create a buffer to display image
'Image buffer info: http://bourabai.kz/einf/freebasic/TutFBgfxImgAndFontBuf.html
Dim imageBuffer As Any Ptr 
imageBuffer = ImageCreate(imageW,imageH)

'Load the image
BLoad userImage, imageBuffer
Put (ScreenW/4,ScreenH/4), imageBuffer
'ImageDestroy(imageBuffer)
Print "Press a key to continue"
Sleep
Cls

'Gather the pixels
Dim Shared imageArray(0 To imageW-1,0 To imageH-1) As Byte
For Row As Integer = 0 to imageW-1
    For Column As Integer  = 0 to imageH-1
    	imageArray(Row,Column) = Point(Row,Column,imageBuffer)
    	Print imageArray(Row,Column);
    Next Column 
Print ""
Next Row

Sleep
Cls

mainMenu

'=======================================================================================================
Sub mainMenu
Cls 
Dim mode As Integer

Print "1. Color" 
Print "2. Exit"
Input "Enter the number corresponding to the desired operation: ", mode

	If mode = 1 Then
		changeColor
	Elseif mode = 2 Then
		end
	Else 
		mainMenu
	EndIf
	
End Sub

/'Change color by the bit
----------------------------------------------------------------------------------------------------------------------'/
	Sub changeColor
		Cls 
		Dim As Integer Lrow, Urow, Lcolumn, Ucolumn
		Dim colorValue As Byte
		
		'Display the range that the user is able to change the colors
		Print "The bounds for the rows in your image are " + Str(LBound(imageArray,1)) + " to " +Str(UBound(imageArray,1)+1)
		Print "The bounds for the columns in your image are " + Str(LBound(imageArray,2)) + " to " + Str(UBound(imageArray,2)+1)
		Print ""
		
		'Prompt the user for the area to modify
		Print "Enter the desired bounds to change"
		Input "Row Lower Bound: ", Lrow
		Input "Row Upper Bound: " ,Urow
		Input "Column Lower Bound: ",Lcolumn
		Input "Column Upper Bound: ",Ucolumn
		
		Input "Enter a number for the desired color value: " , colorValue
		
		Print "Press a key to change the values to the desired value."
		Sleep
		Cls
'----------------------------------------------------------------		
		Print"Effected Values"
	
			For Row As Integer = Lrow to Urow
	    		For Column As Integer  = Lcolumn To Ucolumn 
	    		imageArray(Row,Column) = colorValue
	    		Print imageArray(Row,Column);
	      
	    	Next Column 
			Print ""
			Next
		Sleep
		Cls 
'-----------------------------------------------------------------	
		Print "Overall Values"
	
			For Row As Integer = 0 to imageW-1
	    		For Column As Integer  = 0 to imageH-1
	    		Print imageArray(Row,Column);
	    		Next Column 
			Print ""
			Next Row
		Sleep	
		Cls
	
		Print "Press a key to continue"
		Sleep		
	End Sub

'==================================================================================================================
Cls
'Display image back to user (Not sure what to do here)

/'For loop to give values from imageArray to imageBuffer except that imageBuffer is not an array. . .
For x As Integer = 0 To imageW
	For y As Integer = 0 To imageH
		imageBuffer(x,y) = imageArray(x,y)
	Next
Next
'/

'Need to find a way to copy values from imageArray to imageBuffer
Put (ScreenW/5,ScreenH/5), imageArray, (0,0)-(imageW,imageH),PSet

ImageDestroy(imageBuffer)
Sleep
Thanks
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Upload, Manipulate, and Display bmp back to user

Post by BasicCoder2 »

...
Last edited by BasicCoder2 on Jul 21, 2017 23:20, edited 4 times in total.
St_W
Posts: 1626
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: Upload, Manipulate, and Display bmp back to user

Post by St_W »

CloneFB wrote:My test images consist of 24-bit bmp files generated from MS paint of a single color.
I do not understand why the values correspond to a color: -1 for white, 0 for red, -96 for blue... etc.
A 24-bit bitmap actually uses 32 bits per pixel in the BMP file format as well as in FreeBasic's image buffer (see also Internal graphics formats). A pixel consists of alpha value, red, green and blue components in this order, a single byte (8 bits) each. This is also called ARGB for short. See also https://en.wikipedia.org/wiki/RGBA_color_space

In your program you're first retrieving image width and height. You must use ULong instead of UInteger there because the dimensions are always 32-bit values in the BMP file format. Currently your program won't work when compiled with the 64-bit version of FreeBasic because of that.

Later you are copying the pixel values into a separate array. Some hints for that part:
- As explained above a pixel uses 32-bits so you have to use ULong instead of (U)Byte for your array. You'll see the value best when converting it to hex. MSPaint's red will give the value FFED1C24, for example, where alpha=FF red=ED green=1C and blue=24.
- You're iterating over the image height in the inner loop (and you're calling it "Columns" while actually iterating over the rows) and over the width in the outer loop. Normally that is done the over way round because the image is stored in memory (and in the BMP file) in a row-per-row manner instead of column-per-column.
- The image buffer you created with ImageCreate already stores the pixels in memory and you can access those via (untested):
dim pixel as ULong = *cast(Ulong ptr, image.pixels + Y * image.pitch + X * image.bpp)
So you actually wouldn't need another copy of the pixel data in memory (except for convenience). See also http://freebasic.net/wiki/wikka.php?wak ... AndFontBuf
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Upload, Manipulate, and Display bmp back to user

Post by BasicCoder2 »

St_W wrote: - The image buffer you created with ImageCreate already stores the pixels in memory and you can access those via (untested):
dim pixel as ULong = *cast(Ulong ptr, image.pixels + Y * image.pitch + X * image.bpp)
So you actually wouldn't need another copy of the pixel data in memory (except for convenience). See also http://freebasic.net/wiki/wikka.php?wak ... AndFontBuf
This may be faster than processing images stored as arrays using for next loops however it is not as readable.

.
Last edited by BasicCoder2 on Jul 21, 2017 23:20, edited 1 time in total.
dafhi
Posts: 1641
Joined: Jun 04, 2005 9:51

Re: Upload, Manipulate, and Display bmp back to user

Post by dafhi »

...
Last edited by dafhi on Jul 19, 2017 23:01, edited 4 times in total.
Post Reply