LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-16-2020, 11:48 AM   #1
piobair
Member
 
Registered: Aug 2013
Distribution: Debian, Ubuntu
Posts: 222

Rep: Reputation: Disabled
Creating a .png file


I have a machine generated bitmapped image. Writing that image to an X-Windows screen is a lot more complicated than writing it to a *.png file. Or so I thought.

The attached program (teapots.c) generates an OpenGL image of teapots. Then, if the 'p' key is pressed, it calls subroutine "png.c" which writes that image to a file teapots.png . The <exc> key exits the program normally. (rename teapots.c.txt to teapots.c; rename png.c.txt to png.c)

The resulting file can be read by a common image reader; the reader does not hiccup over the CRC computation. However, that image is blank.

??

The program can be compiled by
gcc -o teapots teapots.c -lglut -lGL
Attached Files
File Type: txt teapots.c.txt (7.7 KB, 16 views)
File Type: txt png.c.txt (4.5 KB, 17 views)
 
Old 08-16-2020, 12:04 PM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 9,756

Rep: Reputation: 4408Reputation: 4408Reputation: 4408Reputation: 4408Reputation: 4408Reputation: 4408Reputation: 4408Reputation: 4408Reputation: 4408Reputation: 4408Reputation: 4408
I don't have an answer for you, but it looks like you started with this:

https://www.opengl.org/archives/reso...book/teapots.c

And then added the following as line 17:

Code:
#include "png.c"
Where png.c appears to be your own code.

Is that correct?

If this isn't meant to be an exercise in understanding the png format, then would you consider using a library like libpng or opencv do write to the png file?

Last edited by dugan; 08-16-2020 at 12:15 PM.
 
Old 08-16-2020, 12:21 PM   #3
hazel
Senior Member
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 4,637
Blog Entries: 12

Rep: Reputation: 2635Reputation: 2635Reputation: 2635Reputation: 2635Reputation: 2635Reputation: 2635Reputation: 2635Reputation: 2635Reputation: 2635Reputation: 2635Reputation: 2635
I can confirm the blank file. But your image is beautiful!

You could change the code of png.c so that it actually returns the number of characters written each time, then check the values.

PS: I did that and I got:
Code:
8 bytes written
25 bytes written
12 bytes written
Screenshot written to teapots.png
The numbers are the ssize_t returns from your write statements. That doesn't look to me anything like enough for such a detailed image, especially the middle section which I assume contains the actual visual data. Your code must be wrong.

Last edited by hazel; 08-16-2020 at 12:52 PM. Reason: Added results of test
 
Old 08-16-2020, 01:00 PM   #4
piobair
Member
 
Registered: Aug 2013
Distribution: Debian, Ubuntu
Posts: 222

Original Poster
Rep: Reputation: Disabled
The size of the output file teapots.png is exactly as expected.
I did a printout of line 300, just to ensure that it was not all zeros. By casual inspection, it looked fine.

Quote:
Originally Posted by hazel View Post
I can confirm the blank file. But your image is beautiful!

You could change the code of png.c so that it actually returns the number of characters written each time, then check the values.

PS: I did that and I got:
Code:
8 bytes written
25 bytes written
12 bytes written
Screenshot written to teapots.png
The numbers are the ssize_t returns from your write statements. That doesn't look to me anything like enough for such a detailed image, especially the middle section which I assume contains the actual visual data. Your code must be wrong.
 
Old 08-16-2020, 01:05 PM   #5
piobair
Member
 
Registered: Aug 2013
Distribution: Debian, Ubuntu
Posts: 222

Original Poster
Rep: Reputation: Disabled
That is correct. The disclaimer is at the head of teapots.c
This exercise is part of a much larger project, which will result in embedded code.

Quote:
Originally Posted by dugan View Post
I don't have an answer for you, but it looks like you started with this:

https://www.opengl.org/archives/reso...book/teapots.c

And then added the following as line 17:

Code:
#include "png.c"
Where png.c appears to be your own code.

Is that correct?

