PNG Slicer

Just starting out? Need help? Post your questions and find answers here.
Phollyer
Enthusiast
Enthusiast
Posts: 140
Joined: Sat Jun 03, 2017 3:36 am
Location: USA, Texas
Contact:

PNG Slicer

Post by Phollyer »

I have a Question, I'm writing a Client Server Wargame engine, and the server needs to control everything. The initial Game Start the server is initialized with a "local" Game folder consisting of a JSON Game file a Map Image (PNG) file, Grid Image (PNG), and 2 Sprite Sheets both PNG.
The Transmitted Buffer Size I've Choose (for NO specific Reason other than I've always like 4K or 4096).
I began looking at ways to slice up the PNG File and came up with the following code. I take any large PNG Slice it up into an Array of memory allocations, then spin the Array and write it back to a New file, Clean up memory and it opens great. It Works, all I would need to do send these Chunks to the Game Player Clients...which I don't think will be too tough.

So the question is...Is there a better stronger faster way? Anyone's thoughts?

(You can choose ANY image file on your pc in the Code)

Code: Select all


Define BlockCnt.i, Block, Remain.i, Length.i

#File$ = "D:\Development\PBProjects\VideoGame\Images\VideoWarGame.png"
Block = 4000


  If ReadFile(0, #File$)
    length = Lof(0)
    *MemoryID = AllocateMemory(length)
    BlockCnt = length / Block
    Remain = Mod(length, Block)
    Dim Chunks.i(BlockCnt+1)
    If *MemoryID
      For X = 0 To BlockCnt
        If X < BlockCnt
          Chunks(X) = AllocateMemory(Block)
          ReadData(0, Chunks(X), Block)
        Else
          Chunks(X) = AllocateMemory(Remain)
          ReadData(0, Chunks(X), Remain)
        EndIf
      Next
    EndIf
    CloseFile(0)
  EndIf
  ;Write Chunks back into file
  CreateFile(0, "D:\Development\PBProjects\VideoGame\Images\VideoWarRedo.png")
  For X = 0 To BlockCnt
    If X < BlockCnt
      WriteData(0, Chunks(X), Block)
    Else
      WriteData(0, Chunks(X), Remain)
    EndIf
  Next
  CloseFile(0)
  FreeMemory(*MemoryID)
  For X = 0 To BlockCnt
    FreeMemory(Chunks(x))
  Next
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: PNG Slicer

Post by RASHAD »

Why don't you load the PNG image then repeat using GrabImage(#Image1, #Image2, x, y, Width, Height)
to slice the main image to your required slices then free the main png
Egypt my love
Phollyer
Enthusiast
Enthusiast
Posts: 140
Joined: Sat Jun 03, 2017 3:36 am
Location: USA, Texas
Contact:

Re: PNG Slicer

Post by Phollyer »

That's a good Idea!
Thanks
Post Reply