Live data from Hacker News

Understanding and writing a JPEG decoder in Python

yasoob.me

11–20 of 48 posts

Re: Understanding and writing a JPEG decoder in Python

#11
post #8
post #2

Hi everyone! OP here. Why write another article on JPEG when there are already hundreds of articles on the internet? Well, normally when you read articles on JPEG, the author just gives you details about what the format looks like. You don’t implement any code to do the actual decompression and decoding. Even if you do write code, it is in C/C++ and not accessible to a wide group of people. I tried to change that thr…

> Even if you do write code, it is in C/C++ and not accessible to a wide group of people. To be honest, writing a jpeg decoder in C seems easier and more natural than doing it in python.

If you’re trying to describe a file format or algorithm in the most accessible way, wouldn’t pseudocode be the best thing to use?

If that’s not an option (because you want the code to be executable), I think Python is the closest popular language to pseudocode.

Re: Understanding and writing a JPEG decoder in Python

#12
post #2

Hi everyone! OP here. Why write another article on JPEG when there are already hundreds of articles on the internet? Well, normally when you read articles on JPEG, the author just gives you details about what the format looks like. You don’t implement any code to do the actual decompression and decoding. Even if you do write code, it is in C/C++ and not accessible to a wide group of people. I tried to change that thr…

It's great to see more people complete the "JPEG decoder challenge" --- and document it too. Have you read the official T.81 spec[1]? It is one of the easier standards to read, and it has flowcharts of all the algorithms which mean you can implement without really understanding the theory.

As you mentioned there are lots of other articles about writing JPEG decoders, as well as for the other popular image formats GIF and PNG. What I haven't seen much of if at all, however, are articles about video decoding; if you're interested to try another challenge, I recommend a decoder for H.261: https://www.itu.int/rec/T-REC-H.261-199303-I/en

(I've written one --- in C --- and it is actually far simpler than JPEG in many ways, although it shares many similarities. A H.261 decoder in Python, with associated article, would definitely be very interesting to see, and AFAIK also a world-first!)

[1] https://www.w3.org/Graphics/JPEG/itu-t81.pdf

Re: Understanding and writing a JPEG decoder in Python

#13
post #2

Hi everyone! OP here. Why write another article on JPEG when there are already hundreds of articles on the internet? Well, normally when you read articles on JPEG, the author just gives you details about what the format looks like. You don’t implement any code to do the actual decompression and decoding. Even if you do write code, it is in C/C++ and not accessible to a wide group of people. I tried to change that thr…

It's great to see more people complete the "JPEG decoder challenge" --- and document it too. Have you read the official T.81 spec[1]? It is one of the easier standards to read, and it has flowcharts of all the algorithms which mean you can implement without really understanding the theory. As you mentioned there are lots of other articles about writing JPEG decoders, as well as for the other popular image formats GIF…

Thanks for the H.261 decoder suggestion! I will actually seriously consider it because I am personally curious how video decoders work as well. I will make sure to document the process if I end up working on it.

Re: Understanding and writing a JPEG decoder in Python

#14
post #11
post #8

Earlier quoted context omitted.

> Even if you do write code, it is in C/C++ and not accessible to a wide group of people. To be honest, writing a jpeg decoder in C seems easier and more natural than doing it in python.

If you’re trying to describe a file format or algorithm in the most accessible way, wouldn’t pseudocode be the best thing to use? If that’s not an option (because you want the code to be executable), I think Python is the closest popular language to pseudocode.

I think being executable is really key, because one way I check my understanding of code is to make modifications to the code and see if they do what I expect them to do.

Re: Understanding and writing a JPEG decoder in Python

#15
post #2

Hi everyone! OP here. Why write another article on JPEG when there are already hundreds of articles on the internet? Well, normally when you read articles on JPEG, the author just gives you details about what the format looks like. You don’t implement any code to do the actual decompression and decoding. Even if you do write code, it is in C/C++ and not accessible to a wide group of people. I tried to change that thr…

This. I know C, C++ implementations are efficient but to see python implementation of this makes it lot easier to understand.

> You don’t implement any code to do the actual decompression and decoding

This is also true in case of face recognition. Nobody tells you internals and just tells you to use tools like OpenCV. Maybe it's too complicated but a post like OP's for face recognition would be a treat!

Re: Understanding and writing a JPEG decoder in Python

#16
post #11

Earlier quoted context omitted.

If you’re trying to describe a file format or algorithm in the most accessible way, wouldn’t pseudocode be the best thing to use? If that’s not an option (because you want the code to be executable), I think Python is the closest popular language to pseudocode.

I think being executable is really key, because one way I check my understanding of code is to make modifications to the code and see if they do what I expect them to do.

IMO the ideal setup would be for an article to contain pseudocode and to have supplementary executable code. This would allow the article to explain concepts without boilerplate and unnecessary details, but also ensure that those details are available for readers who wish to investigate further.

I still think it's an advantage to write the executable code as close to pseudocode as possible.

There is further discussion of pseudocode vs executable code at https://academia.stackexchange.com/q/140986.

Re: Understanding and writing a JPEG decoder in Python

#18
post #13

Earlier quoted context omitted.

It's great to see more people complete the "JPEG decoder challenge" --- and document it too. Have you read the official T.81 spec[1]? It is one of the easier standards to read, and it has flowcharts of all the algorithms which mean you can implement without really understanding the theory. As you mentioned there are lots of other articles about writing JPEG decoders, as well as for the other popular image formats GIF…

Thanks for the H.261 decoder suggestion! I will actually seriously consider it because I am personally curious how video decoders work as well. I will make sure to document the process if I end up working on it.

If you understand JPEG then you already understand the important bits of MPEG. It's also fun to study scaling and colorspaces. See https://justine.storage.googleapis.com/printimage.html and https://justine.storage.googleapis.com/printvideo.html

Re: Understanding and writing a JPEG decoder in Python

#20
post #8

Earlier quoted context omitted.

> Even if you do write code, it is in C/C++ and not accessible to a wide group of people. To be honest, writing a jpeg decoder in C seems easier and more natural than doing it in python.

As someone who has done it in C, I would both agree and disagree --- reading the file format will definitely be easier since C lets you pick bits/bytes/words/etc. off the stream directly, but on the other hand the high-level structures (looping, etc.) would probably be easier with Python. Overall, seeing as OP's Python implementation is less than 300 LoC while my C implementation was closer to 750, the Python might b…

Python also lets you read bytes off the stream directly.
Post reply on HN