Page 1 of 1

Function to detect and resize image file

Posted: Tue Apr 29, 2014 5:34 am
by hmgchang
Dear Masters,

1. Any HMG Functions to detect the imagefile ( jpg, bmp, etc) for Pixel Dimensions ?
2. Any HMG Functions to resize the above dimension and save the new dimension to imagefile ?

When we are using image control to display from imagefile, sometime the image cannot be properly
displayed due to the portrait/landscape orientation. Secondly nowaday picture taken by digital camera
resolution is to big and if only we can resize it to our need, not only save space by speedup the program too.

TIA

Best Regards
Chang

Re: Function to detect and resize image file

Posted: Tue Apr 29, 2014 7:09 am
by mol
I'm using such a code for detecting size of image:

Code: Select all

aSizeOfPicture := HB_GETIMAGESIZE( cImageFileName)

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

Re: Function to detect and resize image file

Posted: Tue Apr 29, 2014 12:06 pm
by esgici
Hi

My way is a bit different ( use high level whenever possible ):
HMG Doc wrote:BT_BitmapWidth (hBitmap)
Return the width of the specified bitmap in pixels.
hBitmap: is a handle to bitmap.

BT_BitmapHeight (hBitmap)
Return the height of the specified bitmap in pixels.
hBitmap: is a handle to bitmap.
Sample :

~hmg~\hfcl\Samples\BosTaurus\demo06.prg

Happy HMG'ing :D

Re: Function to detect and resize image file

Posted: Tue Apr 29, 2014 2:12 pm
by srvet_claudio
esgici wrote:Hi

My way is a bit different ( use high level whenever possible ):
HMG Doc wrote:BT_BitmapWidth (hBitmap)
Return the width of the specified bitmap in pixels.
hBitmap: is a handle to bitmap.

BT_BitmapHeight (hBitmap)
Return the height of the specified bitmap in pixels.
hBitmap: is a handle to bitmap.
Sample :

~hmg~\hfcl\Samples\BosTaurus\demo06.prg

Happy HMG'ing :D
Other way:

Code: Select all

 HMG_GetImageInfo ( [ cFileName | hBitmap ], @nWidth, @nHeight, @aBackColor, [ nRowColor ], [ nColColor ] ) --> return lBoolean

Re: Function to detect and resize image file

Posted: Tue Apr 29, 2014 2:34 pm
by esgici
srvet_claudio wrote:...
Other way:

Code: Select all

HMG_GetImageInfo ( [ cFileName | hBitmap ], @nWidth, @nHeight, @aBackColor, [ nRowColor ], [ nColColor ] ) --> return lBoolean
Oh, yeah !
New Function ( Introduced in 3.2 ) :)
I missed it :oops:

Added later :

My two cents, a little sample for HMG_GetImageInfo() function :

Code: Select all

#include <hmg.ch>

PROCEDURE Main()

   LOCAL cMessage := ImageInfoVerbose( "C:\hmg\hfcl\Samples\BosTaurus\BosTaurus_logo.bmp" )

   IF HB_ISNIL( cMessage ) 
      MsgStop( "Couldn't get image info !", "Error !" )
   ELSE
      MsgBox( cMessage, "Image Info" )
   ENDIF
 
RETURN // ImageInfoSample.Main()
 
*-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.

FUNCTION ImageInfoVerbose( cIMageFile )
    
   LOCAL xRVal, nWidth, nHeight, aBackColor 

   IF HMG_GetImageInfo ( cIMageFile, @nWidth, @nHeight, @aBackColor ) 

      xRVal := "Width : " + NTOC( nWidth ) + CRLF +;   
               "Height : " + NTOC( nHeight ) + CRLF +;   
               "Color : " + HB_ValToExp( aBackColor ) 
   ENDIF

RETURN xRVal // ImageInfoVerbose()

*-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.
Viva HMG, viva Dr. Soto :D

Re: Function to detect and resize image file

Posted: Wed Apr 30, 2014 5:25 am
by hmgchang
Wonderful....
and thanks very much Masters.

Now images can be display properly in the control....
but... any suggestion on how to resize the imagefile ?
;)

Re: Function to detect and resize image file

Posted: Wed Apr 30, 2014 6:48 am
by srvet_claudio
hmgchang wrote:any suggestion on how to resize the imagefile ?
See:
BT_BitmapCopyAndResize (hBitmap, New_Width, New_Height, Mode_Stretch, Algorithm)

Code: Select all

Copy and resizes the specified bitmap and returns a handle to the bitmap with the new size. 

hBitmap: is a handle to the origin bitmap. 

New_Width, New_Height: is the new size of the bitmap image. 

Mode_Stretch: sets the mode as the bitmap of origin is adjusts (is stretches or compresses) in the new size bitmap, it is one of the constants: BT_SCALE or BT_STRETCH (defined in BosTaurus.CH). For default Mode_Stretch = BT_STRETCH. 

Algorithm: sets the mode as the bitmap is resized, it is one of the constants:BT_RESIZE_COLORONCOLOR, BT_RESIZE_HALFTONE or BT_RESIZE_BILINEAR (defined in BosTaurus.CH). For default Algorithm = BT_RESIZE_HALFTONE. 

Re: Function to detect and resize image file

Posted: Fri May 02, 2014 1:59 am
by hmgchang
I read about :
Photo Resizer by HMG+FreeImage.dll by G. Filatov ( one of HMG Forum Topic, but
i dont know how to make the link from here.)

but i need help on how to use Mr. Filatov's DLL for taking an imagefile then
resize and saveback to an imagefile.

Best Regards,
Chang