How to extract picture dimension from jpg file?

General Help regarding HMG, Compilation, Linking, Samples

Moderator: Rathinagiri

Post Reply
User avatar
mol
Posts: 3825
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Contact:

How to extract picture dimension from jpg file?

Post by mol »

Hi!
Has sb. any idea how to simply extract dimension of picture in pixels from jpg file?

Best regards, Marek
User avatar
danielmaximiliano
Posts: 2763
Joined: Fri Apr 09, 2010 4:53 pm
DBs Used: DBF
Location: Argentina
Contact:

Re: How to extract picture dimension from jpg file?

Post by danielmaximiliano »

Hello Mol:
you need to use contrib Harbour HBFimage
Greenshot_2012-01-30_17-59-13.png
Greenshot_2012-01-30_17-59-13.png (60.54 KiB) Viewed 3996 times
*´¨)
¸.·´¸.·*´¨) ¸.·*¨)
(¸.·´. (¸.·` *
.·`. Harbour/HMG : It's magic !
(¸.·``··*

Saludos / Regards
DaNiElMaXiMiLiAnO

Whatsapp. := +54901169026142
Telegram Name := DaNiElMaXiMiLiAnO
Telegram invitation https://t.me/HMGWorkspace
User avatar
danielmaximiliano
Posts: 2763
Joined: Fri Apr 09, 2010 4:53 pm
DBs Used: DBF
Location: Argentina
Contact:

Re: How to extract picture dimension from jpg file?

Post by danielmaximiliano »

Hi:
PDF Freeimage project : http://downloads.sourceforge.net/freeim ... ge3151.pdf
PDF Complete Special Edition_2012-01-30_19-47-49.png
PDF Complete Special Edition_2012-01-30_19-47-49.png (57.08 KiB) Viewed 3984 times
*´¨)
¸.·´¸.·*´¨) ¸.·*¨)
(¸.·´. (¸.·` *
.·`. Harbour/HMG : It's magic !
(¸.·``··*

Saludos / Regards
DaNiElMaXiMiLiAnO

Whatsapp. := +54901169026142
Telegram Name := DaNiElMaXiMiLiAnO
Telegram invitation https://t.me/HMGWorkspace
User avatar
mol
Posts: 3825
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Contact:

Re: How to extract picture dimension from jpg file?

Post by mol »

Many thanks, Daniel, for your quick response!
Best regards, Marek
User avatar
gfilatov
Posts: 1116
Joined: Fri Aug 01, 2008 5:42 am
Location: Ukraine
Contact:

Re: How to extract picture dimension from jpg file?

Post by gfilatov »

mol wrote:Hi!
Has sb. any idea how to simply extract dimension of picture in pixels from jpg file?
Hi Marek,

If you don't want to add the big FreeImage.dll to your project then you may take a look for the following small C-code:

Code: Select all

#pragma BEGINDUMP 

#include "windows.h"

#include "hbapi.h" 
#include "hbapifs.h"

/*
   Author: Andi Jahja <harbour@cbn.net.id>
*/
BOOL GetImageSize( const char *fn, int *x, int *y ) 
{ 
   unsigned char buf[24]; 
   long len; 

   FILE *f = hb_fopen( fn, "rb" ); 

   if (!f) 
      return FALSE; 

   fseek( f, 0, SEEK_END ); 

   len = ftell( f ); 

   fseek( f, 0, SEEK_SET ); 

   if ( len<24 ) 
   { 
      fclose( f ); 
      return FALSE; 
   } 

   // Strategy: 
   // reading GIF dimensions requires the first 10 bytes of the file 
   // reading PNG dimensions requires the first 24 bytes of the file 
   // reading JPEG dimensions requires scanning through jpeg chunks 
   // In all formats, the file is at least 24 bytes big, so we'll read 
   //that always 
   fread( buf, 1, 24, f ); 

   // For JPEGs, we need to read the first 12 bytes of each chunk. 
   // We'll read those 12 bytes at buf+2...buf+14, i.e. overwriting 
   // the existing buf. 
   if (buf[0]==0xFF && buf[1]==0xD8 && buf[2]==0xFF && buf[3]==0xE0 && 
       buf[6]=='J' && buf[7]=='F' && buf[8]=='I' && buf[9]=='F') 
   { 
     long pos = 2; 
     while ( buf[2]==0xFF ) 
     { 
       if (buf[3]==0xC0 || buf[3]==0xC1 || buf[3]==0xC2 || 
          buf[3]==0xC3 || buf[3]==0xC9 || buf[3]==0xCA || 
          buf[3]==0xCB) 
          break; 
       pos += 2+(buf[4]<<8)+buf[5]; 
       if ( pos+12>len ) 
          break; 
       fseek( f, pos, SEEK_SET ); 
       fread( buf+2, 1, 12, f); 
     } 
   } 

   fclose(f); 

   // JPEG: (first two bytes of buf are first two bytes of the jpeg 
   // file; rest of buf is the DCT frame 
   if (buf[0]==0xFF && buf[1]==0xD8 && buf[2]==0xFF) 
   { 
      *y = (buf[7]<<8) + buf[8]; 
      *x = (buf[9]<<8) + buf[10]; 
      return TRUE; 
   } 

   // GIF: first three bytes say "GIF", next three give version 
   // number. Then dimensions 
   if (buf[0]=='G' && buf[1]=='I' && buf[2]=='F') 
   { 
      *x = buf[6] + (buf[7]<<8); 
      *y = buf[8] + (buf[9]<<8); 
      return TRUE; 
   } 

   // PNG: the first frame is by definition an IHDR frame, which gives 
   // dimensions 
   if ( buf[0]==0x89 && buf[1]=='P' && buf[2]=='N' && buf[3]=='G' && 
       buf[4]==0x0D && buf[5]==0x0A && buf[6]==0x1A && buf[7]==0x0A 
       && buf[12]=='I' && buf[13]=='H' && buf[14]=='D' && 
       buf[15]=='R') 
   { 
      *x = (buf[16]<<24) + (buf[17]<<16) + (buf[18]<<8)+(buf[19]<<0); 
      *y = (buf[20]<<24) + (buf[21]<<16) + (buf[22]<<8)+(buf[23]<<0); 
      return TRUE; 
   } 

   return FALSE; 
} 

HB_FUNC( HB_GETIMAGESIZE ) 
/* 
  Syntax: HB_GETIMAGESIZE( cPicFile ) 
  Parameter: cPicFile = graphic file (JPG, GIF, PNG) 
  Return: 2 dim array -> array[1] = width, array[2] == height
*/ 
{ 
   int x = 0, y = 0; 

   GetImageSize( hb_parcx( 1 ), &x, &y ); 

   hb_reta( 2 ); 
   hb_storvni( x, -1, 1 ); 
   hb_storvni( y, -1, 2 ); 
} 

#pragma ENDDUMP
Hope that helps :idea:
Kind Regards,
Grigory Filatov

"Everything should be made as simple as possible, but no simpler." Albert Einstein
User avatar
mol
Posts: 3825
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Contact:

Re: How to extract picture dimension from jpg file?

Post by mol »

THX Grigori!
I've written my own function based on sample from internet, too!
I'll try to reduce it and I'll present it, then.

best regards, Marek
User avatar
esgici
Posts: 4543
Joined: Wed Jul 30, 2008 9:17 pm
DBs Used: DBF
Location: iskenderun / Turkiye
Contact:

Re: How to extract picture dimension from jpg file?

Post by esgici »

mol wrote:... I'll present it, then.
I'll wait it :)

Regards

--

Esgici
Viva INTERNATIONAL HMG :D
User avatar
mol
Posts: 3825
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Contact:

Re: How to extract picture dimension from jpg file?

Post by mol »

gfilatov wrote:
mol wrote:Hi!
Has sb. any idea how to simply extract dimension of picture in pixels from jpg file?
Hi Marek,

If you don't want to add the big FreeImage.dll to your project then you may take a look for the following small C-code:
...
Hope that helps :idea:
I've decided to use your sample Grigori, because of its universality (3 formats) and very small code :)
Many thanks!


PS. While searching JPG format description, I've read that some problems with dimensions of JPG. Sometimes, this way you can read the size of miniature of JPG, not the size of image...
I'll test it
Last edited by mol on Tue Jan 31, 2012 7:09 pm, edited 1 time in total.
User avatar
mol
Posts: 3825
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Contact:

Re: How to extract picture dimension from jpg file?

Post by mol »

esgici wrote:
mol wrote:... I'll present it, then.
I'll wait it :)

Regards

--

Esgici
Hi Esgici, I'll try to finish my code and add some comments, but, I think, Grigori's code looks so much interesting, so I'll use it.
regards, Marek
Post Reply