News:

Building a 3D Ray Tracer  By stevmjon

Main Menu

Play-Pic2Map (Convert Images To maps)

Started by kevin, January 22, 2012, 01:07:22 PM

Previous topic - Next topic

kevin




 Visit www.PlayBasic.com


Play-Pic2Map (WIP THREAD)



  This thread will contain update information / ideas / changes etc that are being made to Play-Pic2Map application.  


What is Play-Pic2Map?

     It's a tool to assist PlayBASIC programmers convert large Images into something that can be drawn as a PlayBASIC map. The programmer loads the source image into the program and it exports a tile map, and an image containing the unique blocks used.  


Features

    * Pretty Fast Conversion.




Beta (Work In Progress) Downloads


     None.




Release Download


     Download Play Pic-2-Map  (Home Page)




kevin

#1
 
Play-Pic2Map V0.01 - Cut'N'Paste Application Building


      This is one of those little tools that just comes in handy every now and then.. The concept is that the users loads a (large) picture, then the tool runs through and cuts it up into a set of unique blocks with an tile map.  Pretty straight forward stuff, but handy none the less.    So far i've really just cut 'n pasted the application together from the standard frame work, with the a cleaned up version of the tile ripping routines.     Interestingly,  i've written a few of tools over the years, the last time dates back to something like like 17 or 18 years ago,  where a friend and I threw together a similar app on the Amiga to convert images into tile maps, called Jabber Blocky.    

      So far the app loads the image, cuts it up and saves it out.  It's all been pretty painless so far, it does need some settings though. So i'll need a bit of tinkering presenting the info to the user.  Just some controls to set the Block Width/Height in the grabber and a little viewer at the end..


buggage

#2
Here's something similar for Blitz that does the same job and spits out the map code, map data and sprites.

I'm sure it wouldn't be difficult to convert the outputted code to PB.

(login required)

[EDIT] I should have stated that the app is a Windows .EXE (and not a Blitz program), but that it can output Blitz code for the maps, but it will still work without Blitz.


LemonWizard

I had a feeling you used a program.
This is really awesome.
I could use this for splitting up and fetching more tiles from tile rip images.
I can't wait till this tool is ready.

Btw Kevin, I changed a few things in my game engine and now it doesn't use a large image at all.
The memory consumption went from 450, 000 kb to something like 91, 000 KB
It varies depending on what sprites/images are in memory but this engine by far takes up the most memory out of everything I've had.
I think if I fixed up the image formatting it would show even more improvement.

But yeah. I WANT THIS APPLICATION :P it would make so many of my projects way easier in the future

When is it going to be ready?

kevin

#4
 
Play-Pic-2-Map V0.03 - That'll Do Pig, That'll do....


      Put a bit more work into this tonight, the functionality hasn't really changed from the initial version.  You still load the image and it cuts it up and dumps out the files.  The only change has been adding some dialogs so the user can set the rippers block size, as well as a percent bar (which basically slows the process down :) ) and viewing mode at the end..    The ripper supports blocks as low as 2x*2y, bellow that and it's just palette mapping the image.    

       Here's a bit of video showing the entire process, will probably give it a bit of clean up pass and then release it as is..

     



kevin

#5
 
