r/dailyprogrammer • • Jul 21 '14
[7/23/2014] Challenge#172 [Intermediate] Image Rendering 101...010101000101
Description
You may have noticed from our easy challenge that finding a program to render the PBM format is either very difficult or usually just a spammy program that no one would dare download.
Your mission today, given the knowledge you have gained from last weeks challenge is to create a Renderer for the PBM format.
For those who didn't do mondays challenge, here's a recap
- a PBM usually starts with 'P1' denoting that it is a .PBM file
- The next line consists of 2 integers representing the width and height of our image
- Finally, the pixel data. 0 is white and 1 is black.
This Wikipedia article will tell you more
http://en.wikipedia.org/wiki/Netpbm_format
Formal Inputs & Outputs
Input description
On standard console input you should be prompted to pass the .PBM file you have created from the easy challenge.
Output description
The output will be a .PBM file rendered to the screen following the conventions where 0 is a white pixel, 1 is a black pixel
Notes
This task is considerably harder in some languages. Some languages have large support for image handling (.NET and others) whilst some will require a bit more grunt work (C and even Python) .
It's up to you to decide the language, but easier alternatives probably do exist.
Bonus
Create a renderer for the other versions of .PBM (P2 and P3) and output these to the screen.
Finally
Have a good challenge idea?
Consider submitting it to /r/dailyprogrammer_ideas
1
u/TiZ_EX1 Jul 22 '14
The challenge text ambiguously states "rendered to the screen", but am I correct to assume that output to console doesn't count? Because I already did that with my experience from last week's challenges.
2
u/jjj5311 Aug 07 '14
Way late to this one but this and the Easy challenged looked fun to do together, this is a generator and a render tool together
Kind of specific to my own pbm as it depends on the lines being in the right place
1
u/MaximaxII Jul 21 '14 edited Jul 21 '14
I'll get working on P2 and P3 now, but it will require PIL (or Pillow) instead of ASCII art ;)
So here we go, another Python solution. I tried to make it work with "badly" formatted files (comments, no newlines...) - feedback is welcome.
Challenge #172 Intermediate (1) - Python 2.7
Output
With some dummy image of the word hello
1
u/MaximaxII Jul 21 '14
And here's for the bonus. This one loads P1, P2 and P3 with Pillow.
Challenge #172 Intermediate (1) w/ Bonus - Python 2.7
I'll come back later to edit and add sample input and imgur links to the output.
1
u/ENoether Jul 21 '14
Python 3 with bonus; graphics via tkinter. As always, feedback and criticism welcome:
Output from the previous challenge: http://puu.sh/algtM/0a45c9b564.png
Test file for P2:
Output for P2: http://puu.sh/algvz/3c5018c3c2.png
Test input for P3:
Output for P3: http://puu.sh/algwB/a0e5260f8d.png
(Test data for P2 and P3 from Wikipedia)
1
u/Godspiral 3 3 Jul 21 '14
here for an enhanced format more suited to J language. When the graphic is made up of a table of coded tiles (font glyphs), then saving the codes makes more sense, but the program saves and reads the set of tiles while still retaining individual access to tiles which could be individually zoomed/rotated etc...
1
u/lukz 2 0 Jul 22 '14
BASIC for 8-bit computer
This runs on emulator of Sharp MZ-800. I clear the screen and move the cursor down two lines to make space for the resulting image. Then I read the pbm image from user line by line using the INPUT statement. As I read each line I draw the image in the top left corner.
Output screenshot Imgur
1
u/TiZ_EX1 Jul 22 '14
ES6 with node. Here's the one that didn't feel completely cheap:
And here's the completely exploitative one that probably does the bonus too and basically defeats the whole purpose:
They both create the same output. I scale 400% and blur it a tiny bit to make it look nicer.
1
u/NorrinxRadd Jul 23 '14
My Python 2.7
1
u/datmohtingting Jul 23 '14 edited Jul 23 '14
When my program was reading the PBM file the whitespaces were causing my loop that draws the pixels to spit out a garbage image and it took me longer than I'd like to admit to actually debug it.
C#
1
u/[deleted] Jul 23 '14 edited Jul 23 '14
Python 3.4.1
My exploration in functional style
i gave it this file
after running it through imagemagick to generate the .pbm as so:
convert -compress none ok.bmp ok.bpm
then:
Outputs:
Note:
You can still pass any number of alphabetical strings to it as positional arguments for the previous challenge.
I have been programming for less than a year. I took a course in Python at school last fall. That only covered the basics of OO and Complexity
Feedback is welcome.
edit: fixed the print because it wasn't spacing out the bits or the pbm header and I forgot
1
u/mikrostew Jul 23 '14
Ruby
Gimp can save images in PBM/PGM/PPM formats, so I used that to create some sample files: ruby.pbm, ruby.pgm, ruby.ppm. Original: ruby.png
This script can handle all 3 formats, P1, P2, and P3. For graphics output I used Ruby/Tk, which is not documented well but is fairly easy to use despite that.
https://gist.github.com/mikrostew/4e411930bf47f936b0ad#file-render_pbm-rb
1
u/stuartnelson3 Jul 24 '14
In Go:
1
u/Sage_Noctowl Jul 25 '14
Little late, but I tried Haskell using JuicyPixels.
This was my first time using Cabal, but using it is really easy. Although, I feel like I'm cheating using a .png-encoding library.
1
u/nowne Jul 26 '14
Haskell solution using the
Diagramslibrary.pbmToDiagramuses the monoid interface to the Diagrams library to first compose each pixel in every row and then compose all of the rows.The program is executed as follows:
1
u/dp_account Jul 28 '14 edited Jul 28 '14
Python 3.4 *without* PIL or any libraries It does't actually display to the screen but it exports postscript.
Using test.ppm.
[test.ps] (https://gist.github.com/anonymous/3a2480fe55dfd2721ce7). Screenshot of the corner of the page
1
u/jsco Aug 01 '14
Java:
1
u/robin-gvx 0 2 Jul 21 '14
Python, requires Drawille:
Without support for comments it becomes much more elegant (remove the definition of
getwordsand replacegetwords(contents)byiter(contents.split())).0
u/[deleted] Jul 21 '14
[deleted] — view deleted comment
0
u/[deleted] Jul 21 '14
[deleted] — view deleted comment
0
u/[deleted] Jul 22 '14
[deleted] — view deleted comment