Code version for read PNG and GIF images

void ddImageReader( const string & name, UInt32 *width, UInt32 *height)
{ 
        // OPEN THE FILE
        unsigned char header[25];
        ifstream file;
        file.open(name.c_str(), ios::binary | ios::in);

        // MAKE SURE FILE IS GOOD
        bool isGood = file.good(); 

        if(file.good()) 
        {
                // READ THE FILE HEADER
                file.seekg(0, ios::beg);
                file.read((char*)header, 25);
                file.close();

                if(memcmp(header,"\211PNG\r\n\032\n",8)==0) 
                {       
                        // PNG FILE
                        *width  = ((int(header[16])<<8 | int(header[17]))<<8 | 
int(header[18]))<<8 | int(header[19]);
                        *height = ((int(header[20])<<8 | int(header[21]))<<8 | 
int(header[22]))<<8 | int(header[23]);
                }
                else if((memcmp(header,"GIF8", 4)==0) && ((header[4]=='9') || 
(header[4]=='7') ) && (header[5]=='a')                                          
          )
                {
                        // GIF FILE
                        *width  = int(header[7])<<8 | int(header[6]);
                        *height = int(header[9])<<8 | int(header[8]);
                }
                else
                {
                        // UNKNOWN FILE
                        *width  = 0;
                        *height = 0;

                }
                return;
        }
        XASSERT(false);
}








-----Original Message-----
From: Daniel Derr [mailto:[email protected]]
Sent: Thu 12/18/2008 12:11 AM
To: [email protected]
Subject: RE: [Wt-interest] Is there a way to know the image width and height?
 

I wrote this today from file format specs. Maybe this will help. I don't think 
there is any legal issues with gif and such, however it is hard to say. I plan 
on improving it a bit, but this is a good start. Seems to work for me.

#include <iostream>
#include <fstream>

using namespace std;

DDImageReader::DDImageReader(string name, DDImage * image)
{
    // OPEN THE FILE
    ifstream file;
    file.open(name.c_str(), ios::binary | ios::in);

    // MAKE SURE FILE IS GOOD
    if(file.good())
    {
        unsigned char header[10];
        unsigned char value[4];

        // IDENTIFY THE TYPE
        file.seekg(0, ios::beg);
        file.read((char*)header, 10);

        image->setName(name);
        if(memcmp(header,"\211PNG\r\n\032\n",8)==0)
        {
            // SET NAME AND TYPE
            image->setType(DDIMAGE_PNG);

            // SEEK TO DIMENSION BITS
            file.seekg(16, ios::beg);

            // WIDTH AND HEIGHT
            file.read((char*)value, 4); 
            image->setWidth (int(value[0])<<24 | int(value[1])<<16 | 
int(value[2])<<8 | int(value[3]));
            file.read((char*)value, 4); 
            image->setHeight(int(value[0])<<24 | int(value[1])<<16 | 
int(value[2])<<8 | int(value[3]));
        }
        else if((memcmp(header,"GIF89a", 6)==0) ||
                (memcmp(header,"GIF87a", 6)==0)     )
        {
            // SET NAME AND TYPE
            image->setType(DDIMAGE_GIF);

            // WIDTH AND HEIGHT
            image->setWidth (int(header[7])<<8 | int(header[6]));
            image->setHeight(int(header[9])<<8 | int(header[8]));
        }
        else
        {
            image->setType(DDIMAGE_UNKNOWN);
            image->setWidth (0);
            image->setHeight(0);
        }
    }
    // CLOSE THE FILE
    file.close();
}

















-----Original Message-----
From: Daniel Derr [mailto:[email protected]]
Sent: Wed 12/17/2008 12:15 AM
To: [email protected]
Subject: RE: [Wt-interest] Is there a way to know the image width and height?
 

I also having issue with needed to know the size of an image on the server 
side. So it would be desirable to read the image size from the file without 
having to load the entire image into memory. I will keep you posted if I find 
out any new information.

Daniel

-----Original Message-----
From: Koen Deforche [mailto:[email protected]]
Sent: Tue 12/9/2008 8:24 AM
To: [email protected]
Subject: Re: [Wt-interest] Is there a way to know the image width and height?
 
Hey David,

2008/12/8 David Eriksson <[email protected]>:
> Hi,
>
> Maybe you can use Boost GIL:
> http://www.boost.org/doc/libs/1_37_0/libs/gil/doc/index.html

Thanks for that link. I didn't know about Gil. I wonder if (and hope
that) boost will ever adopt a rasterizer and font rendering library
(such as Cairo or Anti Grain) to complement Gil.

That would be nice to use as bitmap backend to WPaintDevice (next to
the <canvas>, SVG and VML vector graphics backends).

Regards,
koen

------------------------------------------------------------------------------
SF.Net email is Sponsored by MIX09, March 18-20, 2009 in Las Vegas, Nevada.
The future of the web can't happen without you.  Join us at MIX09 to help
pave the way to the Next Web now. Learn more and register at
http://ad.doubleclick.net/clk;208669438;13503038;i?http://2009.visitmix.com/
_______________________________________________
witty-interest mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/witty-interest



<<winmail.dat>>

------------------------------------------------------------------------------
SF.Net email is Sponsored by MIX09, March 18-20, 2009 in Las Vegas, Nevada.
The future of the web can't happen without you.  Join us at MIX09 to help
pave the way to the Next Web now. Learn more and register at
http://ad.doubleclick.net/clk;208669438;13503038;i?http://2009.visitmix.com/
_______________________________________________
witty-interest mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/witty-interest

Reply via email to