Play-Pic-2-Map V0.04 - Double Packing Pictures


   Ok, so what these types of programs do, is they compress the original source image  by removing any redundant repetitive parts, leaving us with the unique parts and the data on how the blocks are to be reassembled, to recreate the original picture.    Now the thing is, even though your picture might be laid out on a 32x by 32y grid, this may not be the most efficient size for the blocks, as sections of the unique blocks may indeed repeat also.

   So what you can do, is re-compress the 32 by 32 unique blocks down into a smaller sizes (eg half/quarter).  Now for the sake of an example, i took the Castle Map and converted that into a 32 by32 block map with level map.  In raw terms, we're converting the 25 meg (512x * 12256y * 4 byte ) level image and compressing it down to approximately 430K total.  Now this process creates a unique block map image that's 320x*320y, which is the bulk of the 430k,   but rather than keep this block image, we then pass it back through Pic-2-Map again.  This time using a block size of 8 By 8 to further reduce, which spits out a 178x*178y block map + level weighing in at only 128K.   

  Long story short, this means the original 25 meg picture can be represented (on disc at least)  in only 145K without much effort at all.   To load the map is a little messy, since it means we've a two pass process.  First we load the 8*8 blocks + level, then render this level out to an image, thus creating the 32 by 32 block map image in the process.    Once that's created, we can delete the 8 by 8 map (don't need it anymore), create a new map, import the 32 by 32 blocks into the map, then load the 32*32 level.  

  There's a number of benefits that come to mind, but the most obvious one with such an approach, is that just by paying a little more attention to the size of source media, we can make a dramatic impact on overall size of our final game.   

  Anyway,  I've attached the example bellow...  

kevin


LemonWizard

Kevin I think you are the most awesome programmer I know.
I was having a hard time manually extracting the unique tiles from these large world map images of DKC2 levels, and when I first began editing them with gimp I realized alot of those resources were aligned with a 32*32 grid.
The fact that your program was able to redraw all the tiles properly proves they were sized at a proper 32*32 grid.
Which means, I can begin the process of tile ripping these images.
You saved me a ton of work with this program and I am so grateful.
Thank you.

The previous tileset I was using was incomplete, and the ones I saw online were also incomplete. This will make alot of future projects for me easier.


kevin

#8
  Play-Pic-2-Map V0.06 - Faster conversion

    Converting images to maps is very handy and generally wins us both gains in file size as well execution speed benefits also, the only trouble is, converting big images or using small block sized could be pretty slow.  Which is not too surprising given how quickly Pic2Map was put together.

   Looking over the conversion code this morning/evening and there's an obvious bottle neck in the central searching routines.   The initial thinking was to just make those routines into dll's via PlayBASIC2DLL and that'd be that, which worked fine to a point.  The real problem is it doesn't solve the brute froce nature of the routine,  it meant the routine can handle the data much quicker than it normally would, but if you use small blocks, conversion would still be slower than it should be.    The bottleneck occurs when it scans the source image block by block, it'd compare this block (pixel by pixel) to all previous blocks looking for a match.   Even with some checksum styled pre-screening there's a lot of brute force searching going on.

   The solution should be obvious, we need get rid searching  through all the blocks every time, in other words, it needed to a build a type of short list, so the searching routine can then find similar tiles with minimal search iterations.  To do that means flipping the routine upside down, so the new version is broken into 2 passes.


    The first pass just runs through the source image and builds an array with the location (on the source image) of the block and a pixel level check sum.       Once we have all tiles computed we then need to sort the tile data into order based on the checksums.  I just used variation of radix sort with a reference array, which is just a second integer array to stores the 'order' in which the blocks are.  We don't actually rearrange the  blocks array.  


     The second pass we scan through the sorted blocks, checking the current block checksum with  the next block, if the checksums match, we move onto comparing pixel data.  If the pixel matches, we tag this block as being identical too the previous block.     This elements the need to the search the entire data set, we don't even need to bother with list/tree structue really.

      Here's the basic logic of the searching routine.  

PlayBASIC Code: [Select]
     For ThisBlock = 0 to NumberOfBLocks-1

ThisCheckSum = Tiles(ThisBlock).CheckSum

; check if there's a run of bocks with the same checksum ?
if ThisCheckSum = Tiles(ThisBlock+1).CheckSum

; search forward so we're comparing THISBLOCK with the ones following it
For CompareBlock = ThisBlock+1 to NumberOfBLocks

if ThisCheckSum = Tiles(CompareBlock).CheckSum

; here'd compare the pixels..
if ComparePixels(ThisBLock, COmpareBlock)= true

Tiles(CompareBlock).SameAsThisTile = ThisBlock

endif

else
ExitFor CompareBLock
endif

next

endif
next




      So at the end of this you can build your unique blocks by scanning through the tiles() array and only copying out the tiles that don't have the SameAsThisTile field set.   In the new version these fields are preset to impossible values of -1.   As tile zero is a  legal tile index.  

       To test the speed i'm just the same castlecrush.PNG with block sizes of 8*84*4 and even 2*2..      Such small sizes would take minutes, generally many minutes for the smaller sizes and big images with lots of unique tiles.   Now it seems a lot more consistent in terms of speed takes about 6->8 seconds (just regular PB code) to handle the 512x * 12256y sized source image.   



kevin


PlayBASIC-Pic-2-Map V0.07 - Released  - 22nd,Jan,2015

    Here's the latest build of the picture to map conversion tool.   For those that are not familiar this tool loads a picture (BMP / PNG format recommended) and converts this picture into a series of unique tiles with an accompanying PlayBASIC level file.   The user can pick the size of the block and even the alpha masking applied to the image before conversion. 

   For this release we've update the conversion method and converted some of the conversion routines machine code via PlayBASIC2DLL.   So in other words, we took the main work horse routines and build a DLL of them and just linked them back into our program.   The result gives much faster conversion when the user selects tiny block sizes, such as 2*2 or 4*4

   



History




V0.07   - 19th,Jan,2015  - 21st,Jan,2015

- Changes
- replace the typed tile array with typed pointer structure
  to cut down on type allocated when converting images with lots
  and of tiles
- Exported most of conversion functions to a separate PBA
  for creation to DLL 

- Added
- History dialog from PlayBASUC2DLL
- ARGB pixel masking to block conversion


V0.06   - 14-01-2015  - 18th,Jan,2015

- Changes
- replaced conversion method
- removed PlayDialogs and replaced with WindowsFileDialog include        






  Download


  Download PlayBASIC Pic2Map and other free tools from PlayBASIC  Tools page