Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Some Image Support Functions (Alternatively)

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
Corroder
Grandmaster Cheater Supreme
Reputation: 75

Joined: 10 Apr 2015
Posts: 1668

PostPosted: Sat Sep 26, 2020 8:41 pm    Post subject: Some Image Support Functions (Alternatively) Reply with quote

From these topics:

The best until now:
https://forum.cheatengine.org/viewtopic.php?t=609506

and this embedded style:
https://forum.cheatengine.org/viewtopic.php?t=610353

Here is an alternatively to processing an image using CE Lua script.

Some functions for:
1. Flip image vertically by pixel
2. Flip image vertically using copy rect
3. Flip image horizontally by copy rect
4. Rotate image by degree angle (pixel)
5. Turn image color to black and white (pixel)
6. Negates an image (pixel)

WARNING:
- Using image pixel make the process slow, so consider your image pixel size to use it.
- Using pixel make 'tricky' the CPU

You are free to edit and modify the script to get better result as your own style.

Script and example:

Code:
-- Helpers
function getRGB(col)
 local r,g,b = col & 0xff, col >> 8 & 0xff, col >> 16 & 0xff
 return r,g,b
end

function makeColor(r,g,b)
 local colorBitShiftRed = 0
 local colorBitShiftBlue = 16
 local colorBitShiftGreen = 8
 local function clamp(value, min, max) return math.min(math.max(value, min), max) end
 r = 0 or clamp(r, 0, 255); g = 0 or clamp(g, 0, 255); b = 0 or clamp(b, 0, 255);
 return (r << colorBitShiftRed) | (g << colorBitShiftGreen) | (b << colorBitShiftBlue)
end

--Flips vertically a picture
function getImageFlipVertical(img)
 local w = img.width-1
 local h = img.height-1
 local flip = createImage(self)
       flip.width = w+1
       flip.height = h+1
 for y = h, 0, -1 do
     for x = 0,w,1 do
      local col = img.Picture.Bitmap.Canvas.getPixel(x,y)
            flip.Canvas.setPixel(x,h-y,col)
      end
   end
 --return flip
 img.Picture = flip.Picture
 -- optional
 img.Left = 0
 img.Top = 0
 img.Width = flip.Width
 img.Height = flip.Height
 img.Stretch = true
 flip.Destroy()
end

-- Rotates a picture
-- BackColor is Optional
function Rotate(img, angle)
 local angRad = angle*math.pi/180
 local cosine = math.cos(angRad)
 local sine = math.sin(angRad)
 local width_out = math.ceil(img.width * math.abs(cosine) + img.height * math.abs(sine))
 local height_out = math.ceil(img.width * math.abs(sine) + img.height * math.abs(cosine))

 local out = createImage(self)
       out.width = width_out
       out.height = height_out
       out.Canvas.Clear()
       out.Picture.Bitmap.Canvas.TransparentColor = 0
       out.Picture.Bitmap.Canvas.Clear()

 local x_center_image = img.width / 2
 local y_center_image = img.height / 2
 local x_center_outImage = out.width / 2
 local y_center_outImage = out.height / 2

   for y = 0, height_out-1 do
      for x = 0, width_out-1 do
            local outX = math.ceil(cosine*(x-x_center_outImage)+sine*(y-y_center_outImage)+x_center_image)
            local outY = math.ceil(-sine*(x-x_center_outImage)+cosine*(y-y_center_outImage)+y_center_image)

         if outX >= 0 and outX < img.width and outY >=0 and outY < img.height then
             out.Canvas.setPixel(x, y, img.Picture.Bitmap.Canvas.getPixel(outX,outY))
         end
      end
   end
 img.Picture = out.Picture
 img.Left = 0
 img.Top = 0
 img.Width = out.Width
 img.Height = out.Height
 img.Stretch = true
 out.Destroy()
 --return out
end

-- Resizes a picture
function Resize(img,newX,newY)
 local calc = createImage(self)
       calc.width = newX
       calc.height = newY
 local w, h = img.width, img.height
 for x = 1, newX do
     for y = 1, newY do
         local intX = math.floor(x*(w/newX))
       local intY = math.floor(y*(h/newY))
       calc.Canvas.setPixel(x,y,img.Picture.Bitmap.Canvas.getPixel(intX,intY))
     end
 end
 img.Picture = calc.Picture
 img.Left = 0
 img.Top = 0
 img.Width = calc.Width
 img.Height = calc.Height
 img.Stretch = true
 calc.Destroy()
 --return calc
end

-- Negates a picture
function nvgImage(img)
 local im = createImage(self)
       im.width = img.width
       im.height = img.height
 for y =0,img.height-1 do
      for x = 0, img.width-1 do
        local color = img.Picture.Bitmap.Canvas.getPixel(x,y)
         r,g,b = getRGB(color)
        local nwCol = makeColor(255-r,255-g,255-b,a)
        im.Canvas.setPixel(x,y,nwCol)
     end
 end
 img.Picture = im.Picture
 img.Left = 0
 img.Top = 0
 img.Width = im.Width
 img.Height = im.Height
 img.Stretch = true
 im.Destroy()
 --return im
end

