Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #96650

Re: PNG coding

From Richard Heathfield <rjh@cpax.org.uk>
Newsgroups comp.lang.c
Subject Re: PNG coding
Date 2016-11-15 21:45 +0000
Organization Fix this later
Message-ID <o0fvki$c8n$1@dont-email.me> (permalink)
References <o0foak$4cu$1@reader2.panix.com> <efb414a2-37fd-f415-1353-d92dad04592b@verizon.net>

Show all headers | View raw


On 15/11/16 20:34, James R. Kuyper wrote:
> On 11/15/2016 02:40 PM, ruben safir wrote:
>> Does anyone know of any sample code for reading PNG files that doesn't
>> include using libpng.... just straight direct access.
>
> How about looking at libpng itself? It is the official reference
> implementation for PNG, and the source code is available under a
> permissive free software license. I'd think you'd learn at least as much
> by looking at the code for the official reference implementation as
> you'd learn by looking at any other code.

Just to encourage people to take James up on his suggestion, here are 
some statistics from the libpng code.

Firstly, there'd better be LOTS of it. We want to learn a lot, don't we?

C files: 17
H files: 6

That looks promising, but not too impressive. Maybe we'd better count 
lines instead:

Lines of C: 33593
Lines of H: 6791

Ah, that's better! But how much does it all *weigh*?

Kilobytes of C: 1116
Kilobytes of H: 304

Ah, that's very reassuring. Over a megabyte of C code. Let's remind 
ourselves what we're doing: reading a PNG into memory, or writing it 
out. Okay, a megabyte (of *source*) seems perfectly reasonable for that.

Now, we all know that there's nothing like the pre-processor for making 
code readable, so let's hope they use the pre-processor a *lot*:

Number of lines of H starting #: 1667
Number of lines of C starting #: 2223

Yep, excellent.

Disappointingly, they supply a file, example.c, that shows you how to 
use the library. You'd have thought you'd learn more from reading the 
whole of the library code line by line in order to understand how to 
call it.

But they redeem themselves in a most unexpected manner --- example.c 
/does not compile/! And it's not supposed to. There's a comment to that 
effect:

"This file does not currently compile, because it is missing certain 
parts, like allocating memory to hold an image.  You will have to supply 
these parts to get it to compile."

They refer the interested reader, instead, to a 2000+ line test program, 
which I presume was originally developed as an IOCCC entry. The main() 
function calls a function named test_one_file(), which sounds quite a 
promising place to start. Let's take a look at it:

/* Test one file */
static int
test_one_file(PNG_CONST char *inname, PNG_CONST char *outname)
{
    static png_FILE_p fpin;
    static png_FILE_p fpout;  /* "static" prevents setjmp corruption */
    pngtest_error_parameters error_parameters;
    png_structp read_ptr;
    png_infop read_info_ptr, end_info_ptr;
#ifdef PNG_WRITE_SUPPORTED
    png_structp write_ptr;
    png_infop write_info_ptr;
    png_infop write_end_info_ptr;
#ifdef PNG_WRITE_FILTER_SUPPORTED
    int interlace_preserved = 1;
#endif /* WRITE_FILTER */
#else /* !WRITE */
    png_structp write_ptr = NULL;


Actually, since it's about 900 lines long, I don't think I'll show any 
more of it. It's obviously all quite self-explanatory, and you won't 
need any help at all in understanding it completely. Five minutes should 
do it. Maybe ten if you're having a bad day. For the whole library? 
Perhaps an hour. After all, it's only 40,000 lines.

-- 
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

PNG coding ruben safir <ruben@mrbrklyn.com> - 2016-11-15 14:40 -0500
  Re: PNG coding "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2016-11-15 12:02 -0800
    Re: PNG coding ruben safir <ruben@mrbrklyn.com> - 2016-11-15 15:20 -0500
      Re: PNG coding "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2016-11-15 12:25 -0800
  Re: PNG coding "James R. Kuyper" <jameskuyper@verizon.net> - 2016-11-15 15:34 -0500
    Re: PNG coding Popping mad <rainbow@colition.gov> - 2016-11-15 20:57 +0000
      Re: PNG coding "James R. Kuyper" <jameskuyper@verizon.net> - 2016-11-15 16:12 -0500
      Re: PNG coding jadill33@gmail.com - 2016-11-15 14:00 -0800
        Re: PNG coding Jorgen Grahn <grahn+nntp@snipabacken.se> - 2016-11-20 11:14 +0000
    Re: PNG coding Richard Heathfield <rjh@cpax.org.uk> - 2016-11-15 21:45 +0000
      Re: PNG coding "James R. Kuyper" <jameskuyper@verizon.net> - 2016-11-15 17:15 -0500
        Re: PNG coding Richard Heathfield <rjh@cpax.org.uk> - 2016-11-15 22:41 +0000
          Re: PNG coding Melzzzzz <mel@zzzzz.com> - 2016-11-15 23:49 +0100
          Re: PNG coding Anton Shepelev <anton.txt@g{oogle}mail.com> - 2016-11-16 15:23 +0300
            Re: PNG coding Richard Heathfield <rjh@cpax.org.uk> - 2016-11-16 13:57 +0000
              Re: PNG coding BGB <cr88192@hotmail.com> - 2016-11-18 01:24 -0600
                Re: PNG coding BartC <bc@freeuk.com> - 2016-11-18 11:56 +0000
                Re: PNG coding BGB <cr88192@hotmail.com> - 2016-11-18 12:48 -0600
                Re: PNG coding BartC <bc@freeuk.com> - 2016-11-19 00:21 +0000
                Re: PNG coding BGB <cr88192@hotmail.com> - 2016-11-19 01:50 -0600
        Re: PNG coding Melzzzzz <mel@zzzzz.com> - 2016-11-15 23:42 +0100
        Re: PNG coding BGB <cr88192@hotmail.com> - 2016-11-15 17:53 -0600
          Re: PNG coding "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2016-11-15 16:25 -0800
        Re: PNG coding raltbos@xs4all.nl (Richard Bos) - 2016-11-18 08:15 +0000
          Re: PNG coding jameskuyper@verizon.net - 2016-11-18 09:35 -0800
          Re: PNG coding supercat@casperkitty.com - 2016-11-20 14:34 -0800
      Re: PNG coding BartC <bc@freeuk.com> - 2016-11-16 19:38 +0000
        Re: PNG coding Richard Heathfield <rjh@cpax.org.uk> - 2016-11-16 22:16 +0000
  Re: PNG coding "Mr. Man-wai Chang" <toylet.toylet@gmail.com> - 2016-11-16 21:14 +0800
  Re: PNG coding Jean-marc Lienher <jean-marc.lienher@bluewin.ch> - 2016-11-16 20:55 +0100
    Re: PNG coding BartC <bc@freeuk.com> - 2016-11-16 20:31 +0000
    Re: PNG coding Richard Heathfield <rjh@cpax.org.uk> - 2016-11-16 22:24 +0000

csiph-web