3

So I am developing a type of tamagotchi (virtual pet) for my microprocessors final. I made my own images 128x64 pixels long, as I am using a display with that resolution, so each image weighs 1Kbytes. I am using an at89s52 (8052) microcontroller and it doesn't have enough memory to store all the animations I want. My plan (and I kind of want to keep it that way) is to use an EPROM to save all my images with intel hex format (the programmer I am using is SUPERPRO and it imports that type of files). Of course the assembly code will be easy for me after the point where I have the data in the ROM. I am not that good of a programmer to develop a code that does exaclty what I want (convert images to intel hex), and all the software I have tried doesn't generate them correctly (inserts hex values that aren't supposed to be there, for example: in a blank space there is supposed to be only zeroes, and there is another value). I have tried with png with transparent background, with white background and jpg. The images I have are such:

https://i.stack.imgur.com/86yCD.jpg (it seems I am not allowed to post images here)

I don´t see much help in other places of the internet, so the answer to this question would be of great help for future MCU-based programmers. Thank you.

old_timer
  • 69,149
  • 8
  • 89
  • 168
Neko
  • 41
  • 1
  • 5
  • Are they exclusively black-and-white images? This should be a trivial conversion—set the black bits to 1 and the white bits to 0. I'm not sure what's wrong with the software you found. (Also, I'm not sure what in the world "intel hex" is. How is Intel's hexadecimal format different than everyone else's? As far as I know, it isn't.) – Cody Gray - on strike May 07 '17 at 07:42
  • @CodyGray It's a [badly named format](https://en.wikipedia.org/wiki/Intel_HEX) that I've never heard of until now :) To the OP: What exactly are you asking? For a tool to converts images into Intel HEX? I'm afraid that's off-topic. Have you thought about using sprites? Or just burn a raw binary? – Margaret Bloom May 07 '17 at 09:25
  • not badly named there is intel hex and motorola srecord, if you only just heard of it now then either you are new to microcontrollers or the format is older than you are. eeprom programmers have been using one or the other file format since eeprom came out or actually likely before, needed the same formats for programming mcus back in the day when they were eeproms before flash and you plugged them into a programmer before moving to the board. – old_timer May 07 '17 at 09:40
  • this question has nothing to do with assembly language nor microcontrollers, it is simply about how to generate intel hex files from some raw data. all the rest of the question, etc is irrelevant from what i can tell. I wouldnt call this unclear what you are asking... – old_timer May 07 '17 at 09:41
  • @MargaretBloom actually intel hex is a perfectly named format, it uses hex for the data representation and comes from intel, actually motorola srecord is not bad either as each record in the file starts with an S and they were probably an also-ran to intel so they couldnt call it motorola hex that wouldnt make sense motorola s hex or others would be poorly named but intel hex and motorola s-record are very well if not perfectly named file formats. – old_timer May 07 '17 at 09:49
  • @old_timer Sorry, couln't think of the tags and I wanted to be clear :( – Neko May 07 '17 at 15:28
  • Your reasons for wanting to make an eeprom are fine, but the programming problem you have had to do with converting images into data into an eeprom programming file (in intel hex format) correct? Understanding why you wanted an eeprom (or some sort of storage device) was worth some level of explanation... – old_timer May 07 '17 at 15:35
  • @old_timer Yes, I thought I would have to write my own code to do this process, since I couldn't find resources and tools. And I wanted an eprom because I have more than 50kb of images and they don't fit in my MCU :D – Neko May 07 '17 at 15:38
  • there are alternatives to eeprom, and/or there are ways to deal with the data so it isnt as large. I dont know if you put raw pixels in there which you would get with using off the shelf image tools, or if you reduced the data down (two color, large chunks of white or black, you can remove most of that data with even a run length encoding, if nothing else, but there are other ways too). – old_timer May 07 '17 at 15:48
  • yes... I'm new to this... – Neko May 07 '17 at 16:41

1 Answers1

4

It's about 30 years since I last made an EPROM :-)

Anyway, you need 2 things...

Part One

Firstly, your files are PNG format which means they have dates, times, palettes, gamma chunks and a bunch of zlib compressed data and you can't just copy that to a screen buffer. So, you need to convert the PNGs to a simple binary format where 0 is off and 1 is on and there is nothing else in the file. The easiest way to do that is with ImageMagick which is installed on most Linux platforms and is available for free on macOS and Windows. Let's say one of your frames is called anim.png and we want to get it to a simple format, like PGM (Portable GreyMap - see Wikipedia description) we can use ImageMagick like this at the console:

convert anim.png -compress none anim.pgm

The first few lines will be:

P2
128 64
255
255 255 255 255 255 255 255 ...
...
...

because the image is 128x64 and the maximum brightness in the file is 255. Then all the data follows in ASCII (because I put -compress none). In there, 255 represents white and 0 represents black.

As that is too big for the screen, here is an image of how it looks - hopefully you can see your black box as a bunch of zeroes in the middle at the bottom:

enter image description here

Now, if you run that same command again, but remove the -compress none, the same header will be produced but the data will follow in binary.

convert anim.png anim.pgm

And we can also use sed to delete the 3 lines of header:

convert anim.png anim.pgm | sed '1,3d' > anim.bin

Now you have a binary file of just pure pixels that is free of dates/times, author and copyrights, palettes and compressed data, you can pass to the next part.

Part 2

Secondly, once you have got your data in a sensible binary format you need to convert it to Intel Hex, and for that you need srec_cat which is available for Linux daily and via homebrew on a Mac.

Then, I haven't tested this and have never used it, I think you will want something like:

srec_cat anim.bin -binary -output -intel 

:020000040000FA
:20000000323535203235352032353520323535203235352032353520323535203235352000
:200020003235352032353520323535203235352032353520323535203235352032353520E0
:200040003235352032353520323535203235352032353520323535203235352032353520C0
:200060003235352032353520323535203235352032353520323535203235352032353520A0
:20008000323535203235352032353520323535203235352032353520323535203235352080
...
:207E8000353520323535203235352032353520323535203235352032353520323535203202
:207EA0003535203235352032353520323535203235352032353520323535203235352032E2
:147EC000353520323535203235352032353520323535200A2A
:00000001FF

Summary

You can abbreviate and simplify what I am suggesting above - I will leave it there so folks can understand it in future though!

convert YourImage.png gray: | srec_cat - -binary -output -intel 

The gray: is a very simple ImageMagick format equivalent to just the binary part of a PGM file without any header. Like PGM it uses one byte per pixel so it will be somewhat inefficient for your pure black and white needs. You can see that by looking at the file size - the PGM file is 8192 bytes, so 1 byte per pixel. If you really, really want 1 bit per pixel, you could use PBM format like this:

convert YourImage.png pbm: | sed '1,3d' | srec_cat - -binary -output -intel 

Note:

  1. From v7 of ImageMagick onwards, you should replace convert by magick so as to avoid clashing with Windows' built-in convert command that converts filesystems to NTFS.

  2. ImageMagick is quite a large package, you could do this equally well just with the NetPBM suite, and use the tool called pngtopnm in place of convert.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • Thank you so much! I tried turning the images to binary and then using srec_cat to convert to hex... but I think I never considered the junk in pngs. And wow, I have never thought of making an EPROM, things of the past I guess. It brings tears to my eyes that there is such an active community that helps. – Neko May 07 '17 at 15:32
  • I am having trouble with the "sed" command as I am using windows. Any ideas sweet maestre? It says "sed" isn't recognized as an internal or external command, program or file by executable lots. – Neko May 07 '17 at 18:14
  • Windows? Ugh! I have never tried this but according to this post you can use `MORE +3` http://stackoverflow.com/a/11432438/2836621 – Mark Setchell May 07 '17 at 18:30
  • So, i used more +3 and worked like a charm on the bin. But after that, using the command srec_cat.exe 1.bin.new -binary -o 1.hex -intel, where 1 is my image, it generates a hex file that has more bytes than I need (I want the end to be 3FFH, as I said, I need to limit the image to 1kb, also there seems to be AHs and DHs instead of pure zeroes where the square is supposed to be) and the size in bytes (the beggining of the intel hex format) is 20H. I'm sorry to bug you so much :( – Neko May 07 '17 at 18:55
  • Ok, try using the very last example in my answer - the one with `convert YourImage.png pbm: ...` – Mark Setchell May 07 '17 at 19:01
  • with the +3 command I need to have a file first, how do I save the bin created with pbm:? – Neko May 07 '17 at 19:12
  • `convert YourImage.png image.pbm` which should be 1024 bytes. Then `more +3 image.pbm | srec_cat ...` should work. – Mark Setchell May 07 '17 at 19:19
  • I am stuck with the pbm of 1kb and after that I am using the sintaxis more +3 "1.bmp" >"1.bin" and after that srec_cat.exe 1.bin -binary -o 1.hex -intel, and it still generates thrash. I tried using your sintaxis more +3 1.pbm buuuut in windows it shows a more -3%- at the end of the screen. is there a way to convert the pbm to bin after I use the +3? – Neko May 07 '17 at 19:38
  • Sorry, Windows is just hopeless! Maybe you have Powershell? http://stackoverflow.com/a/14341672/2836621 – Mark Setchell May 07 '17 at 20:09