LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 09-03-2012, 01:09 AM   #1
Skaperen
Senior Member
 
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,684
Blog Entries: 31

Rep: Reputation: 176Reputation: 176
function to read image files in C


I'm thinking about a program I want to write which will need to read image files of major formats into an array of pixels, as well as info like geometry, bit depth, and gamma value. It needs to preserve as much of the bit depth as is present in the image (e.g. for a 16-bit per color image, I'd need at least 16 bits per pixel). If the format is a lossless one, of course I need perfect pixels.

All the libraries I see out there so far as way overly complex. For specific libraries for specific formats, I would need to add their complexity together if I am to use them. I also looked for something generic. The one I found (MagickWand) was also way overly complex.

I'm looking for something simple. Does it exist? Before I have to dive into the complex mess, I want to exhaust the search for simpler solutions to avoid gumming up my code with a lot of library overhead calls.

Sure, a little bit of overhead is needed in my case. I need to find out the geometry and the bit/byte depth so I can set up an array of pixels. if there are gamma variations, I need to know that, too. Then I just want to read the whole thing in. If it can covert to linear, that's a plus. I'd be happy with it providing 32 bits per color per pixel if it can convert to that.

Any good choices out there? Or do I have to dive into the complex mess (looks so far like MagickWand is it).

The following formats (all variations of these that commonly exist) would be a minimum: bmp, gif, jpeg, png, pnm/ppm, tiff
 
Old 09-03-2012, 02:07 AM   #2
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

Take a look at libimlib2. It looks very versatile yet lightweight. I successfully run example code from the documentation distributed with the library. The feh image viewer uses this library, so using it you can easily test whether required file formats are supported or not.

There is another possibility, opencv, but I don't seem to be able to run example program on ubuntu and it is probably an overkill for your purposes anyway. My previous experience with python-opencv was very positive though.

Last edited by firstfire; 09-03-2012 at 02:21 AM.
 
Old 09-03-2012, 07:58 AM   #3
Skaperen
Senior Member
 
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,684

Original Poster
Blog Entries: 31

Rep: Reputation: 176Reputation: 176
It looks like Imlib (package libimlib2 on Ubuntu) has dependencies on X libraries. There's a requirement I had not thought of. Since my anticipated program needs to run in a server context, this could create some problems. It appears Imlib is intended as a tool for X Windows applications displaying imagery.

I see it does have a bunch of files in the Ubuntu package of type .so that are in a subdirectory called "loaders". It seems there may be some minimally implemented source I could borrow and make a smaller library from, or just toss into my own program. I'll have to check up on the source.
 
Old 09-03-2012, 12:54 PM   #4
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