If this isn't meant to be an exercise in understanding the png format, then would you consider using a library like libpng or opencv do write to the png file?
 
Old 08-16-2020, 01:33 PM   #6
hazel
Senior Member
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 4,637
Blog Entries: 12

Rep: Reputation: 2635Reputation: 2635Reputation: 2635Reputation: 2635Reputation: 2635Reputation: 2635Reputation: 2635Reputation: 2635Reputation: 2635Reputation: 2635Reputation: 2635
Quote:
Originally Posted by piobair View Post
The size of the output file teapots.png is exactly as expected.
I did a printout of line 300, just to ensure that it was not all zeros. By casual inspection, it looked fine.
Oops! I missed out one looping write statement. Now there are 603 write ops reported: the three that I had before and the other 600 with 1512 bytes each. Does that feel right?

PS: If the correct data is going into the file, time to check the header section. I don't know how the png format actually works, but I assume the header tells libpng how much data there is and how to read it.

Last edited by hazel; 08-16-2020 at 01:54 PM. Reason: Added PS
 
Old 08-16-2020, 03:47 PM   #7
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 228Reputation: 228Reputation: 228
Quote:
Originally Posted by piobair View Post
the reader does not hiccup over the CRC computation.
How do you know that? The blank image could be result of the reader noticing incorrect checksum.

Code:
//    c = (unsigned long) ~0; For some reason, this does not equal 0xffffffffL
That’s most likely because you’re building this on 64-bit system where ‘unsigned long’ is 64-bit wide. You should be using ‘uint32_t’ et al if you care about the exact width of a variable (and you typically do when dealing with binary files).

Adding
Code:
	      printf("%02x %02x %02x", pixel[0], pixel[1], pixel[2]);
after ‘glReadPixels’ suggests that you are getting the correct data. I would suggest simplifying the problem and at first generating a two-colour 2×2 image with a chequerboard pattern. If that won’t work, the resulting file will be small enough that it will be easy to manually interpret it with ‘hexdump -C’.
 
1 members found this post helpful.
Old 08-16-2020, 04:21 PM   #8
piobair
Member
 
Registered: Aug 2013
Distribution: Debian, Ubuntu
Posts: 222

Original Poster
Rep: Reputation: Disabled
With an incorrect check sum, the viewer will respond that there is something wrong with the file.
As it is, Gwenview (or GIMP) says the image is 500 X 600 pixels.

A long int is 8 bytes. It doesn't matter (?) if it is a 32 bit machine or a 64 bit machine.
unsigned long c;
c = (unsigned long) ~0;
acts differently than
c = 0xffffffffL;

??

[QUOTE=mina86;6156613]How do you know that? The blank image could be result of the reader noticing incorrect checksum.

Last edited by piobair; 08-16-2020 at 04:26 PM.
 
Old 08-16-2020, 05:08 PM   #9
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 228Reputation: 228Reputation: 228
Quote:
Originally Posted by piobair View Post
A long int is 8 bytes. It doesn't matter (?) if it is a 32 bit machine or a 64 bit machine.
unsigned long c;
c = (unsigned long) ~0;
acts differently than
c = 0xffffffffL;
Because ~0 == -1 and when converted to unsigned long, that becomes ULONG_MAX which (on x86_64) is a 64-bit quantity with all bits set.
 
Old 08-16-2020, 11:20 PM   #10
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,075

Rep: Reputation: 1475Reputation: 1475Reputation: 1475Reputation: 1475Reputation: 1475Reputation: 1475Reputation: 1475Reputation: 1475Reputation: 1475Reputation: 1475
Also there is `sizeof(long)` to find it out. (Tipical values: Win16/Win32/Win64: 4; Unix32: 4; Unix64: 8)
 
  


Reply

Tags
png


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Webalizer: configure: error: png library not found... please install png. mikeshn Linux - Software 5 01-10-2005 02:28 PM
Where is trashcan_full/empty.png and kmenu.png??? d33pdream Linux - General 2 02-25-2003 11:13 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration