+ Code Snippet// Project: ColorDatabase
// Created: 2023-04-03
//#constant picWidth 256
//#constant picHeight 256
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "colorDatabase" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( 1024, 768 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
type _str
r as String
g as String
b as String
a as String
endtype
type _map
r as integer
g as integer
b as integer
a as integer
endtype
global maps as _map[]
SetRawWritePath(GetReadPath())
SetBorderColor(255,0,0)
dumpText("test1.txt",256) //dumps a randomstring of text to a file so as later it can be converted to an image
loadText("test1.txt") //loads the previously dumped text to convert to an image
img2=ConvertInfoToPic(256)
saveimage(img2,"testImage.png")
img2=loadimage("testimage.png") //
ConvertPicToInfo(img2)
//deleteImage(img2)
img2 = loadDumpImage2("test1.txt")
spr=CreateSprite(Img2)
SetSpritePosition(spr,0,0)
SetClearColor( 0,0,0 )
SetSpriteTransparency(spr,0 )
do
//clearscreen()
Print("test")
print(maps.length)
/*Print(chr(maps[maps.length].r))
Print(Chr(maps[maps.length].b))
Print(Chr(maps[random(1,maps.length)].g))
Print(Chr(maps[random(1,maps.length)].a))
*/
//if getpointerPressed()
x=GetPointerX():y=GetPointerY()
print(lookupRGBAatXY(x,y))
print(lookupRGBAatImageXY(img2,x,y))
//print(getStringFromImage(img2,32,1)) //gets the nth occurence of string from image delimeted by chr 32 sometimes troublesome
//endif
sync()
loop
/*
function dumpText(filename as string)
file = OpenToWrite(filename):count=0
ch as string
//WriteLine(file,words)
repeat
repeat
ch=chr(random(32,90))
until isPrintable(ch)
WriteByte(file,asc(ch)) //save characters in byte format
inc count
until count=64*64 //the dimentions of the picture
closefile(file)
endfunction
*/
function loadText(filename as string)
file = OpenToRead(filename)
myMap as _map
line as string
count =1
repeat
myMap.r = (ReadByte(file))
myMap.g = (ReadByte(file))
myMap.b = (readByte(file))
myMap.a = (readByte(file))
maps.insert(myMap)
until fileEof(file)=1
CloseFile(file)
endfunction
function ConvertInfoToPic(size)
//imgMemblock = CreateMemblockFromImage(img)
memsize = size*size*4 + 12
imgMemblock = CreateMemblock(memsize)
SetMemblockInt(imgMemblock, 0, size) // img width
SetMemblockInt(imgMemblock, 4, size) // img height
SetMemblockInt(imgMemblock, 8, 32) // img depth (must be 32)
//r=32
//imgMemblock = CreateMemblock(256*256)
//size = 12 +64 *64 *4 `memblock 'size' is summary of image width (integer, 4 bytes), image height (integer, 4 bytes), image depth (integer, 4 bytes) - that is 12 bytes - and raw image data (the number of pixels multiplied by 4 bytes)
//imgmemblock = createMemblock(size)
//deleteImage(img)
width = GetMemblockInt(imgmemblock,0)
height = GetMemblockInt(imgmemblock,4)
count=0: myMap as _map
global ww as integer:ww=width
global hh as integer:hh=height
if maps.length>0
for x=0 to width-1
for y =0 to height-1
if Count<=maps.length
myMap=maps[count]
offset = (12+((y * width) + x) * 4) - 4
SetMemblockbyte(imgmemblock,offset,myMap.r)
SetMemblockbyte(imgmemblock,offset+1,myMap.g)
setMemblockbyte(imgmemblock,offset+2,myMap.b)
SetMemblockbyte(imgmemblock,offset+3,myMap.a)
inc Count
endif
next
next
endif
newimg=CreateImageFromMemblock(imgMemblock)
DeleteMemblock(imgMemblock)
endfunction newImg
function ConvertPicToInfo(img)
imgMemblock = CreateMemblockFromImage(img)
//imgMemblock = CreateMemblock(64*64)
width = GetMemblockInt(imgmemblock,0)
height = GetMemblockInt(imgmemblock,4)
count=0
myMap as _map
for x=0 to width-1
for y=0 to height-1
offset = (12+((y * width) + x) * 4) - 4
myMap.r=GetMemblockbyte(imgmemblock,offset)
myMap.g=GetMemblockbyte(imgmemblock,offset+1)
myMap.b=GetMemblockbyte(imgmemblock,offset+2)
myMap.a=GetMemblockbyte(imgmemblock,offset+3)
maps.insert(myMap)
next
next
//deleteImage(img)
DeleteMemblock(imgMemblock)
endfunction //newImg
function makeRandomString(s$ as string,fromAsci as integer, toAsci as integer,n as integer,ammount as integer)
//Creates a random string of asci characters in the range fromAsci toAsci with the ammount of n
r$=s$
if n <= ammount //then exitfunction r$
r$=r$+(chr(random(fromAsci,toAsci)))
r$=makeRandomString(r$,fromAsci,toAsci,n+1,ammount)
endif
endfunction r$
//str as string:str=getStringFromImage(img2,Chr(32),1) //delimetered by character13 and location 1
function getStringFromImage(img as integer,dStr as integer, num as integer)
delimeter as integer:newStr as string:newStr="":count as integer: cStr$=chr(dStr)
imgMemblock = CreateMemblockFromImage(img)
width = GetMemblockInt(imgmemblock,0)
height = GetMemblockInt(imgmemblock,4)
count=0
myMap as _map:myStr as _str
for x=0 to width-1
for y=0 to height-1
offset = (12+((y * width) + x) * 4) - 4
myMap.r=GetMemblockByte(imgmemblock,offset)
myMap.g=GetMemblockByte(imgmemblock,offset+1)
myMap.b=GetMemblockByte(imgmemblock,offset+2)
myMap.a=GetMemblockByte(imgmemblock,offset+3)
//maps.insert(myMap)
myStr.r=chr(myMap.r)
myStr.g=chr(myMap.g)
myStr.b=chr(myMap.b)
myStr.a=chr(myMap.a)
if myStr.r=cStr$ :inc delimeter:inc count :endif
if myStr.g=cstr$ :inc delimeter:inc count :endif
if myStr.b=cstr$ :inc delimeter:inc count :endif
if myStr.a=cstr$ :inc delimeter:inc count :endif
//print(cStr$)
if delimeter>0
//print("madeit")
newStr=NewStr+myStr.r+myStr.g+myStr.b+myStr.a
endif
//ClearScreen()
if delimeter>0
if count>num
DeleteMemblock(imgmemblock)
//newStr=TrimString(newStr,mid(cStr$,2,1))
exitfunction newStr //_trimString(NewStr)
endif
endif
next
next
//print(r$)
DeleteMemblock(imgMemblock)
//newStr=_trimString(NewStr)
//newStr=TrimString(newStr,cStr$)
endfunction newStr
function _trimString(str as string)
ch as integer
for i = 1 to len(str)
ch=asc(mid(str,i,1))
if isPrintable(ch)
s$=s$+mid(str,i,1)
endif
next i
endfunction s$
print(lookupRGBAatXY(x,y))
print(lookupRGBAatImageXY(img2,x,y))
function lookupRGBAatXY(x as integer,y as integer)
myString as string
clearScreen()
setScissor(X,Y,X+1,Y+1)
render()
img2 = getImage(x,y,1,1)
rem create memblock
mem = createMemblockfromImage(img2)
offset = 12
rem get memblock data
r = GetMemblockByte(mem,offset)
g = getMemblockbyte(mem,offset+1)
b = getMemblockByte(mem,offset+2)
a = getMemblockByte(mem,offset+3)
rem tidy up
deletememblock(mem)
deleteimage(img2)
setScissor(0,0,0,0)
myString =chr(r)+chr(g)+chr(b)+chr(a)
endfunction myString
function lookupRGBAatImageXY(image,x as integer,y as integer)
myString as String
rem create memblock
mem = createMemblockfromImage(image)
//size=getMemblockInt(Mem, 0) // img width
width = GetMemblockInt(mem,0)
height = GetMemblockInt(mem,4)
if x<0 or y<0 then exitfunction ""
if x>width-1 or y>height-1 then exitfunction ""
offset = (12+((y * width) + x) * 4) - 4
rem get memblock data
r = GetMemblockByte(mem,offset)
g = getMemblockbyte(mem,offset+1)
b = getMemblockByte(mem,offset+2)
a = getMemblockByte(mem,offset+3)
myString =chr(r)+chr(g)+chr(b)+chr(a)
rem tidy up
deletememblock(mem)
//deleteimage(image)
endfunction myString
/*
function isPunctuation(ch1 as integer)
ch as string:ch=chr(ch1)
if (ch = "!" or ch = "\" or ch = "#" or ch = "$" or ch = "%" or ch = "&" or ch = "\" or ch = "(" or ch = ")" or ch = "*" or ch = "+" or ch = "," or ch = "-" or ch = "." or ch = "/" or ch = ":" or ch = ";" or ch = "<" or ch = "=" or ch = ">" or ch = "?" or ch = "@" or ch = "[" or ch = "\" or ch = "]" or ch = "^" or ch = "`" or ch = "{" or ch = "|" or ch = '}')
exitfunction 1
endif
endfunction 0
*/
function isPunctuation(ch as integer)
ch1 as string:ch1=str(ch)
punctuation as string[30] = ["!","\","#","$","%","&","\","(",")","*","+",",","-",".","/",":",";","<","=",">","?","@","[","\","]","^","`","{","|","}"]
punctuation.sort()
if punctuation.find(ch1) > -1 or ch=34 or ch=32 then exitfunction 1
//if ch=32 then exitfunction 1
//if str(ch) = StripString(str(ch), " !\#$%&\()*+-./:;<=>?@[]^`{|}") then exitfunction 1
endfunction 0
function isAlphaNumeric(ch as integer)
//ch1 as integer
//ch1=asc(left(ch,1))
if ch >47 and ch <58 then exitfunction 1 //numbers 0 to 9
if ch >96 and ch <123 then exitfunction 1 //lowercase letters
if ch >64 and ch <91 then exitfunction 1 //uppercase letters
endfunction 0
function isPrintable(ch as integer)
//ch1 as integer:ch1=asc(ch)
if (isAlphaNumeric(ch)=1 or isPunctuation(ch)=1)
exitfunction 1
endif
endfunction 0
// printable character range: 33 to 126 and 128 to 254
// size: width or height of image (it'll be square)
function dumpText(filename as string, size as integer)
file = OpenToWrite(filename)
dataSize = size*size*4
// write 12 bytes at start of file, this
// will simplify loading it later
writeInteger(file, 0)
writeInteger(file, 0)
writeInteger(file, 0)
for i = 1 to dataSize
repeat
ch = random(31,200)
until isPrintable(ch)
//if ch = 127 then ch = 126 // 127 is delete key
writeByte(file, ch)
next i
closeFile(file)
endfunction
function loadDumpImage2(filename as string)
m = createMemblockFromFile(filename)
// calculate size of image. This is why we added 12 empty bytes
// when writing the file, so the image data would be offset and leave
// room for the header info
size = sqrt((getMemblockSize(m)-12) / 4)
setMemblockInt(m, 0, size)
setMemblockInt(m, 4, size)
setMemblockInt(m, 8, 32)
img = createImageFromMemblock(m)
deleteMemblock(m)
endfunction img
Function Bit24to8BitColorConvert(img)
memblock = CreateMemblockFromImage(img)
width = GetMemblockInt(memblock, 0)
height = GetMemblockInt(memblock, 4)
size = GetMemblockSize(memblock)
for c = 12 to size - 1 step 4
ThisR = GetMemblockByte(memblock,c)
ThisG = GetMemblockByte(memblock,c+1)
ThisB = GetMemblockByte(memblock,c+2)
ThisR=(ThisR * 7 / 255) << 5
ThisG=(ThisG * 7 / 255) << 2
ThisB=(ThisB * 3 / 255)
//ThisC = (ThisR*0.30) + (ThisG*0.59) + (THisB*0.11)
SetMemblockByte(memblock,c,ThisR)
SetMemblockByte(memblock,c+1,ThisG)
SetMemblockByte(memblock,c+2,ThisB)
next c
ThisIMG = CreateImageFromMemblock(memblock)
//SaveImage(ThisIMG,"myimage.png")
DeleteMemblock(memblock)
EndFunction ThisIMG
what was discovered is that lookupRGBAatXY(x,y) and the lookupRGBAatImageXY(img2,x,y) will return different values
this is due to one grabbing a pixel to convert and the other looking up in the image directly.
The program does take a bit to load first time and once you have done so much can be commented out but i wanted to add
getStringFromImage(img2,32,1) will grab a string similar to trim string between delimetered text but will almost hang the program
if there isnt the string its searching for when its in the main loop hence why its commented out