Actually the package libimlib2 does not have X dependencies:
Code:
$ apt-cache show libimlib2 | grep -i depend
Depends: libbz2-1.0, libc6 (>= 2.11), libfreetype6 (>= 2.2.1), libgif4 (>= 4.1.4), libid3tag0 (>= 0.15.1b), libjpeg8 (>= 8c), libpng12-0 (>= 1.2.13-4), libtiff4, libx11-6, libxext6, zlib1g (>= 1:1.1.4)
(don't count libxext which does not have further X dependencies) but libimlib2-dev does:
Code:
$ apt-cache show libimlib2-dev | grep -i depend
Depends: libimlib2 (= 1.4.4-1build1), libc6-dev, libjpeg-dev, libtiff-dev, libpng12-dev, zlib1g-dev, libgif-dev, libx11-dev, libxext-dev, libfreetype6-dev, libltdl3-dev
Moreover, I found the following lines in Imlib2.h (from -dev package):
Code:
# ifndef X_DISPLAY_MISSING
#  include <X11/Xlib.h>
# endif
So, my conclusion is that Imlib2 may be used without any X libraries installed. To do that you'll need to install libimlib2 on the server, get Imlib2.h header file and use it as follows:
Code:
#define X_DISPLAY_MISSING
#include "Imlib2.h"
You will probably also need to manually create a symbolic link, e.g.
Code:
ln -s /usr/lib/libImlib2.so.1 libImlib2.so
(either in current directory or in /usr/lib)

I have successfully done this on my X-less laptop attached to my 3D printer (a kind of server). At least test program, which converts between different image formats, works.

I suppose there is a way to compile Imlib2 with X_DISPLAY_MISSING defined and install resulting library on a server, but you'll need to install lots of other libraries anyway, so this is actually even harder way to setup the library.

Last edited by firstfire; 09-03-2012 at 01:04 PM.
 
Old 09-03-2012, 02:45 PM   #5
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 647

Rep: Reputation: 189Reputation: 189
FreeImage is a framework built upon those "specific libraries for specific formats" and provides a unified interface for image manipulations.
Give it a try.
 
Old 09-04-2012, 12:04 AM   #6
Skaperen
Senior Member
 
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,684

Original Poster
Blog Entries: 31

Rep: Reputation: 176Reputation: 176
Quote:
Originally Posted by firstfire View Post
Hi.

Actually the package libimlib2 does not have X dependencies:
Code:
$ apt-cache show libimlib2 | grep -i depend
Depends: libbz2-1.0, libc6 (>= 2.11), libfreetype6 (>= 2.2.1), libgif4 (>= 4.1.4), libid3tag0 (>= 0.15.1b), libjpeg8 (>= 8c), libpng12-0 (>= 1.2.13-4), libtiff4, libx11-6, libxext6, zlib1g (>= 1:1.1.4)
(don't count libxext which does not have further X dependencies) but libimlib2-dev does:
Code:
$ apt-cache show libimlib2-dev | grep -i depend
Depends: libimlib2 (= 1.4.4-1build1), libc6-dev, libjpeg-dev, libtiff-dev, libpng12-dev, zlib1g-dev, libgif-dev, libx11-dev, libxext-dev, libfreetype6-dev, libltdl3-dev
How about if I do count libx11-6 ?

On my desktop it is linked to some X libs:
Code:
lorentz/root /root 92# ldd /usr/lib/libImlib2.so.1.4.2
	linux-vdso.so.1 =>  (0x00007fff5e5ae000)
	libfreetype.so.6 => /usr/lib/libfreetype.so.6 (0x00007f2c34bb1000)
	libz.so.1 => /lib/libz.so.1 (0x00007f2c34999000)
	libX11.so.6 => /usr/lib/libX11.so.6 (0x00007f2c34662000) (definitely X)
	libXext.so.6 => /usr/lib/libXext.so.6 (0x00007f2c34450000) (OK you said not to count this)
	libdl.so.2 => /lib/libdl.so.2 (0x00007f2c3424c000)
	libm.so.6 => /lib/libm.so.6 (0x00007f2c33fc8000)
	libc.so.6 => /lib/libc.so.6 (0x00007f2c33c45000)
	libxcb.so.1 => /usr/lib/libxcb.so.1 (0x00007f2c33a28000) (not sure of this)
	/lib64/ld-linux-x86-64.so.2 (0x00007f2c350c2000)
	libXau.so.6 => /usr/lib/libXau.so.6 (0x00007f2c33824000) (I remember this being some X)
	libXdmcp.so.6 => /usr/lib/libXdmcp.so.6 (0x00007f2c3361e000) (and this)
lorentz/root /root 93#
I get this:
Code:
server-46-67/root/x0 /root 1# apt-cache show libimlib2-dev | grep -i depend
Depends: libimlib2 (= 1.4.4-1build1), libc6-dev, libjpeg-dev, libtiff-dev, libpng12-dev, zlib1g-dev, libgif-dev, libx11-dev, libxext-dev, libfreetype6-dev, libltdl3-dev
server-46-67/root/x0 /root 2#
When I try to install on a server instance:
Code:
server-46-67/root/x0 /root 2# apt-get install libimlib2
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following extra packages will be installed:
  libgif4 libid3tag0 libx11-6 libxau6 libxcb1 libxdmcp6 libxext6
The following NEW packages will be installed:
  libgif4 libid3tag0 libimlib2 libx11-6 libxau6 libxcb1 libxdmcp6 libxext6
0 upgraded, 8 newly installed, 0 to remove and 18 not upgraded.
Need to get 892 kB/1,140 kB of archives.
After this operation, 2,827 kB of additional disk space will be used.
Do you want to continue [Y/n]?
I suppose maybe it might need X libs for some image formats. But I really rather have something that focuses on image processing, instead of treating it as an accessory for an X application.
 
Old 09-04-2012, 12:11 AM   #7
Skaperen
Senior Member
 
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,684

Original Poster
Blog Entries: 31

Rep: Reputation: 176Reputation: 176
Quote:
Originally Posted by 414N View Post
FreeImage is a framework built upon those "specific libraries for specific formats" and provides a unified interface for image manipulations.
Give it a try.
This looks like what I might need. I will check it out.
Code:
server-46-67/root/x0 /root 12# apt-get install libfreeimage3
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following extra packages will be installed:
  libilmbase6 liblcms1 libmng1 libopenexr6 libopenjpeg2 libraw5
Suggested packages:
  liblcms-utils
The following NEW packages will be installed:
  libfreeimage3 libilmbase6 liblcms1 libmng1 libopenexr6 libopenjpeg2 libraw5
0 upgraded, 7 newly installed, 0 to remove and 18 not upgraded.
Need to get 1,911 kB of archives.
After this operation, 5,906 kB of additional disk space will be used.
Do you want to continue [Y/n]?
Seems to not depend on too much, at least for Ubuntu. I will have to check it on Slackware later.
 
Old 09-04-2012, 01:50 AM   #8
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Quote:
Originally Posted by Skaperen View Post
How about if I do count libx11-6 ?
Ooops, I missed that, sorry!
 
Old 09-04-2012, 01:58 AM   #9
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 647

Rep: Reputation: 189Reputation: 189
Quote:
Originally Posted by Skaperen View Post
Seems to not depend on too much, at least for Ubuntu. I will have to check it on Slackware later.
The source archive of FreeImage contains the sources of the other image libs (libpng, libOpenEXR etc.) so it's self-contained.
You can find a SlackBuild for it on SBo. BTW, I'm the current maintainer of the script
 
Old 09-04-2012, 08:46 AM   #10
Skaperen
Senior Member
 
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,684

Original Poster
Blog Entries: 31

Rep: Reputation: 176Reputation: 176
It looks like FreeImage won't bring in all the bits for 16 bits per color images (what I get when I convert my camera raw images that have 14 bits per color over to PNG). It intentionally divides the 16-bit values by 256. It doesn't say why it divides, but we know that means all you get at most is 8 bits per color. While the images will end up being displayed at 8 bits per color, more bits are needed to avoid quantizing artifacts in the calculations I'll be doing, which involve removing the gamma correction, doing maths on linear values, then re-correcting for the gamma effect (because even new displays expect gamma corrected values by emulating the gamma error of old analog phosphor CRT displays).

FYI, this program idea I have is not limited to operating on my own camera images. That's why I need multi-format import capability (though I doubt GIF would be expected to be imported with any decent quality, since true-color-GIF never caught on, and wasn't very efficient).

Last edited by Skaperen; 09-04-2012 at 08:49 AM.
 
Old 09-04-2012, 09:37 AM   #11
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 647

Rep: Reputation: 189Reputation: 189
I just found on "teh interwebz" a 48bpp TIFF image to perform some tests (for anyone else interested, you can find it on this page).
Although I think FreeImage should handle that color depth, you could also try using OpenImageIO. It has good documentation (even in PDF format) available.

Last edited by 414N; 09-04-2012 at 09:40 AM.
 
  


Reply



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
Read Write access to a iso9660 filesystem..mount a .iso image as read write ceazar123 Linux - Newbie 16 09-01-2010 09:07 AM
Read Write access to a iso9660 filesystem..mount a .iso image as read write ceazar123 Linux - General 2 08-26-2010 03:32 PM
can't read from the device driver using read() function. parthi_s Linux - Newbie 1 03-23-2009 06:16 AM
Help with the read() function in C smoothdogg00 Programming 4 09-25-2006 08:44 PM
how to read iso image files david.des@veriz Linux - Software 3 06-10-2002 10:13 PM

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

All times are GMT -5. The time now is 06:03 PM.

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