r/dailyprogrammer • • Jul 21 '14
[7/21/2014] Challenge #172 [Easy] ■■□□□▦■□
Description
A portable bitmap is one of the oldest image formats around and grants access to very simple image creation and sharing. Today, you will be creating an image of this format.
A simple PBM program can be seen [here] (http://en.wikipedia.org/wiki/Netpbm_format) (Note that we'll be creating the simplest version, a PBM, not PPM or PGM.)
But basically the program consists of the following:
- A 2byte string (usually 'P1') denoting the file format for that PBM
- 2 integers denoting the Width and Height of our image file respectively
- And finally, our pixel data - Whether a pixel is 1 - Black or 0 - White.
Formal Inputs & Outputs
Input description
On standard console input you should be prompted to enter a small piece of text ("programming", "proggit", "hello world" etc...)
Output description
The output will be a .PBM file consiting of an image which contains the text you have entered
Notes
/u/chunes has kindly mapped all alpha characters to their 0 1 equivalents, saving you a lot of time.
https://gist.github.com/anonymous/0ce707518d9e581499f5
Here is a worthwhile tutorial on the PBM format and programming for it
http://blog.plover.com/prog/perl/lines.html
The .PBM (you may also see it called NetPBM) is not very well supported any more, this makes actually viewing the PBM difficult as not many programs support it.
Feel free to download software which would render your .PBM to the screen but for all intents and purposes, the format is more important than the output cosidering the difficulty of viewing the image.
Finally
Have a good challenge idea?
Consider submitting it to /r/dailyprogrammer_ideas
21
u/chunes 1 2 Jul 21 '14
Here's a font file you guys can read for the challenge since it's a bit tedious to come up with it yourself. https://gist.github.com/anonymous/0ce707518d9e581499f5
9
u/skeeto -9 8 Jul 21 '14 edited Jul 21 '14
C, using chunes's font packed into two strings. It puts a single column of padding between characters without trailing padding.
I love NetPBM. It's a really convenient format for creating images without a specialized image library, and all the software I care about (qiv, ImageMagick, Gimp, Emacs) can display and handle NetPBM just as easily as it can other formats. Here's a scaled-up version of the output:
2
u/Vijfhoek 1 0 Jul 27 '14 edited Jul 27 '14
x86 Assembly (FASM, Windows)
I accidentally made the P4 format instead of P1 (binary instead of decimal). Hope you don't mind!
Source: https://github.com/Vijfhoek/DailyProgrammer/tree/master/172%20Easy%20ASM
main.asmcontains the source code andfont.incthe font.EDIT A great pbm (and many other image formats) viewer is Irfanview. It might look a little dated, but I've used it since I first touched a computer. Do make sure to only open images with it though, as the video viewer isn't that great.
15
u/llasarus 1 0 Jul 21 '14
Brainfuck I made a brainfuck program here sometime last year and I haven't made anything before or since; so here is another brainfuck solution, I hope you guys find it interesting:
The program takes an input from stdin and prints the result in stdout. Since brainfuck doesn't have any filesystem it wasn't possible for me to write to a file directly. I have only tested the code in my custom made interpreter, but I suppose it will work in any other as well.
Sample outputs (the font is only 3x5)
I hope my formatting is right, I'm currently using my laptop and I haven't got RES installed. (FYI: Everything is completely hand written, a little copy paste in some places, but no generated code)
6
u/lushr Jul 21 '14 edited Jul 21 '14
Java 8
Note that this uses the JLine terminal library.
This system uses the system font library to fetch the default sans serif font.
Edit: Fix for people who aren't on Windows.
4
u/Godspiral 3 3 Jul 21 '14
in J
icons =: ({. , 0 ". each }.)("1) 26 8 $ cutLF wd 'clippaste'
bmp =: (, > {."1 icons)&([: >@:}."1 icons {~ i.)
saving as a list of tiles in binary format
save =: 3!:1
read =: 3!:2
read/save and reformat tile output:
3
u/mjd Jul 21 '14
The program
cjpeg, which is distributed as part of the JPEG reference implementation, transforms a PBM file to a JPEG file. On my Ubuntu machine, this program can be installed with the commandapt-get install libjpeg-turbo-progs.3
u/KarambaMuchacho Jul 21 '14 edited Jul 21 '14
Below is my solution in C#, I've used chunes's font file. I am a beginner so any suggestions are very appreciated.
For sample input string "H a i" the output is:
Code:
EDITS Formatting issues.
3
u/TiZ_EX1 Jul 21 '14 edited Jul 22 '14
Don't worry about displaying? But I spent practically all of last week displaying this format.
EDIT: Updated with a lot of things. Added a lot more characters to the font file. And the algorithm just happens to be able to handle variable-width characters, so I made some in font.txt. font.txt is now responsible for having a space character. My font.txt is here.
ECMAScript6 on node.js:
Pretty-printed version here.
Display mode in action:
3
u/irish_ayes Jul 24 '14
Here's my code using C#. It's my first submission, and it's been a long while since I put any code down. I'm sure there's a lot of shorthand I could be using, and some more efficient techniques, but it works.
2
u/gfixler Jul 22 '14
Here's a messy Clojure version to go along with my messy Haskell version. I'm in a messy mood today. Feel free to critique.
2
u/datmohtingting Jul 22 '14 edited Jul 22 '14
>EASY
C#
2
u/DadFoundMyAccount Jul 24 '14
Java: A little late but here's my ugly and slow code:
And A Sample output:
2
u/ddsnowboard Jul 24 '14 edited Jul 24 '14
Python 3, that link at the top is the .txt of chunes' font file.
I'm sure there's plenty of stuff here that's not very good practice, so please let me know if you see something stupid.
EDIT: Added a space character.
2
u/kuzux 0 0 Jul 26 '14
I'm in a hotel room and don't have my computer with me. For an easy problem, pen and paper was easier to use than typing the program on the phone keyboard.
Here's my solution in Haskell, written on paper obviously not tested, hopefully it works :)
http://i.imgur.com/SemoJtg.jpg
2
u/OllieShadbolt 1 0 Jul 21 '14 edited Jul 21 '14
Python 3.2.5 Two-liner;
Currently accepts A-Z upper and lower case, as well as spaces, printing the output into 'output.pbm'. Thank you to /u/chunes for this font I ended up using. Most of the code is correctly formatting the font ready for use, and I know that I can put this in an external file to run from, but as I'm posting it here I thought it would be better to post all of the code as a whole.
If I've made any mistakes, as I probably have as I did it in a rush and I'm still learning, please let me know <3
Input of 'Hello World' results in the 'output.pbm' file reading the following;
Edit; Modified the text so it's easier to read in the output, and general post format sorting
1
u/lelarentaka Jul 21 '14 edited Jul 21 '14
Scala 2.10.3+
Gist: https://gist.github.com/AliceCengal/f1e4b54d37734c37c357
I stored the rendering information in another file, whose name is passed in as the parameter of the script. It looks like this for the first three letters:
That way, one could ontensibly provide a custom font to the renderer.
The word to be rendered is passed in via standard input, and the rendering is printed to standard output, from which you can save the result to a file or pipe to an imaging program.
I added a one pixel padding around the letters. Could make that customizable too through a second parameter to the script.
Script:
Sample output for "HELLO":
1
u/MaximaxII Jul 21 '14
Here's my solution in Python - feedback is welcome!
Challenge #172 Easy - Python 2.7
The font
The hard part was formatting /u/chunes' font, so save yourself some time and use this:
As you may have noticed, it's a font dictionary - every item in the list corresponds to one line of the representation of the character.
1
u/datmohtingting Jul 21 '14
I can't wrap my head around how to format everything horizontally, my output is like this:
https://gist.github.com/anonymous/a19d538a1edeae077249
Instead of this
https://gist.github.com/anonymous/ba2fd872e269909dd6ee
I'm using C#
1
u/mjd Jul 21 '14
The program
pbmtexttakes a string and an optional font file and produces a bitmap image displaying the string. The font file is itself a PBM file that just has bitmaps of all the characters in a 12×8 grid.1
u/ENoether Jul 21 '14
Python 3; accepts upper and lower case along with spaces, but converts to uppercase. Uses the font from /u/chunes, but is written to work with any given font as long as the file is formatted the same way. As always, feedback and criticism welcome:
Run:
1
u/thebmo Jul 21 '14
PYTHON 2.7.3 utilzing /u/MaximaxII 's impressive dict... get it? anyone? I added a bit of console feedback. e: forgot my output
OUTPUT: Python Rulz
1
u/[deleted] Jul 21 '14 edited Mar 08 '24
Reddit Wants to Get Paid for Helping to Teach Big A.I. Systems The internet site has long been a forum for discussion on a huge variety of topics, and companies like Google and OpenAI have been using it in their A.I. projects. "The Reddit corpus of data is really valuable," Steve Huffman, founder and chief executive of Reddit, said in an interview. "But we don’t need to give all of that value to some of the largest companies in the world for free."
1
u/mikrostew Jul 21 '14
In Ruby
I converted the text file from /u/chunes into json (https://gist.github.com/mikrostew/f4091d32f046febff54b) and I'm using that in the script.
1
u/thestoicattack Jul 21 '14
bash, assuming a modern sed for the "~" specifier (i.e., not the one in Mac OS):
1
u/[deleted] Jul 21 '14 edited Jul 22 '14
Java, replaced zeros with spaces so console output is readable.
I haven't the faintest clue how to actually output to an image. I did some googling and found not much in regards to it.I used the file provided by /u/chunesI cast the input to a byte array, which holds the int value in ASCII for each character. I don't know of a quicker/better way to do it without searching.
Edit: Oh, I get it. I just need to output my console output, with the zeros and the header, to a file with a .pbm extension.
Here's the updated code that properly outputs to a PBM file. It also takes command line args for the name of the output.
I'm padding with spaces on both sides as well as a blank line on the top and bottom for readability.
Console output:
Image output
1
u/gfixler Jul 22 '14
Here's a Haskell solution. This is maybe my 4th, small program in the language, and feels very messy and involved. Critiques welcome.
1
u/NorrinxRadd Jul 22 '14
New to programming, here is my python 2.7. I was lazy about the font and downloaded the linked text file so I do load and use that. Supports multiple words.
1
u/marchelzo Jul 22 '14
My first C++ program that is more complex than Hello World (using /u/chunes' font.txt):
Feedback would be great. I would love to know an easier way to read from files. I was in gdb for quite a while trying to figure out why it wasn't parsing the font.txt file properly.
1
u/auxiliary-character Jul 22 '14
I feel like this is cheating.
Output
1
u/MJkram Jul 22 '14
Decided to torment myself with this in javascript by only using symbols to create the code.
It definitely made the challenge more interesting!
To output the code you need to call:
JSFiddle
1
u/nonextstop Jul 22 '14 edited Jul 22 '14
Java:
There are probably tons of better ways to do this, but it works.
1
u/tylerwal Jul 23 '14
PowerShell using the 'font.txt'
1
u/[deleted] Jul 23 '14 edited Jul 23 '14
First time posting, written in Objective-C. Utilizes /u/MaximaxII 's dict, modified for Objective-C. Any critique is welcome.
Output (resized): imgur
1
u/chaoticlychaotic Jul 23 '14
Solution in C# using the font file provided by /u/chunes. First C# thing I've written so there's probably unnecessary "using"s everywhere. Extensive comments. Woo.
Output is:
With no zeros:
1
u/stuartnelson3 Jul 23 '14
In Go:
1
u/atlasMuutaras Jul 24 '14 edited Jul 24 '14
My ugly-as-hell solution in python 2.7
1
u/13467 1 1 Jul 24 '14 edited Jul 26 '14
I made a tool Simon Tatham wrote called
dewinfontinto a Python 2.x module on gist -- it unpacks and reads Windows .FON files (likeC:/Windows/Fonts/sserife.fon) which contain bitmap fonts. Using this package, I wrote a simple text-to-PBM converter:Sample output:
1
u/dp_account Jul 27 '14 edited Jul 27 '14
Python 3.4
Output:
1
u/[deleted] Jul 28 '14
I used a 3x5 font I found here: http://robey.lag.net/2010/01/23/tiny-monospace-font.html - I left-padded the 15 bits to 16, then created a hex encoding for it. It doesn't actually write a pbm file, but it does spit the pixels/asterisks to stdout.
1
u/Banashark Jul 28 '14
I made my solution in C#/.Net electing to parse over the font file provided by /u/chunes as an exercise in parsing.
I also wrote an article going through the flow of thought I had while writing the program which actually helped me out a few times.
Let me know what you think!
http://banashek.com/blog/2014/07/28/challenge-slash-r-slash-dailyprogrammer-172-easy/
tl;dr finished program at the bottom of the file and github solution available at https://github.com/Banashek/ProgrammingChallenges/tree/master/Reddit%20Daily%20Programmer/172_Easy
1
u/shake_wit_dem_fries Jul 30 '14
Rust, with iterators EVERYWHERE. It's not pretty (specifically in the font->string portion), but it is flexible. Variable width characters are supported and unicode should work but doesn't for some reason.
Imgur
Used /u/chunes' font file with some additions.
1
u/jjj5311 Aug 07 '14
C#, includes the render tool
1
u/[deleted] Aug 17 '14 edited Aug 17 '14
Python 2.7
pretty new to python's i/o, so this was a fun exercise! i leaned pretty heavily on the structure of chunes' font.txt, so there's a lot of magic numbers in there. I'm pretty satisfied with the result, though.
1
u/Joris1225 Sep 03 '14
My solution in Java. Had loads of fun with this one. The Letters class is too messy to see the light of day, but it basically has a String for every letter(and space) of /u/chunes font without spaces or newlines. It then gets the right substring using the index and the width of the character.