There are several tasks that require operating on an image.
I wrote some
basic ppm functions.
Perhaps we can implement some of the
image manipulation tasks.
I am having a little trouble initializing the image with zeros...
I think some more binary zero support in autohotkey will be usefull.
ppm := ppm_new(50, 50, 255) ; 0 pixels are not well supported
ppm_fill(ppm, 89, 84, 85) ; because AutoHotkey does not support
msgbox % getPixel(ppm, 1, 1) ; binary zeroes well
setPixel(90, 90, 90, ppm, 1, 1)
msgbox % getPixel(ppm, 1, 1)
msgbox % ppm
return
ppm_read(file)
{
fileread, ppm, % file
return ppm
}
ppm_width(ppm)
{
regexmatch(ppm, "\R(\d+)\s(\d+)", dim)
return dim1
}
ppm_height(ppm)
{
regexmatch(ppm, "\R(\d+)\s(\d+)", dim)
return dim2
}
ppm_colors(ppm)
{
regexmatch(ppm, "\R(\d+)\D*\R", colors) ; \R stands for any
return colors1
}
ppm_data(ppm)
{
pos := regexmatch(ppm, "\R(\d+)\D*\R", colors) ; \R stands for any newline
stringtrimleft, data, ppm, pos + strlen(colors1)
return data
}
ppm_header(ppm)
{
pos := regexmatch(ppm, "\R(\d+)\D*\R", colors) ; \R stands for any newline
stringleft, header, ppm, pos + strlen(colors1)
return header
}
ppm_fill(ByRef ppm, r, g, b)
{
width := ppm_width(ppm)
height := ppm_height(ppm)
header := ppm_header(ppm)
headerLength := strlen(header) + 1
varsetcapacity(data, width * height, 0)
loop, % (width * height)
{
if r
numput(r, data, (A_Index - 1) * 3, "uchar")
if g
numput(g, data, (A_Index - 1) * 3 + 1, "uchar")
if b
numput(b, data, (A_Index - 1) * 3 + 2, "uchar")
}
VarCopy(&ppm + headerLength, &data, width * height)
}
ppm_new(width, height, colors)
{
header = P6`n%width% %height%`n%colors%`n
headerLength := strlen(header)
varsetcapacity(ppm, width * height + headerLength, 1) ; a zero here in 3rd parameter causes problems.
VarCopy(&ppm, &header, headerLength)
return ppm
}
VarCopy(Target, Source, Length) {
DllCall("RtlMoveMemory","uInt",Target,"uInt",Source,"uInt",Length)
}
getPixel(ppm, x, y)
{
data := ppm_data(ppm)
r := numget(data, x * y, "uchar")
g := numget(data, x * y + 1, "uchar")
b := numget(data, x * y + 2, "uchar")
pixel = %r%`n%g%`n%b%
return pixel
}
setPixel(r, g, b, ByRef ppm, x, y)
{
header := ppm_header(ppm)
L := strlen(header)
numput(r, ppm, x * y + L, "uchar")
numput(g, ppm, x * y + L + 1, "uchar")
numput(b, ppm, x * y + L + 2, "uchar")
}
By the way, AutoHotkey is now
ranked 34 in the most popular languages on rosettacode, bypassing javascript. 90 tasks are implemented. half way to top 10.