2010-07-24 19:43:53 +00:00
/*
* SpanDSP - a series of DSP components for telephony
*
* image_translate_tests . c - Tests for the image translation routines .
*
* Written by Steve Underwood < steveu @ coppice . org >
*
* Copyright ( C ) 2009 Steve Underwood
*
* All rights reserved .
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License version 2 , as
* published by the Free Software Foundation .
*
* 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 ; if not , write to the Free Software
* Foundation , Inc . , 675 Mass Ave , Cambridge , MA 0213 9 , USA .
*/
/*! \file */
/*! \page image_translate_tests_page Image translation tests
\ section image_translate_tests_page_sec_1 What does it do ?
*/
# if defined(HAVE_CONFIG_H)
# include "config.h"
# endif
# include <inttypes.h>
# include <stdlib.h>
# include <stdio.h>
# include <ctype.h>
# include <string.h>
# include <assert.h>
# include <math.h>
# include <errno.h>
//#if defined(WITH_SPANDSP_INTERNALS)
# define SPANDSP_EXPOSE_INTERNAL_STRUCTURES
//#endif
# include "spandsp.h"
# define INPUT_TIFF_FILE_NAME ".. / test-data / local / lenna-colour.tif"
typedef struct
{
const uint8_t * image ;
int width ;
int length ;
int current_row ;
int bytes_per_pixel ;
} image_descriptor_t ;
static void display_row ( int row , int width , uint8_t buf [ ] )
{
int i ;
int test_pixel ;
printf ( " %3d: " , row ) ;
for ( i = 0 ; i < width ; i + + )
{
test_pixel = ( buf [ i > > 3 ] > > ( 7 - ( i & 7 ) ) ) & 0x01 ;
printf ( " %c " , ( test_pixel ) ? ' ' : ' @ ' ) ;
}
printf ( " \n " ) ;
}
/*- End of function --------------------------------------------------------*/
static int test_dithered_50_by_50 ( int row , int width , uint8_t buf [ ] )
{
static const char * image [ 50 ] =
{
" 0: @ @ @ @ @ @ @ @@ @@@@@@@@@@@@@@ " ,
" 1: @ @ @ @ @ @ @ @ @ @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 48: @ @ @ @ @ @ @@ @ @@ @@@@@@ @@@@@ " ,
" 49: @ @ @ @ @ @ @ @ @ @ @ @@ @@ @ @ @@@@@@@ "
} ;
int i ;
int match ;
int ref_pixel ;
int test_pixel ;
match = 0 ;
for ( i = 0 ; i < width ; i + + )
{
ref_pixel = ( image [ row ] [ i + 5 ] = = ' ' ) ;
test_pixel = ( buf [ i > > 3 ] > > ( 7 - ( i & 7 ) ) ) & 0x01 ;
if ( ref_pixel ! = test_pixel )
match = - 1 ;
}
return match ;
}
/*- End of function --------------------------------------------------------*/
static int row_read ( void * user_data , uint8_t buf [ ] , size_t len )
{
image_descriptor_t * im ;
im = ( image_descriptor_t * ) user_data ;
if ( im - > current_row > = im - > length )
return 0 ;
memcpy ( buf , & im - > image [ im - > current_row * im - > width * im - > bytes_per_pixel ] , len ) ;
im - > current_row + + ;
return len ;
}
/*- End of function --------------------------------------------------------*/
2012-08-08 13:26:06 +00:00
static void get_bilevel_image ( image_translate_state_t * s , int compare )
2010-07-24 19:43:53 +00:00
{
int i ;
int len ;
uint8_t row_buf [ 5000 ] ;
for ( i = 0 ; i < s - > output_length ; i + + )
{
if ( ( len = image_translate_row ( s , row_buf , ( s - > output_width + 7 ) / 8 ) ) ! = ( s - > output_width + 7 ) / 8 )
{
printf ( " Image finished early - %d %d \n " , len , ( s - > output_width + 7 ) / 8 ) ;
exit ( 2 ) ;
}
display_row ( i , s - > output_width , row_buf ) ;
if ( compare )
{
if ( test_dithered_50_by_50 ( i , s - > output_width , row_buf ) )
{
printf ( " Dithered image mismatch at row %d \n " , i ) ;
printf ( " Test failed \n " ) ;
exit ( 2 ) ;
}
}
}
if ( ( len = image_translate_row ( s , row_buf , ( s - > output_width + 7 ) / 8 ) ) ! = 0 )
{
printf ( " Image finished late - %d %d \n " , len , ( s - > output_width + 7 ) / 8 ) ;
exit ( 2 ) ;
}
}
/*- End of function --------------------------------------------------------*/
2012-08-08 13:26:06 +00:00
static void get_gray8_image ( image_translate_state_t * s , int compare )
{
int i ;
int j ;
int len ;
uint8_t row_buf [ 5000 ] ;
for ( i = 0 ; i < s - > output_length ; i + + )
{
if ( ( len = image_translate_row ( s , row_buf , s - > output_width ) ) ! = s - > output_width )
{
printf ( " Image finished early - %d %d \n " , len , s - > output_width ) ;
exit ( 2 ) ;
}
if ( compare )
{
for ( j = 0 ; j < 50 ; j + + )
{
if ( row_buf [ j ] ! = j * 1200 / 256 )
{
printf ( " Image mismatch - %d - %d \n " , j , row_buf [ 3 * j ] ) ;
exit ( 2 ) ;
}
}
}
}
if ( ( len = image_translate_row ( s , row_buf , s - > output_width ) ) ! = 0 )
{
printf ( " Image finished late - %d %d \n " , len , s - > output_width ) ;
exit ( 2 ) ;
}
}
/*- End of function --------------------------------------------------------*/
static void get_gray16_image ( image_translate_state_t * s , int compare )
{
int i ;
int j ;
int len ;
uint16_t row_buf [ 5000 ] ;
for ( i = 0 ; i < s - > output_length ; i + + )
{
if ( ( len = image_translate_row ( s , ( uint8_t * ) row_buf , 2 * s - > output_width ) ) ! = 2 * s - > output_width )
{
printf ( " Image finished early - %d %d \n " , len , 2 * s - > output_width ) ;
exit ( 2 ) ;
}
if ( compare )
{
for ( j = 0 ; j < 50 ; j + + )
{
if ( row_buf [ j ] ! = j * 1200 )
{
printf ( " Image mismatch - %d - %d \n " , j , row_buf [ j ] ) ;
exit ( 2 ) ;
}
}
}
}
if ( ( len = image_translate_row ( s , ( uint8_t * ) row_buf , 2 * s - > output_width ) ) ! = 0 )
{
printf ( " Image finished late - %d %d \n " , len , 2 * s - > output_width ) ;
exit ( 2 ) ;
}
}
/*- End of function --------------------------------------------------------*/
static void get_colour8_image ( image_translate_state_t * s , int compare )
{
int i ;
int j ;
int len ;
uint8_t row_buf [ 5000 ] ;
for ( i = 0 ; i < s - > output_length ; i + + )
{
if ( ( len = image_translate_row ( s , row_buf , 3 * s - > output_width ) ) ! = 3 * s - > output_width )
{
printf ( " Image finished early - %d %d \n " , len , 3 * s - > output_width ) ;
exit ( 2 ) ;
}
if ( compare )
{
for ( j = 0 ; j < 50 ; j + + )
{
if ( row_buf [ 3 * j + 0 ] ! = j * 1200 / 256 | | row_buf [ 3 * j + 1 ] ! = j * 1200 / 256 | | row_buf [ 3 * j + 2 ] ! = j * 1200 / 256 )
{
printf ( " Image mismatch - %d - %d %d %d \n " , j , row_buf [ 3 * j + 0 ] , row_buf [ 3 * j + 1 ] , row_buf [ 3 * j + 2 ] ) ;
exit ( 2 ) ;
}
}
}
}
if ( ( len = image_translate_row ( s , row_buf , 2 * s - > output_width ) ) ! = 0 )
{
printf ( " Image finished late - %d %d \n " , len , 3 * s - > output_width ) ;
exit ( 2 ) ;
}
}
/*- End of function --------------------------------------------------------*/
static void get_colour16_image ( image_translate_state_t * s , int compare )
{
int i ;
int j ;
int len ;
uint16_t row_buf [ 5000 ] ;
for ( i = 0 ; i < s - > output_length ; i + + )
{
if ( ( len = image_translate_row ( s , ( uint8_t * ) row_buf , 6 * s - > output_width ) ) ! = 6 * s - > output_width )
{
printf ( " Image finished early - %d %d \n " , len , 6 * s - > output_width ) ;
exit ( 2 ) ;
}
if ( compare )
{
for ( j = 0 ; j < 50 ; j + + )
{
if ( row_buf [ 3 * j + 0 ] ! = j * 1200 | | row_buf [ 3 * j + 1 ] ! = j * 1200 | | row_buf [ 3 * j + 2 ] ! = j * 1200 )
{
printf ( " Image mismatch - %d - %d %d %d \n " , j , row_buf [ 3 * j + 0 ] , row_buf [ 3 * j + 1 ] , row_buf [ 3 * j + 2 ] ) ;
exit ( 2 ) ;
}
}
}
}
if ( ( len = image_translate_row ( s , ( uint8_t * ) row_buf , 6 * s - > output_width ) ) ! = 0 )
{
printf ( " Image finished late - %d %d \n " , len , 6 * s - > output_width ) ;
exit ( 2 ) ;
}
}
/*- End of function --------------------------------------------------------*/
2010-07-24 19:43:53 +00:00
static void dither_tests_gray16 ( void )
{
int i ;
int j ;
2012-08-08 13:26:06 +00:00
image_translate_state_t * s ;
2010-07-24 19:43:53 +00:00
uint16_t image [ 50 * 50 ] ;
image_descriptor_t im ;
printf ( " Dithering from a 16 bit per sample gray scale to bi-level \n " ) ;
im . image = ( const uint8_t * ) image ;
im . width = 50 ;
im . length = 50 ;
im . bytes_per_pixel = 2 ;
im . current_row = 0 ;
for ( i = 0 ; i < im . length ; i + + )
{
for ( j = 0 ; j < im . width ; j + + )
image [ i * im . width + j ] = j * 1200 ;
}
2012-08-08 13:26:06 +00:00
s = image_translate_init ( NULL , T4_IMAGE_TYPE_GRAY_12BIT , im . width , im . length , T4_IMAGE_TYPE_BILEVEL , - 1 , - 1 , row_read , & im ) ;
get_bilevel_image ( s , TRUE ) ;
image_translate_free ( s ) ;
2010-07-24 19:43:53 +00:00
}
/*- End of function --------------------------------------------------------*/
static void dither_tests_gray8 ( void )
{
int i ;
int j ;
2012-08-08 13:26:06 +00:00
image_translate_state_t * s ;
2010-07-24 19:43:53 +00:00
uint8_t image [ 50 * 50 ] ;
image_descriptor_t im ;
printf ( " Dithering from a 8 bit per sample gray scale to bi-level \n " ) ;
im . image = image ;
im . width = 50 ;
im . length = 50 ;
im . bytes_per_pixel = 1 ;
im . current_row = 0 ;
for ( i = 0 ; i < im . length ; i + + )
{
for ( j = 0 ; j < im . width ; j + + )
image [ i * im . width + j ] = j * 1200 / 256 ;
}
2012-08-08 13:26:06 +00:00
s = image_translate_init ( NULL , T4_IMAGE_TYPE_GRAY_8BIT , im . width , im . length , T4_IMAGE_TYPE_BILEVEL , - 1 , - 1 , row_read , & im ) ;
get_bilevel_image ( s , TRUE ) ;
image_translate_free ( s ) ;
2010-07-24 19:43:53 +00:00
}
/*- End of function --------------------------------------------------------*/
static void dither_tests_colour16 ( void )
{
int i ;
int j ;
2012-08-08 13:26:06 +00:00
image_translate_state_t * s ;
2010-07-24 19:43:53 +00:00
uint16_t image [ 50 * 50 * 3 ] ;
image_descriptor_t im ;
printf ( " Dithering from a 3x16 bit per sample colour to bi-level \n " ) ;
im . image = ( const uint8_t * ) image ;
im . width = 50 ;
im . length = 50 ;
im . bytes_per_pixel = 6 ;
im . current_row = 0 ;
for ( i = 0 ; i < im . length ; i + + )
{
for ( j = 0 ; j < im . width ; j + + )
{
2012-08-08 13:26:06 +00:00
image [ i * 3 * im . width + 3 * j + 0 ] = j * 1200 + i ;
image [ i * 3 * im . width + 3 * j + 1 ] = j * 1200 + i ;
image [ i * 3 * im . width + 3 * j + 2 ] = j * 1200 + i ;
}
}
s = image_translate_init ( NULL , T4_IMAGE_TYPE_COLOUR_12BIT , im . width , im . length , T4_IMAGE_TYPE_BILEVEL , - 1 , - 1 , row_read , & im ) ;
get_bilevel_image ( s , TRUE ) ;
printf ( " Scrunching from a 3x16 bit per sample colour to 8 bit per sample gray scale \n " ) ;
im . image = ( const uint8_t * ) image ;
im . width = 50 ;
im . length = 50 ;
im . bytes_per_pixel = 6 ;
im . current_row = 0 ;
for ( i = 0 ; i < im . length ; i + + )
{
for ( j = 0 ; j < im . width ; j + + )
{
image [ i * 3 * im . width + 3 * j + 0 ] = j * 1200 + i ;
image [ i * 3 * im . width + 3 * j + 1 ] = j * 1200 + i ;
image [ i * 3 * im . width + 3 * j + 2 ] = j * 1200 + i ;
2010-07-24 19:43:53 +00:00
}
}
2012-08-08 13:26:06 +00:00
s = image_translate_init ( s , T4_IMAGE_TYPE_COLOUR_12BIT , im . width , im . length , T4_IMAGE_TYPE_GRAY_8BIT , - 1 , - 1 , row_read , & im ) ;
get_gray8_image ( s , TRUE ) ;
printf ( " Scrunching from a 3x16 bit per sample colour to 16 bit per sample gray scale \n " ) ;
im . image = ( const uint8_t * ) image ;
im . width = 50 ;
im . length = 50 ;
im . bytes_per_pixel = 6 ;
im . current_row = 0 ;
for ( i = 0 ; i < im . length ; i + + )
{
for ( j = 0 ; j < im . width ; j + + )
{
image [ i * 3 * im . width + 3 * j + 0 ] = j * 1200 + i ;
image [ i * 3 * im . width + 3 * j + 1 ] = j * 1200 + i ;
image [ i * 3 * im . width + 3 * j + 2 ] = j * 1200 + i ;
}
}
s = image_translate_init ( s , T4_IMAGE_TYPE_COLOUR_12BIT , im . width , im . length , T4_IMAGE_TYPE_GRAY_12BIT , - 1 , - 1 , row_read , & im ) ;
get_gray16_image ( s , TRUE ) ;
printf ( " Scrunching from a 3x16 bit per sample colour to 3x8 bit per sample colour \n " ) ;
im . image = ( const uint8_t * ) image ;
im . width = 50 ;
im . length = 50 ;
im . bytes_per_pixel = 6 ;
im . current_row = 0 ;
for ( i = 0 ; i < im . length ; i + + )
{
for ( j = 0 ; j < im . width ; j + + )
{
image [ i * 3 * im . width + 3 * j + 0 ] = j * 1200 + i ;
image [ i * 3 * im . width + 3 * j + 1 ] = j * 1200 + i ;
image [ i * 3 * im . width + 3 * j + 2 ] = j * 1200 + i ;
}
}
s = image_translate_init ( s , T4_IMAGE_TYPE_COLOUR_12BIT , im . width , im . length , T4_IMAGE_TYPE_COLOUR_8BIT , - 1 , - 1 , row_read , & im ) ;
get_colour8_image ( s , TRUE ) ;
printf ( " Scrunching from a 3x16 bit per sample colour to 3x16 bit per sample colour \n " ) ;
im . image = ( const uint8_t * ) image ;
im . width = 50 ;
im . length = 50 ;
im . bytes_per_pixel = 6 ;
im . current_row = 0 ;
for ( i = 0 ; i < im . length ; i + + )
{
for ( j = 0 ; j < im . width ; j + + )
{
image [ i * 3 * im . width + 3 * j + 0 ] = j * 1200 + i ;
image [ i * 3 * im . width + 3 * j + 1 ] = j * 1200 + i ;
image [ i * 3 * im . width + 3 * j + 2 ] = j * 1200 + i ;
}
}
s = image_translate_init ( s , T4_IMAGE_TYPE_COLOUR_12BIT , im . width , im . length , T4_IMAGE_TYPE_COLOUR_12BIT , - 1 , - 1 , row_read , & im ) ;
get_colour16_image ( s , TRUE ) ;
image_translate_free ( s ) ;
2010-07-24 19:43:53 +00:00
}
/*- End of function --------------------------------------------------------*/
static void dither_tests_colour8 ( void )
{
int i ;
int j ;
2012-08-08 13:26:06 +00:00
image_translate_state_t * s ;
2010-07-24 19:43:53 +00:00
uint8_t image [ 50 * 50 * 3 ] ;
image_descriptor_t im ;
printf ( " Dithering from a 3x8 bit per sample colour to bi-level \n " ) ;
im . image = image ;
im . width = 50 ;
im . length = 50 ;
im . bytes_per_pixel = 3 ;
im . current_row = 0 ;
for ( i = 0 ; i < im . length ; i + + )
{
for ( j = 0 ; j < im . width ; j + + )
{
2012-08-08 13:26:06 +00:00
image [ i * 3 * im . width + 3 * j + 0 ] = j * 1200 / 256 + i ;
image [ i * 3 * im . width + 3 * j + 1 ] = j * 1200 / 256 + i ;
image [ i * 3 * im . width + 3 * j + 2 ] = j * 1200 / 256 + i ;
2010-07-24 19:43:53 +00:00
}
}
2012-08-08 13:26:06 +00:00
s = image_translate_init ( NULL , T4_IMAGE_TYPE_COLOUR_8BIT , im . width , im . length , T4_IMAGE_TYPE_BILEVEL , - 1 , - 1 , row_read , & im ) ;
get_bilevel_image ( s , TRUE ) ;
image_translate_free ( s ) ;
2010-07-24 19:43:53 +00:00
}
/*- End of function --------------------------------------------------------*/
static void grow_tests_colour8 ( void )
{
int i ;
int j ;
2012-08-08 13:26:06 +00:00
image_translate_state_t * s ;
2010-07-24 19:43:53 +00:00
uint8_t image [ 50 * 50 * 3 ] ;
image_descriptor_t im ;
printf ( " Image growth tests \n " ) ;
im . image = image ;
im . width = 50 ;
im . length = 50 ;
im . bytes_per_pixel = 3 ;
im . current_row = 0 ;
for ( i = 0 ; i < im . length ; i + + )
{
for ( j = 0 ; j < im . width ; j + + )
{
2012-08-08 13:26:06 +00:00
image [ i * 3 * im . width + 3 * j + 0 ] = j * 1200 / 256 + i ;
image [ i * 3 * im . width + 3 * j + 1 ] = j * 1200 / 256 + i ;
image [ i * 3 * im . width + 3 * j + 2 ] = j * 1200 / 256 + i ;
2010-07-24 19:43:53 +00:00
}
}
2012-08-08 13:26:06 +00:00
s = image_translate_init ( NULL , T4_IMAGE_TYPE_COLOUR_8BIT , im . width , im . length , T4_IMAGE_TYPE_BILEVEL , 200 , - 1 , row_read , & im ) ;
get_bilevel_image ( s , FALSE ) ;
image_translate_free ( s ) ;
2010-07-24 19:43:53 +00:00
}
/*- End of function --------------------------------------------------------*/
2012-08-03 19:45:09 +00:00
static void lenna_tests ( int output_width , int output_length_scaling , const char * file )
2010-07-24 19:43:53 +00:00
{
TIFF * in_file ;
TIFF * out_file ;
int image_width ;
int image_length ;
int output_length ;
uint8_t * image ;
uint8_t * image2 ;
int len ;
int total ;
int16_t bits_per_sample ;
int16_t samples_per_pixel ;
int i ;
int n ;
2012-08-08 13:26:06 +00:00
image_translate_state_t * s ;
2010-07-24 19:43:53 +00:00
image_descriptor_t im ;
2012-08-03 19:45:09 +00:00
float x_resolution ;
float y_resolution ;
uint16_t res_unit ;
2010-07-24 19:43:53 +00:00
printf ( " Dithering Lenna from colour to bi-level test \n " ) ;
if ( ( in_file = TIFFOpen ( INPUT_TIFF_FILE_NAME , " r " ) ) = = NULL )
return ;
image_width = 0 ;
TIFFGetField ( in_file , TIFFTAG_IMAGEWIDTH , & image_width ) ;
if ( image_width < = 0 )
return ;
image_length = 0 ;
TIFFGetField ( in_file , TIFFTAG_IMAGELENGTH , & image_length ) ;
if ( image_length < = 0 )
return ;
2012-08-03 19:45:09 +00:00
x_resolution = 200.0 ;
TIFFGetField ( in_file , TIFFTAG_XRESOLUTION , & x_resolution ) ;
y_resolution = 200.0 ;
TIFFGetField ( in_file , TIFFTAG_YRESOLUTION , & y_resolution ) ;
res_unit = RESUNIT_INCH ;
2012-08-08 13:26:06 +00:00
TIFFGetField ( in_file , TIFFTAG_RESOLUTIONUNIT , & res_unit ) ;
2010-07-24 19:43:53 +00:00
bits_per_sample = 0 ;
TIFFGetField ( in_file , TIFFTAG_BITSPERSAMPLE , & bits_per_sample ) ;
samples_per_pixel = 0 ;
TIFFGetField ( in_file , TIFFTAG_SAMPLESPERPIXEL , & samples_per_pixel ) ;
2012-08-03 19:45:09 +00:00
printf ( " Original image is %d x %d, %f x %f resolution, %d bits per sample, %d samples per pixel \n " , image_width , image_length , x_resolution , y_resolution , bits_per_sample , samples_per_pixel ) ;
2010-07-24 19:43:53 +00:00
if ( ( image = malloc ( image_width * image_length * samples_per_pixel ) ) = = NULL )
return ;
for ( total = 0 , i = 0 ; i < 1000 ; i + + )
{
len = TIFFReadEncodedStrip ( in_file , i , & image [ total ] , image_width * image_length * samples_per_pixel - total ) ;
if ( len < = 0 )
break ;
total + = len ;
if ( total = = image_width * image_length * samples_per_pixel )
{
printf ( " Done \n " ) ;
break ;
}
}
printf ( " Image size %d %d \n " , total , image_width * image_length * samples_per_pixel ) ;
TIFFClose ( in_file ) ;
2012-08-03 19:45:09 +00:00
if ( output_length_scaling > 0 )
output_length = ( double ) image_length * output_length_scaling * output_width / image_width ;
else
output_length = - 1 ;
2010-07-24 19:43:53 +00:00
im . image = image ;
im . width = image_width ;
im . length = image_length ;
im . current_row = 0 ;
im . bytes_per_pixel = samples_per_pixel ;
2012-08-08 13:26:06 +00:00
s = image_translate_init ( NULL , T4_IMAGE_TYPE_COLOUR_8BIT , image_width , image_length , T4_IMAGE_TYPE_BILEVEL , output_width , output_length , row_read , & im ) ;
2010-07-24 19:43:53 +00:00
output_width = image_translate_get_output_width ( s ) ;
output_length = image_translate_get_output_length ( s ) ;
if ( ( out_file = TIFFOpen ( file , " w " ) ) = = NULL )
return ;
TIFFSetField ( out_file , TIFFTAG_IMAGEWIDTH , output_width ) ;
TIFFSetField ( out_file , TIFFTAG_IMAGELENGTH , output_length ) ;
2012-08-03 19:45:09 +00:00
TIFFSetField ( out_file , TIFFTAG_XRESOLUTION , x_resolution ) ;
if ( output_length_scaling > 0 )
y_resolution * = output_length_scaling ;
TIFFSetField ( out_file , TIFFTAG_YRESOLUTION , y_resolution ) ;
TIFFSetField ( out_file , TIFFTAG_RESOLUTIONUNIT , res_unit ) ;
2010-07-24 19:43:53 +00:00
TIFFSetField ( out_file , TIFFTAG_BITSPERSAMPLE , 1 ) ;
TIFFSetField ( out_file , TIFFTAG_ORIENTATION , ORIENTATION_TOPLEFT ) ;
TIFFSetField ( out_file , TIFFTAG_SAMPLESPERPIXEL , 1 ) ;
TIFFSetField ( out_file , TIFFTAG_ROWSPERSTRIP , - 1 ) ;
TIFFSetField ( out_file , TIFFTAG_PLANARCONFIG , PLANARCONFIG_CONTIG ) ;
TIFFSetField ( out_file , TIFFTAG_PHOTOMETRIC , PHOTOMETRIC_MINISWHITE ) ;
TIFFSetField ( out_file , TIFFTAG_FILLORDER , FILLORDER_LSB2MSB ) ;
TIFFSetField ( out_file , TIFFTAG_PAGENUMBER , 0 , 1 ) ;
TIFFSetField ( out_file , TIFFTAG_PHOTOMETRIC , PHOTOMETRIC_MINISWHITE ) ;
TIFFSetField ( out_file , TIFFTAG_FILLORDER , FILLORDER_LSB2MSB ) ;
printf ( " Input %d x %d, output %d x %d \n " , image_width , image_length , output_width , output_length ) ;
if ( ( image2 = malloc ( output_width * output_length / 8 ) ) = = NULL )
return ;
memset ( image2 , 0 , output_width * output_length / 8 ) ;
n = 0 ;
for ( i = 0 ; i < output_length ; i + + )
n + = image_translate_row ( s , & image2 [ n ] , output_width / 8 ) ;
TIFFWriteEncodedStrip ( out_file , 0 , image2 , output_width * output_length / 8 ) ;
TIFFWriteDirectory ( out_file ) ;
TIFFClose ( out_file ) ;
2012-08-08 13:26:06 +00:00
image_translate_free ( s ) ;
2010-07-24 19:43:53 +00:00
}
/*- End of function --------------------------------------------------------*/
int main ( int argc , char * * argv )
{
# if 1
dither_tests_gray16 ( ) ;
dither_tests_gray8 ( ) ;
dither_tests_colour16 ( ) ;
dither_tests_colour8 ( ) ;
# endif
# if 1
grow_tests_colour8 ( ) ;
# endif
# if 1
2012-08-03 19:45:09 +00:00
lenna_tests ( 0 , 0 , " lenna-bw.tif " ) ;
lenna_tests ( 200 , 0 , " lenna-bw-200.tif " ) ;
lenna_tests ( 1728 , 0 , " lenna-bw-1728.tif " ) ;
lenna_tests ( 1728 , 2 , " lenna-bw-1728-superfine.tif " ) ;
2010-07-24 19:43:53 +00:00
# endif
printf ( " Tests passed. \n " ) ;
return 0 ;
}
/*- End of function --------------------------------------------------------*/
/*- End of file ------------------------------------------------------------*/