Files
harbour-core/contrib/hbwin/win_bmpd.c
Viktor Szakats e00f50079e 2017-09-11 13:12 UTC Viktor Szakats (vszakats users.noreply.github.com)
* contrib/gtqtc/gtqtc1.cpp
  * contrib/gtwvg/gtwgud.c
  * contrib/gtwvw/wvwdraw.c
  * contrib/hbamf/amfdec.c
  * contrib/hbamf/amfstdio.c
  * contrib/hbbz2io/bz2io.c
  * contrib/hbgzio/gzio.c
  * contrib/hbhpdf/core.c
  * contrib/hbmemio/memio.c
  * contrib/hbmisc/irm.c
  * contrib/hbmisc/spd.c
  * contrib/hbnetio/netiosrv.c
  * contrib/hbssl/ssl_sock.c
  * contrib/hbwin/olecore.c
  * contrib/hbwin/wapi_commctrl.c
  * contrib/hbwin/wapi_winuser.c
  * contrib/hbwin/win_bmpd.c
  * contrib/hbwin/win_misc.c
  * contrib/hbzebra/qrcode.c
  * contrib/rddads/ads1.c
  * contrib/rddads/adsfunc.c
  * contrib/rddsql/sqlmix.c
  * contrib/sddfb/core.c
  * contrib/xhb/cstructc.c
  * contrib/xhb/hboutdbg.c
  * contrib/xhb/hbserv.c
  * include/hbdefs.h
  * include/hbexprb.c
  * include/hbrddcdx.h
  * include/hbthread.h
  * include/hbwmain.c
  * src/common/hbffind.c
  * src/compiler/complex.c
  * src/compiler/hbmain.c
  * src/compiler/hbopt.c
  * src/debug/dbgentry.c
  * src/rdd/dbf1.c
  * src/rdd/dbfcdx/dbfcdx1.c
  * src/rdd/dbffpt/dbffpt1.c
  * src/rdd/dbfnsx/dbfnsx1.c
  * src/rdd/dbfntx/dbfntx1.c
  * src/rdd/hbsix/sxcompr.c
  * src/rdd/wacore.c
  * src/rdd/workarea.c
  * src/rtl/cputime.c
  * src/rtl/dates.c
  * src/rtl/gtchrmap.c
  * src/rtl/gtcrs/gtcrs.c
  * src/rtl/gtpca/gtpca.c
  * src/rtl/gtsln/gtsln.c
  * src/rtl/gtsln/kbsln.c
  * src/rtl/gtsln/keytrans.c
  * src/rtl/gtstd/gtstd.c
  * src/rtl/gttrm/gttrm.c
  * src/rtl/gtwvt/gtwvt.c
  * src/rtl/gtxwc/gtxwc.c
  * src/rtl/hbcom.c
  * src/rtl/hbproces.c
  * src/rtl/itemseri.c
  * src/rtl/langapi.c
  * src/rtl/run.c
  * src/rtl/sha1.c
  * src/rtl/space.c
  * src/vm/classes.c
  * src/vm/hvm.c
  * src/vm/itemapi.c
  * src/vm/set.c
  * src/vm/thread.c
    * convert commented C code to #if 0 blocks, or clean them up in
      different ways
2017-09-11 13:27:12 +00:00

317 lines
8.9 KiB
C

