Merging Two Images

When working with multiple images, you may find the need to merge two images together. Snowbound Software’s RasterMaster Imaging SDK product for both Windows (DLL, ActiveX,.NET) and for Java provide functions and methods that give you the ability to merge two images.

Below are code samples explaining how to merge two images in JAVA and C/C++:

************************* IN JAVA (Java Imaging Toolkit) ***************************

/********************************************************************

*

* MergeImages

*

* Demonstrates how to merge two images.

*

* Snowbound Functions Demonstrated:

* IMG_decompress_bitmap()

* IMG_promote_24()

* IMG_merge_block()

* IMG_save_bitmap()

*

* Copyright (c) 2006, Snowbound Software.

*

*******************************************************************/

import Snow.Snowbnd;

public class MergeImages

{

/**

* This method demonstrates how to merge two images.

*

* @param inFile1 Name of first file to merge.

* @param inFile2 Name of second file to merge.

* @param outFileName Name of output file.

* @param fileType Type of file to save (See Appendix B)

* @return Integer. Status of file conversion method.

*/

public static int mergeImages

(

String inFile1, /* name of first file to merge */

String inFile2, /* name of second file to merge */

String outFileName, /* name of merged file */

int fileType /* type of file to save (Appendix B) */

)

{

Snowbnd sImage1 = new Snowbnd(); /* Snowbound object for image */

Snowbnd sImage2 = new Snowbnd(); /* Snowbound object for image */

int nStatus = 0; /* initialize return status */

int imageHandle1 = 0; /* Snowbound image handle */

int imageHandle2 = 0; /* Snowbound image handle */

int imageDepth1; /* bits depth of image */

int imageDepth2; /* bits depth of image */

/* load (read in) first file for merge */

/* NOTE: Snowbound will determine the file type and open accordingly */

imageHandle1 = sImage1.IMG_decompress_bitmap( inFile1, 0 );

/* check if valid image handle */

if ( imageHandle1 < 0 )

{

/* if nImageHandle less than 0 there was an error decompressing
file */

/* do something clever here for error */

return imageHandle1;

}

/* get image bit depth */

imageDepth1 = sImage1.getBitsPerPixel();

/* load (read in) second file for merge */

/* NOTE: Snowbound will determine the file type and open accordingly */

imageHandle2 = sImage2.IMG_decompress_bitmap( inFile2, 0 );

/* check if valid image handle */

if ( imageHandle2 < 0 )

{

/* if nImageHandle less than 0 there was an error decompressing
file */

/* do something clever here for error */

return imageHandle2;

}

/* get image bit depth */

imageDepth2 = sImage2.getBitsPerPixel();

/* make bit depths compatible */

if ( imageDepth1 != imageDepth2 )

{

if ( imageDepth1 < 24 )

{

sImage1.IMG_promote_24();

}

if ( imageDepth2 < 24 )

{

sImage2.IMG_promote_24();

}

}

/* merge the text bitmap onto the image */

nStatus = sImage1.IMG_merge_block( sImage2, 0, 0, 2 );

if ( nStatus < 0 )

{

/* error occurred while trying to merge */

/* do something clever here for error */

return nStatus;

}

/* save file to desired file type */

/* NOTE: See Appendix B of manual for a complete list of file type constants */

nStatus = sImage1.IMG_save_bitmap( outFileName, fileType );

/* check for error */

if ( nStatus < 0 )

{

/* error occurred while trying to save image to desired type */

/* do something clever here for error */

return nStatus;

}

/* return status */

return nStatus;

}

}

*************************** IN C/C++ (Windows Imaging Toolkit) ************************

/********************************************************************

*

* MergeImages

*

* Demonstrates how to merge two images.

*

* Snowbound Functions Demonstrated:

* IMG_decompress_bitmap()

* IMG_bitmap_info()

* IMG_promote_8()

* IMG_promote_24()

* IMG_promote_32()

* IMG_merge_bitmap_handle()

* IMG_save_bitmap()

* IMG_delete_bitmap()

*

* Copyright (c) 2006, Snowbound Software.

*

*******************************************************************/

/* windows specific include files */

#ifdef WIN32

#include

#include

#include "nt_glue.h"

#endif

/* the following include files are supplied with your Snowbound product */

#include "imglib.h"

#include "img_err.h"

int MergeImages

(

char* szInFile1, /* name of first image to merge */

char* szInFile2, /* name of second image to merge */

char* szOutFileName, /* name of merged file */

int nFileType /* type of file to save (Appendix B) */

)

{

int nStatus = 0; /* initialize return status */

int nImageHandle1 = 0; /* Snowbound image handle for first image */

int nImageHandle2 = 0; /* Snowbound image handle for second image */

int nWidth1; /* width of first image */

int nHeight1; /* height of first image */

int nDepth1; /* bit depth of first image */

int nWidth2; /* width of second image */

int nHeight2; /* height of second image */

int nDepth2; /* bit depth of second image */

/* load (read in) first file for merge */

/* NOTE: Snowbound will determine the file type and open accordingly */

nImageHandle1 = IMG_decompress_bitmap( szInFile1 );

/* check if valid image handle */

if ( nImageHandle1 < 0 )

{

/* if nImageHandle1 less than 0 there was an error decompressing file */

/* do something clever here for error */

return nImageHandle1;

}

/* get image information: height, width, bit depth */

IMG_bitmap_info( nImageHandle1, &nWidth1, &nHeight1, &nDepth1 );

/* load (read in) second file for merge */

/* NOTE: Snowbound will determine the file type and open accordingly */

nImageHandle2 = IMG_decompress_bitmap( szInFile2 );

/* check if valid image handle */

if ( nImageHandle2 < 0 )

{

/* if nImageHandle2 less than 0 there was an error decompressing file */

/* do something clever here for error */

return nImageHandle2;

}

/* get image information: height, width, bit depth */

IMG_bitmap_info( nImageHandle2, &nWidth2, &nHeight2, &nDepth2 );

/* make bit depths compatible */

if ( nDepth1 < nDepth2 )

{

switch ( nDepth2 )

{

case 8:

IMG_promote_8( nImageHandle1 );

break;

case 24:

IMG_promote_24( nImageHandle1 );

break;

case 32:

IMG_promote_32( nImageHandle1 );

break;

}

}

else if ( nDepth1 > nDepth2 )

{

switch ( nDepth1 )

{

case 8:

IMG_promote_8( nImageHandle2 );

break;

case 24:

IMG_promote_24( nImageHandle2 );

break;

case 32:

IMG_promote_32( nImageHandle2 );

break;

}

}

/* merge the images */

nStatus = IMG_merge_bitmap_handle( nImageHandle1, nImageHandle2, 2, 0, 0 );

if ( nStatus < 0 )

{

/* error occurred while trying to merge */

/* free up image handles in Snowbound library */

IMG_delete_bitmap( nImageHandle1 );

IMG_delete_bitmap( nImageHandle2 );

/* do something clever here for error */

return nStatus;

}

/* save file to desired file type */

/* NOTE: See Appendix B of manual for a complete list of file type constants */

nStatus = IMG_save_bitmap( nImageHandle1, szOutFileName, nFileType );

/* check for error */

if ( nStatus < 0 )

{

/* error occurred while trying to save image to desired type */

/* free up image handles in Snowbound library */

IMG_delete_bitmap( nImageHandle1 );

IMG_delete_bitmap( nImageHandle2 );

/* do something clever here for error */

return nStatus;

}

/* free up image handles in Snowbound library */

nStatus = IMG_delete_bitmap( nImageHandle1 );

nStatus = IMG_delete_bitmap( nImageHandle2 );

/* return status */

return nStatus;

}

Category:

Online Demo