-- Flashes a picture
function flashImg(img)
 local canalMin = {["r"]=255,["g"]=255,["b"]=255}
 local canalMax = {["r"]=0,["g"]=0,["b"]=0}
 local w,h = img.width,img.height
 local out = createImage(self)
       out.width = w
       out.height = h

 for y=0,h-1 do
      for x=0,w-1 do
        local col = img.Picture.Bitmap.Canvas.getPixel(x,y)
         r,g,b = getRGB(col)
         local colSet = {}
         colSet.r = r
         colSet.g = g
         colSet.b = b
        for k in pairs(colSet) do
            if colSet[k]<canalMin[k] then canalMin[k]=colSet[k] end
            if colSet[k]>canalMax[k] then canalMax[k]=colSet[k] end
       end
     end
 end

 for k,v in pairs(canalMin) do print('min',k,v) end
 for k,v in pairs(canalMax) do print('max',k,v) end

 for y=0,h-1 do
      for x=0,w-1 do
        local col = img.Picture.Bitmap.Canvas.getPixel(x,y)
         r,g,b = getRGB(col)
         local colSet1 = {}
         colSet1.r = r
         colSet1.g = g
         colSet1.b = b
          for k in pairs(colSet1) do
           colSet1[k] = ((colSet1[k]-canalMin[k])*255)
           colSet1[k]= colSet1[k]/(canalMax[k]-canalMin[k])
         end
       local nwCol  = makeColor(colSet1.r,colSet1.g,colSet1.b,img.Picture.Bitmap.Canvas.getPixel(x,y))
       out.Canvas.setPixel(x,y,nwCol)
     end
 end
 img.Picture = out.Picture
 img.Left = 0
 img.Top = 0
 img.Width = out.Width
 img.Height = out.Height
 img.Stretch = true
 out.Destroy()
 --return out
end

--- Flip using copy rect
function FlipHorizontal(img)
 local DummyImage, X, Y
 X = img.Picture.Bitmap.Width
 Y = img.Picture.Bitmap.Height
 DummyImage = createImage(self)
 DummyImage.Picture.Bitmap.Width = X
 DummyImage.Picture.Bitmap.Height = Y
 DummyImage.Picture.Bitmap.Canvas.CopyRect(X,0,0,Y, img.Picture.Bitmap.Canvas, 0,0,X,Y)
 img.Picture = DummyImage.Picture
 img.Left = 0
 img.Top = 0
 img.Width = img.Width
 img.Height = img.Height
 img.Stretch = true
 DummyImage.Destroy()
end

function FlipVertical(img)
 local DummyImage, X, Y
 X = img.Picture.Bitmap.Width
 Y = img.Picture.Bitmap.Height
 DummyImage = createImage(self)
 DummyImage.Picture.Bitmap.Width = X
 DummyImage.Picture.Bitmap.Height = Y
 DummyImage.Picture.Bitmap.Canvas.CopyRect(X,0,0,Y, img.Picture.Bitmap.Canvas, X,Y,0,0)
 img.Picture = DummyImage.Picture
 img.Left = 0
 img.Top = 0
 img.Width = img.Width
 img.Height = img.Height
 img.Stretch = true
 DummyImage.Destroy()
end

--- Turn image to black and white
function BW(Img)
 local Thresold = 128
 local Pixel, Intensity, r, g, b, newPixel

 local out = createImage(self)
       out.width = Img.Width
       out.height = Img.Height
       out.Picture.Bitmap.width = Img.Width
       out.Picture.Bitmap.height = Img.Height
       out.Canvas.Clear()
       out.Picture.Bitmap.Canvas.Clear()

 for y= 0, Img.Height-1 do
     for  x = 0, Img.Width-1 do
         Pixel = Img.Picture.Bitmap.Canvas.getPixel(x,y)
         r,g,b = getRGB(Pixel)
         Intensity = (r + g + b) / 3
         if Intensity <  Thresold then
            newPixel = 0
         else
            newPixel = 0xffffff
        end
       out.Canvas.setPixel(x, y, newPixel)
       end
 end
 Img.Picture = out.Picture
 Img.Left = 0
 Img.Top = 0
 Img.Width = out.Width
 Img.Height = out.Height
 Img.Stretch = true
 out.Destroy()
end


---- Testing
if f then f.destroy() end
f = createForm()
f.setSize(300,300)
f.Position = 'poScreenCenter'
f.Caption = 'Original Image'

Image1 = createImage(f)
Image1.setSize(300,300)
Image1.setPosition(0,0)
Image1.stretch = true
Image1.Picture.loadFromStream(findTableFile('sample.png').stream)
Image1.Transparent = true

UnitTest = [[
--flip vertical by pixel
f.Caption = 'Flip Vertical by pixel'
getImageFlipVertical(Image1)

--flip vertical by copy rect
f.Caption = 'Flip Vertical by rect'
FlipVertical(Image1)

--flip Horizontal by copy rect
f.Caption = 'Flip Horizontal by rect'
FlipHorizontal(Image1)

--Rotate by 70 degree
f.Caption = 'Rotate Image 70 Degree'
Rotate(Image1, 70)

--Negates an image
f.Caption = 'Negates Image'
nvgImage(Image1)

--Flashing an image (not tested)
--r value from RGB has not fixed yet
f.Caption = 'Flashing Image'
flashImg(Image1)

--Turn an image to black and white color
f.Caption = 'Black And White Image'
BW(Image1)
]]


I share the scripts above since I didn't plan to share my 'CE Image Processing Tool' app.

Good luck...



ImageHandler.jpg
 Description:
Results Example
 Filesize:  141.83 KB
 Viewed:  759 Time(s)

ImageHandler.jpg



_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites