Computer Science Canada Insectoid's Summer Programming Challenge |
Author: | Insectoid [ Tue Apr 19, 2011 5:28 pm ] |
Post subject: | Insectoid's Summer Programming Challenge |
So, I was watching a first-year Harvard lecture on C programming the other day. One of the assignments given to the students involved writing a program that takes a bmp file and a number n as input, and outputs the image multiplied in area by n to a bmp file. It was then that I realized that I had never done I/O on anything but text files. I didn't have a clue how other file types worked. My challenge for compsci.ca users (primarily students, but other people to, if you want) this summer is to write a program that does something with a common file format. A bitmap is a good place to start, as it's a fairly basic format. Add a watermark, change the colors, be creative! After that, move into more involved formats such as ZIP files or other compression formats, MS Word documents, etc. Maybe even design your own format for something. This isn't a contest. It's a challenge. Everybody wins, unless you don't learn anything. Then only everyone who learned something wins. ~Insectoid |
Author: | Raknarg [ Tue Apr 19, 2011 5:59 pm ] |
Post subject: | RE:Insectoid\'s Summer Programming Challenge |
Is turing legal? |
Author: | Tony [ Tue Apr 19, 2011 6:05 pm ] |
Post subject: | RE:Insectoid\'s Summer Programming Challenge |
As a file format? |
Author: | Insectoid [ Tue Apr 19, 2011 6:19 pm ] |
Post subject: | RE:Insectoid\'s Summer Programming Challenge |
Use whatever language you want. The objective is that it isn't a text format- this includes .txt, .t, .cpp, .rb, etc. |
Author: | Dratino [ Wed Apr 20, 2011 8:55 pm ] |
Post subject: | RE:Insectoid\'s Summer Programming Challenge |
I've worked with Microsoft BMP files before, but only to generate visual versions of certain types of output. |
Author: | SNIPERDUDE [ Thu Apr 21, 2011 12:09 pm ] |
Post subject: | RE:Insectoid\'s Summer Programming Challenge |
Sounds fun, I might take a stab at it. I haven't worked with anything other than text files as well (besides my own encrypted "text" files for program saves). |
Author: | RandomLetters [ Thu Apr 21, 2011 9:50 pm ] |
Post subject: | RE:Insectoid\'s Summer Programming Challenge |
I'm trying to write the header input for a bmp file editor right now in java. How can one convert a byte array into an integer? (stupid, stupid signed integers >>) |
Author: | DemonWasp [ Thu Apr 21, 2011 10:28 pm ] |
Post subject: | RE:Insectoid\'s Summer Programming Challenge |
Err... long i = b[3] << 24 | b[2] << 16 | b[1] << 8 | b[0] You may have to fiddle with the indicies, depending on whether the source is big or little endian and whether or not I've screwed them up. Alternately, DataInputStream, which you can extend to fill in the missing unsigned-int, unsigned-long logic (why they didn't include those is beyond me). |
Author: | btiffin [ Thu Apr 21, 2011 11:14 pm ] | ||
Post subject: | Re: Insectoid's Summer Programming Challenge | ||
Do me a favour;
learn some of this. If you ever want to work at a bank, insurance company or government agency, understanding the above code can get you a great big resume++ Cheers |
Author: | RandomLetters [ Fri Apr 22, 2011 1:13 pm ] |
Post subject: | Re: RE:Insectoid\'s Summer Programming Challenge |
DemonWasp @ Thu Apr 21, 2011 10:28 pm wrote: Err...
long i = b[3] << 24 | b[2] << 16 | b[1] << 8 | b[0] You may have to fiddle with the indicies, depending on whether the source is big or little endian and whether or not I've screwed them up. Alternately, DataInputStream, which you can extend to fill in the missing unsigned-int, unsigned-long logic (why they didn't include those is beyond me). Ah, I got it, thanks! All the bytes were in reverse order |
Author: | DemonWasp [ Fri Apr 22, 2011 1:43 pm ] |
Post subject: | Re: Insectoid's Summer Programming Challenge |
btiffin @ Thu Apr 21, 2011 11:14 pm wrote: learn some of this. If you ever want to work at a bank, insurance company or government agency, understanding the above code can get you a great big resume++
I suppose it's a good thing I never want to work for a bank, insurance company, or government agency, because that code is terrifyingly ugly. Not that these organizations generally do any better in any other language. |
Author: | DtY [ Fri Apr 22, 2011 7:44 pm ] |
Post subject: | Re: RE:Insectoid\'s Summer Programming Challenge |
RandomLetters @ Fri Apr 22, 2011 1:13 pm wrote: Ah, I got it, thanks!
That's the endianess, this great computer science concept in which you can put the bytes in any order because it doesn't matter to the computer. You never put the individual bits in the byte backward though.
All the bytes were in reverse order I might be missing something, but is there any useful reason to use little endian? |
Author: | ultimatebuster [ Sat Apr 23, 2011 11:40 am ] |
Post subject: | RE:Insectoid\'s Summer Programming Challenge |
isn't this just non-text binary formats? |
Author: | Insectoid [ Sat Apr 23, 2011 11:52 am ] |
Post subject: | RE:Insectoid\'s Summer Programming Challenge |
Pretty much. But you have to deal with things you usually don't with text files. Most languages have advanced functions for dealing with text (fscanf & getchar in C, for example). With non-text formats you're limited to raw byte reading and writing* (fread and fwrite in C), and all the formatting has to be done yourself. Also, doing this forces you to learn about how files are stored on a drive, which isn't really taught in school until 2nd year uni or higher (at least 2nd year- I still haven't been taught it). *Yes, I know libraries exist to read popular formats, but that would defeat the purpose of this challenge. |
Author: | ultimatebuster [ Sat Apr 23, 2011 1:17 pm ] | ||
Post subject: | RE:Insectoid\'s Summer Programming Challenge | ||
I'm not sure if Python can write non-text format via file.write nm it can. though i think i has to be direct HEX code or via struct. Following writes a text file in binary directly.
|
Author: | DtY [ Sat Apr 23, 2011 5:23 pm ] | ||
Post subject: | Re: RE:Insectoid\'s Summer Programming Challenge | ||
ultimatebuster @ Sat Apr 23, 2011 1:17 pm wrote: I'm not sure if Python can write non-text format via file.write
You can also use ord() to convert a single character to it's ordinal code (the character's numeric value) and chr() to go back.nm it can. though i think i has to be direct HEX code or via struct. Following writes a text file in binary directly.
|
Author: | RandomLetters [ Sat Apr 23, 2011 7:30 pm ] | ||
Post subject: | Re: Insectoid's Summer Programming Challenge | ||
Well, finished the program to load a 24 bit bitmap, and apply a tunnel effect, like that terrible, terrible feature in ipad photobooth It's still extremely slow to something like MS paint however, which most of the time is taken because of loading the bitmap into an array.
Is there any way to optimize this? Currently, I have a 2D array of pixels, which are themselves an array of 3 bytes. |
Author: | DemonWasp [ Sun Apr 24, 2011 10:28 am ] | ||
Post subject: | RE:Insectoid\'s Summer Programming Challenge | ||
BufferedInputStream:
Also, don't forget to close your input streams when you're done with them. |
Author: | DtY [ Sun Apr 24, 2011 12:07 pm ] | ||
Post subject: | Re: Insectoid's Summer Programming Challenge | ||
RandomLetters @ Sat Apr 23, 2011 7:30 pm wrote: Well, finished the program to load a 24 bit bitmap, and apply a tunnel effect, like that terrible, terrible feature in ipad photobooth
There's a lot of overhead in using an array for each pixel. Instead, make each pixel a single integer, made up of red<<16+green<<8+blue. You'll have to use some weird bitmasking stuff to get the individual components, but it will be a lot more space efficient. It may seem odd at first that storing 24 bits in 32 bits is more space efficient, but to creating an array requires creating a pointer, which is itself 32 bits, and then 8 bits for each index in the array.It's still extremely slow to something like MS paint however, which most of the time is taken because of loading the bitmap into an array.
Is there any way to optimize this? Currently, I have a 2D array of pixels, which are themselves an array of 3 bytes. |
Author: | RandomLetters [ Sat Apr 30, 2011 9:01 pm ] | ||
Post subject: | RE:Insectoid\'s Summer Programming Challenge | ||
Thank's for the advice. It's almost instantaneous now. Here's the mostly complete product. Bit operations are surprisingly useful.
|
Author: | Brightguy [ Mon May 02, 2011 2:47 am ] |
Post subject: | Re: RE:Insectoid\'s Summer Programming Challenge |
DtY @ Sat Apr 23, 2011 5:23 pm wrote: You can also use ord() to convert a single character to it's ordinal code (the character's numeric value) and chr() to go back.
In high school the first time I worked with a binary format I opened the file in text mode, translated the characters to their ordinal codes, made the requried modifications, translated the ordinal codes back to characters and outputted the file in text mode. Heh. |
Author: | ultimatebuster [ Mon May 02, 2011 9:40 am ] |
Post subject: | Re: RE:Insectoid\'s Summer Programming Challenge |
Brightguy @ Mon May 02, 2011 2:47 am wrote: DtY @ Sat Apr 23, 2011 5:23 pm wrote: You can also use ord() to convert a single character to it's ordinal code (the character's numeric value) and chr() to go back.
In high school the first time I worked with a binary format I opened the file in text mode, translated the characters to their ordinal codes, made the requried modifications, translated the ordinal codes back to characters and outputted the file in text mode. Heh. Now that's how you do it. Rebel. |
Author: | Velocity [ Thu Jan 12, 2012 12:29 am ] |
Post subject: | RE:Insectoid\'s Summer Programming Challenge |
what is the difference between binary and trinary? |
Author: | Insectoid [ Thu Jan 12, 2012 7:48 am ] |
Post subject: | RE:Insectoid\'s Summer Programming Challenge |
What's the difference between a bicycle and a tricycle? |
Author: | mirhagk [ Thu Jan 12, 2012 10:49 am ] |
Post subject: | RE:Insectoid\'s Summer Programming Challenge |
1001010101 1210120120 |