/*
* Bitmap dimension detection
*
* Copyright 2010 Viktor Szakats (vszakats.net/harbour)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file LICENSE.txt. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA (or visit https://www.gnu.org/licenses/).
*
* As a special exception, the Harbour Project gives permission for
* additional uses of the text contained in its release of Harbour.
*
* The exception is that, if you link the Harbour libraries with other
* files to produce an executable, this does not by itself cause the
* resulting executable to be covered by the GNU General Public License.
* Your use of that executable is in no way restricted on account of
* linking the Harbour library code into it.
*
* This exception does not however invalidate any other reasons why
* the executable file might be covered by the GNU General Public License.
*
* This exception applies only to the code released by the Harbour
* Project under the name Harbour. If you copy code from other
* Harbour Project or Free Software Foundation releases into a copy of
* Harbour, as the General Public License permits, the exception does
* not apply to the code that you add in this way. To avoid misleading
* anyone as to the status of such modified files, you must delete
* this exception notice from them.
*
* If you write modifications of your own for Harbour, it is your choice
* whether to permit this exception to apply to your modifications.
* If you do not wish that, delete this exception notice.
*
*/
#include "hbwin.h"
#include "hbapiitm.h"
#if defined( HB_HAS_PNG ) && defined( HB_HAS_ZLIB )
#include "png.h"
#endif
/* .jpeg size detection code. [vszakats] */
#define _JPEG_RET_OK 0
#define _JPEG_RET_OVERRUN 1
#define _JPEG_RET_INVALID 2
#define _JPEG_RET_UNSUPPORTED 3
#define _JPEG_CS_GRAY 1
#define _JPEG_CS_RGB 2
#define _JPEG_CS_CMYK 3
static int hb_jpeg_get_param( const HB_BYTE * buffer, HB_SIZE nBufferSize, int * piHeight, int * piWidth, int * piColorSpace, int * piBPC )
{
HB_SIZE nPos = 0;
HB_U16 tag;
HB_U16 height = 0;
HB_U16 width = 0;
HB_BYTE colorspace = 0;
HB_BYTE bpc = 0;
if( piHeight )
*piHeight = ( int ) height;
if( piWidth )
*piWidth = ( int ) width;
if( piColorSpace )
*piColorSpace = ( int ) colorspace;
if( piBPC )
*piBPC = ( int ) bpc;
if( nPos >= nBufferSize )
return _JPEG_RET_OVERRUN;
tag = HB_SWAP_UINT16( ( HB_U16 ) HB_GET_LE_UINT16( buffer + nPos ) ); nPos += 2;
/* SOI marker */
if( tag != 0xFFD8 )
return _JPEG_RET_INVALID;
for( ;; )
{
HB_U16 size;
if( nPos >= nBufferSize )
return _JPEG_RET_OVERRUN;
tag = HB_SWAP_UINT16( ( HB_U16 ) HB_GET_LE_UINT16( buffer + nPos ) ); nPos += 2;
if( nPos >= nBufferSize )
return _JPEG_RET_OVERRUN;
size = HB_SWAP_UINT16( ( HB_U16 ) HB_GET_LE_UINT16( buffer + nPos ) ); nPos += 2;
/* SOF markers */
if( tag == 0xFFC0 ||
tag == 0xFFC1 ||
tag == 0xFFC2 ||
tag == 0xFFC9 )
{
if( nPos >= nBufferSize )
return _JPEG_RET_OVERRUN;
colorspace = *( buffer + nPos ); nPos += 1;
if( nPos >= nBufferSize )
return _JPEG_RET_OVERRUN;
height = HB_SWAP_UINT16( ( HB_U16 ) HB_GET_LE_UINT16( buffer + nPos ) ); nPos += 2;
if( nPos >= nBufferSize )
return _JPEG_RET_OVERRUN;
width = HB_SWAP_UINT16( ( HB_U16 ) HB_GET_LE_UINT16( buffer + nPos ) ); nPos += 2;
if( nPos >= nBufferSize )
return _JPEG_RET_OVERRUN;
bpc = *( buffer + nPos );
break;
}
else if( ( tag | 0x00FF ) != 0xFFFF ) /* lost marker */
return _JPEG_RET_UNSUPPORTED;
nPos += size - 2;
if( nPos >= nBufferSize )
return _JPEG_RET_OVERRUN;
}
if( piHeight )
*piHeight = ( int ) height;
if( piWidth )
*piWidth = ( int ) width;
if( piBPC )
*piBPC = ( int ) bpc;
if( piColorSpace )
{
switch( colorspace )
{
case 1: *piColorSpace = _JPEG_CS_GRAY; break;
case 3: *piColorSpace = _JPEG_CS_RGB; break;
case 4: *piColorSpace = _JPEG_CS_CMYK; break;
}
}
return _JPEG_RET_OK;
}
/* .png size detection code. [vszakats] */
#if defined( HB_HAS_PNG ) && defined( HB_HAS_ZLIB )
#define _PNG_RET_OK 0
#define _PNG_RET_ERR_INVALID1 1
#define _PNG_RET_ERR_INVALID2 2
#define _PNG_RET_ERR_INIT1 3
#define _PNG_RET_ERR_INIT2 4
#define _PNG_RET_ERR_INIT3 5
#define _PNG_RET_ERR_READ 6
typedef struct
{
const HB_BYTE * buffer;
HB_SIZE nLen;
HB_SIZE nPos;
HB_BOOL bOk;
} HB_PNG_READ;
static void hb_png_read_func( png_structp png_ptr, png_bytep data, png_uint_32 length )
{
HB_PNG_READ * hb_png_read_data = ( HB_PNG_READ * ) png_get_io_ptr( png_ptr );
png_uint_32 pos;
for( pos = 0; pos < length && hb_png_read_data->nPos < hb_png_read_data->nLen; )
data[ pos++ ] = hb_png_read_data->buffer[ hb_png_read_data->nPos++ ];
hb_png_read_data->bOk = ( length == pos );
}
static int hb_png_get_param( const HB_BYTE * buffer, HB_SIZE nBufferSize, int * piHeight, int * piWidth, int * piColorSpace, int * piBPC )
{
png_structp png_ptr;
png_infop info_ptr;
png_byte header[ 8 ];
HB_PNG_READ hb_png_read_data;
int iResult;
if( piHeight )
*piHeight = 0;
if( piWidth )
*piWidth = 0;
if( piColorSpace )
*piColorSpace = 0;
if( piBPC )
*piBPC = 0;
if( nBufferSize < sizeof( header ) )
return _PNG_RET_ERR_INVALID1;
memcpy( header, buffer, sizeof( header ) );
if( png_sig_cmp( header, ( png_size_t ) 0, sizeof( header ) ) )
return _PNG_RET_ERR_INVALID2;
png_ptr = png_create_read_struct( PNG_LIBPNG_VER_STRING, NULL, NULL, NULL );
if( ! png_ptr )
return _PNG_RET_ERR_INIT1;
info_ptr = png_create_info_struct( png_ptr );
if( ! info_ptr )
{
png_destroy_read_struct( &png_ptr, ( png_infopp ) NULL, ( png_infopp ) NULL );
return _PNG_RET_ERR_INIT2;
}
hb_png_read_data.buffer = buffer;
hb_png_read_data.nLen = nBufferSize;
hb_png_read_data.nPos = sizeof( header );
hb_png_read_data.bOk = HB_TRUE;
png_set_sig_bytes( png_ptr, sizeof( header ) );
png_set_read_fn( png_ptr, ( void * ) &hb_png_read_data, ( png_rw_ptr ) hb_png_read_func );
png_read_info( png_ptr, info_ptr );
if( hb_png_read_data.bOk )
{
png_uint_32 width;
png_uint_32 height;
int bit_depth;
int color_type;
png_get_IHDR( png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, NULL, NULL, NULL );
if( piHeight )
*piHeight = ( int ) height;
if( piWidth )
*piWidth = ( int ) width;
if( piBPC )
*piBPC = bit_depth;
if( piColorSpace )
*piColorSpace = color_type;
iResult = _PNG_RET_OK;
}
else
iResult = _PNG_RET_ERR_READ;
png_destroy_read_struct( &png_ptr, &info_ptr, ( png_infopp ) NULL );
return iResult;
}
#endif
HB_FUNC( WIN_BITMAPDIMENSIONS )
{
const void * buffer = hb_parc( 1 );
HB_SIZE nSize = hb_parclen( 1 );
int iType = hbwin_bitmapType( buffer, nSize );
int iHeight = 0;
int iWidth = 0;
HB_BOOL bRetVal = HB_FALSE;
if( iType == HB_WIN_BITMAP_BMP && nSize >= sizeof( BITMAPCOREHEADER ) )
{
BITMAPFILEHEADER * pbmfh = ( BITMAPFILEHEADER * ) buffer;
BITMAPINFO * pbmi = ( BITMAPINFO * ) ( pbmfh + 1 );
/* Remember there are 2 types of BitMap File */
if( pbmi->bmiHeader.biSize == sizeof( BITMAPCOREHEADER ) )
{
iWidth = ( ( BITMAPCOREHEADER * ) pbmi )->bcWidth;
iHeight = ( ( BITMAPCOREHEADER * ) pbmi )->bcHeight;
}
else
{
iWidth = pbmi->bmiHeader.biWidth;
iHeight = abs( pbmi->bmiHeader.biHeight );
}
bRetVal = HB_TRUE;
}
else if( iType == HB_WIN_BITMAP_JPEG )
{
bRetVal = ( hb_jpeg_get_param( ( const HB_BYTE * ) buffer, nSize, &iHeight, &iWidth, NULL, NULL ) == _JPEG_RET_OK );
}
#if defined( HB_HAS_PNG ) && defined( HB_HAS_ZLIB )
else if( iType == HB_WIN_BITMAP_PNG )
{
bRetVal = ( hb_png_get_param( ( const HB_BYTE * ) buffer, nSize, &iHeight, &iWidth, NULL, NULL ) == _PNG_RET_OK );
}
#endif
hb_storni( iWidth, 2 );
hb_storni( iHeight, 3 );
hb_retl( bRetVal );
}