Sisyphus repository
Last update: 1 october 2023 | SRPMs: 18631 | Visits: 37708261
en ru br
ALT Linux repos
S:1.20-alt1

Group :: Graphics
RPM: hfalg

 Main   Changelog   Spec   Patches   Sources   Download   Gear   Bugs and FR  Repocop 

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <jpeglib.h>
#include <jerror.h>
#include <assert.h>
#include <fcntl.h>
#include <errno.h>
#include <math.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

/*
** hfalg means Hacker Factor Algorithms
** See http://www.hackerfactor.com/papers/bh-usa-07-krawetz-wp.pdf
**
** This version is by William D. Colburn
** <schlake+hfalg@nmt.edu>
**
** $Id: hfalg.c,v 1.20 2010/10/08 04:23:00 schlake Exp schlake $
*/

/*
** TO COMPILE: -ljpeg -lm
*/

int vectorLength( int red, int green, int blue )
{
double r = red;
double g = green;
double b = blue;

return( (int) sqrt( r*r + g*g + b*b ) );
}

int usage( char **argv )
{
fprintf( stderr, "usage:\n %s <something> [<jpeg>]\n", argv[0] );
fprintf( stderr, " <something>:\n" );
fprintf( stderr, " ela: Error Level Analysis\n" );
fprintf( stderr, " minmax1\n" );
fprintf( stderr, " minmax2\n" );
fprintf( stderr, " minmax3\n" );
fprintf( stderr, " minmax4\n" );
fprintf( stderr, " minmax5\n" );
fprintf( stderr, " mincolor\n" );
fprintf( stderr, " midcolor\n" );
fprintf( stderr, " maxcolor\n" );
fprintf( stderr, " avgdist\n" );
fprintf( stderr, " pnm\n" );
}

int forkBuffer( int fd0[2], int fd1[2] )
{
char *acBuffer;
int i;
size_t iBufSize = 8192 * 128;
size_t iBytesRead = 0;
size_t iTotalRead = 0;
pid_t pid;

pid = fork();
if (pid < 0 )
{
perror( "forkBuffer(): fork()" );
exit( 1 );
}
if (pid > 0)
{
return( 0 );
}

close( fd0[1] );
close( fd1[0] );

acBuffer = malloc( iBufSize * sizeof( char ));

if (!acBuffer)
{
perror( "forkBuffer(): malloc()" );
exit( 1 );
}

memset( acBuffer, 0x00, iBufSize * sizeof( char ) );

if (fcntl( fd0[0], F_SETFL, O_NONBLOCK ))
perror( "forkBuffer(): fcntl()" );

while ((iBytesRead = read( fd0[0], &acBuffer[iTotalRead], sizeof( char ) * (iBufSize - iTotalRead) )))
{
if (-1 == iBytesRead)
continue;

iTotalRead += iBytesRead;
if (iBytesRead == iBufSize)
{
char *acNew = (char *)realloc( acBuffer, iBufSize * 2 * sizeof( char ));

if (acNew)
{
acBuffer = acNew;
iBufSize *= 2;
}
else
{
perror( "forkBuffer(): realloc()" );
exit( 0 );
}
}
}

write( fd1[1], acBuffer, sizeof( char ) * iTotalRead );
close( fd1[1] );

exit( 0 );
}

int decompressJpeg( FILE *fp, int *piWidth, int *piHeight, JSAMPARRAY *prBuf )
{
struct jpeg_decompress_struct rDecompressOrigInfo;
struct jpeg_error_mgr rDecompressOrigError;
JSAMPARRAY ppBuffer0;

rDecompressOrigInfo.err = jpeg_std_error( &rDecompressOrigError );
jpeg_create_decompress( &rDecompressOrigInfo );

if (fp)
jpeg_stdio_src( &rDecompressOrigInfo, fp );
else
jpeg_stdio_src( &rDecompressOrigInfo, stdin );

jpeg_read_header( &rDecompressOrigInfo, TRUE );

/*
** set parameters for decompression eventually
*/

jpeg_start_decompress( &rDecompressOrigInfo );

*piHeight = rDecompressOrigInfo.output_height;
*piWidth = rDecompressOrigInfo.output_width;

ppBuffer0 = (*rDecompressOrigInfo.mem->alloc_sarray)((j_common_ptr) &rDecompressOrigInfo, JPOOL_PERMANENT, rDecompressOrigInfo.output_width * rDecompressOrigInfo.output_components, *piHeight );

while (rDecompressOrigInfo.output_scanline < rDecompressOrigInfo.image_height )
jpeg_read_scanlines( &rDecompressOrigInfo, ppBuffer0 + rDecompressOrigInfo.output_scanline, 1 );

/*
jpeg_finish_decompress( &rDecompressOrigInfo );
*/

*prBuf = ppBuffer0;
return( 0 );
}

/*
** minmax1
** RGB of the image where:
** r is 255 if r >= g or r >= b
** g is 255 if g >= r or g >= b
** b is 255 if b >= r or b >= g
*/
int minmax1( int argc, char **argv )
{
JSAMPARRAY ppBuffer0;

FILE *fpJpeg = NULL;

int iHeight;
int iWidth;
int iRow;
int iCol;

if (argv[2])
{
if ((fpJpeg = fopen( argv[2], "rb" )) == NULL )
{
perror( "ela(): fopen()" );
return( 1 );
}
}
else
fpJpeg = stdin;

decompressJpeg( fpJpeg, &iWidth, &iHeight, &ppBuffer0 );

fprintf( stdout, "P3\n%d %d\n255\n", iWidth, iHeight );

#define RED(x,y) (GETJSAMPLE( ppBuffer0[(x)][(y) * 3] ))
#define GREEN(x,y) (GETJSAMPLE( ppBuffer0[(x)][(y) * 3 + 1]))
#define BLUE(x,y) (GETJSAMPLE( ppBuffer0[(x)][(y) * 3 + 2]))

for (iRow = 0; iRow < iHeight; iRow++ )
{
for (iCol = 0; iCol < iWidth; iCol++ )
{
int r = RED( iRow, iCol );
int g = GREEN( iRow, iCol );
int b = BLUE( iRow, iCol );
int red = 255 * (r>=g && r>=b);
int green = 255 * (g>=r && g>=b);
int blue = 255 * (b>=r && b>=g);

fprintf( stdout, "%d %d %d\n", red, green, blue );
}
}

#undef RED
#undef GREEN
#undef BLUE

}


/*
** minmax5
** RGB of the image where:
** r is r if r >= g or r >= b
** g is g if g >= r or g >= b
** b is b if b >= r or b >= g
*/
int minmax5( int argc, char **argv )
{
JSAMPARRAY ppBuffer0;

FILE *fpJpeg = NULL;

int iHeight;
int iWidth;
int iRow;
int iCol;

if (argv[2])
{
if ((fpJpeg = fopen( argv[2], "rb" )) == NULL )
{
perror( "ela(): fopen()" );
return( 1 );
}
}
else
fpJpeg = stdin;

decompressJpeg( fpJpeg, &iWidth, &iHeight, &ppBuffer0 );

fprintf( stdout, "P3\n%d %d\n255\n", iWidth, iHeight );

#define RED(x,y) (GETJSAMPLE( ppBuffer0[(x)][(y) * 3] ))
#define GREEN(x,y) (GETJSAMPLE( ppBuffer0[(x)][(y) * 3 + 1]))
#define BLUE(x,y) (GETJSAMPLE( ppBuffer0[(x)][(y) * 3 + 2]))

for (iRow = 0; iRow < iHeight; iRow++ )
{
for (iCol = 0; iCol < iWidth; iCol++ )
{
int r = RED( iRow, iCol );
int g = GREEN( iRow, iCol );
int b = BLUE( iRow, iCol );
int red = 0;
int green = 0;
int blue = 0;
if (r>=g && r>=b)
red = r;
if (g>=r && g>=b)
green = g;
if (b>=r && b>=g)
blue = b;

fprintf( stdout, "%d %d %d\n", red, green, blue );
}
}

#undef RED
#undef GREEN
#undef BLUE

}

int minmax3( int argc, char **argv )
{
JSAMPARRAY ppBuffer0;

FILE *fpJpeg = NULL;

int iHeight;
int iWidth;
int iRow;
int iCol;

if (argv[2])
{
if ((fpJpeg = fopen( argv[2], "rb" )) == NULL )
{
perror( "ela(): fopen()" );
return( 1 );
}
}
else
fpJpeg = stdin;

decompressJpeg( fpJpeg, &iWidth, &iHeight, &ppBuffer0 );

fprintf( stdout, "P3\n%d %d\n255\n", iWidth, iHeight );

#define RED(x,y) (GETJSAMPLE( ppBuffer0[(x)][(y) * 3] ))
#define GREEN(x,y) (GETJSAMPLE( ppBuffer0[(x)][(y) * 3 + 1]))
#define BLUE(x,y) (GETJSAMPLE( ppBuffer0[(x)][(y) * 3 + 2]))

for (iRow = 0; iRow < iHeight; iRow++ )
{
for (iCol = 0; iCol < iWidth; iCol++ )
{
int r = RED( iRow, iCol );
int g = GREEN( iRow, iCol );
int b = BLUE( iRow, iCol );
int red = 0;
int green = 0;
int blue = 0;
int mag = vectorLength( r, g, b );
int nhigh = 0;
int nlow = 255;
int ntmp;

if (iCol > 0)
{
int itmp = vectorLength( RED( iRow, iCol - 1 ), GREEN( iRow, iCol - 1 ), BLUE( iRow, iCol - 1 ) );

if (itmp > nhigh)
nhigh = itmp;
if (itmp < nlow)
nlow = itmp;
}
if ((iCol + 2) < iWidth)
{
int itmp = vectorLength( RED( iRow, iCol - 1 ), GREEN( iRow, iCol - 1 ), BLUE( iRow, iCol - 1 ) );

if (itmp > nhigh)
nhigh = itmp;
if (itmp < nlow)
nlow = itmp;
}
if (iRow > 0)
{
int itmp = vectorLength( RED( iRow - 1, iCol ), GREEN( iRow - 1, iCol ), BLUE( iRow - 1, iCol ) );

if (itmp > nhigh)
nhigh = itmp;
if (itmp < nlow)
nlow = itmp;
}
if ((iRow + 2) < iHeight)
{
int itmp = vectorLength( RED( iRow + 1, iCol ), GREEN( iRow + 1, iCol ), BLUE( iRow + 1, iCol ) );

if (itmp > nhigh)
nhigh = itmp;
if (itmp < nlow)
nlow = itmp;
}

if (mag > nhigh)
{
red = 255;
green = 255;
blue = 0;
}
if (mag < nlow)
{
red = 0;
green = 255;
blue = 255;
}

fprintf( stdout, "%d %d %d\n", red, green, blue );
}
}

#undef RED
#undef GREEN
#undef BLUE
}

int minmax4( int argc, char **argv )
{
JSAMPARRAY ppBuffer0;

FILE *fpJpeg = NULL;

int iHeight;
int iWidth;
int iRow;
int iCol;

if (argv[2])
{
if ((fpJpeg = fopen( argv[2], "rb" )) == NULL )
{
perror( "ela(): fopen()" );
return( 1 );
}
}
else
fpJpeg = stdin;

decompressJpeg( fpJpeg, &iWidth, &iHeight, &ppBuffer0 );

fprintf( stdout, "P3\n%d %d\n255\n", iWidth, iHeight );

#define RED(x,y) (GETJSAMPLE( ppBuffer0[(x)][(y) * 3] ))
#define GREEN(x,y) (GETJSAMPLE( ppBuffer0[(x)][(y) * 3 + 1]))
#define BLUE(x,y) (GETJSAMPLE( ppBuffer0[(x)][(y) * 3 + 2]))

for (iRow = 0; iRow < iHeight; iRow++ )
{
for (iCol = 0; iCol < iWidth; iCol++ )
{
int r = RED( iRow, iCol );
int g = GREEN( iRow, iCol );
int b = BLUE( iRow, iCol );
int red = 0;
int green = 0;
int blue = 0;
int mag = vectorLength( r, g, b );
int nhigh = 0;
int nlow = 255;
int ntmp;
int n[4][3];

if ((iCol > 0) && ((iCol + 2) < iWidth) && (iRow > 0) && ((iRow + 2) < iHeight))
{
n[0][0] = RED( iRow, iCol - 1 );
n[0][1] = GREEN( iRow, iCol - 1 );
n[0][2] = BLUE( iRow, iCol - 1 );

n[1][0] = RED( iRow, iCol + 1 );
n[1][1] = GREEN( iRow, iCol + 1 );
n[1][2] = BLUE( iRow, iCol + 1 );

n[2][0] = RED( iRow - 1, iCol );
n[2][1] = GREEN( iRow - 1, iCol );
n[2][2] = BLUE( iRow - 1, iCol );

n[3][0] = RED( iRow + 1, iCol );
n[3][1] = GREEN( iRow + 1, iCol );
n[3][2] = BLUE( iRow + 1, iCol );

#define MAX( c, o1, o2 ) ((c>o1)&&(c>o2)&&(c>n[0][0])&&(c>n[0][1])&&(c>n[0][2])&&(c>n[1][0])&&(c>n[1][1])&&(c>n[1][2])&&(c>n[2][0])&&(c>n[2][1])&&(c>n[2][2])&&(c>n[3][0])&&(c>n[3][1])&&(c>n[3][2]))

if (MAX( r, g, b ))
red = 255;
if (MAX( g, r, b ))
green = 255;
if (MAX( b, r, g ))
blue = 255;
}
fprintf( stdout, "%d %d %d\n", red, green, blue );
}
}

#undef RED
#undef GREEN
#undef BLUE

}


int minmax2( int argc, char **argv )
{
JSAMPARRAY ppBuffer0;

FILE *fpJpeg = NULL;

int iHeight;
int iWidth;
int iRow;
int iCol;

if (argv[2])
{
if ((fpJpeg = fopen( argv[2], "rb" )) == NULL )
{
perror( "ela(): fopen()" );
return( 1 );
}
}
else
fpJpeg = stdin;

decompressJpeg( fpJpeg, &iWidth, &iHeight, &ppBuffer0 );

fprintf( stdout, "P3\n%d %d\n255\n", iWidth, iHeight );

#define RED(x,y) (GETJSAMPLE( ppBuffer0[(x)][(y) * 3] ))
#define GREEN(x,y) (GETJSAMPLE( ppBuffer0[(x)][(y) * 3 + 1]))
#define BLUE(x,y) (GETJSAMPLE( ppBuffer0[(x)][(y) * 3 + 2]))

for (iRow = 0; iRow < iHeight; iRow++ )
{
for (iCol = 0; iCol < iWidth; iCol++ )
{
int r = RED( iRow, iCol );
int g = GREEN( iRow, iCol );
int b = BLUE( iRow, iCol );
int red = 255;
int green = 255;
int blue = 255;

if (iCol > 0)
{
if (RED( iRow, iCol - 1 ) > r)
red = 0;
if (GREEN( iRow, iCol - 1 ) > g)
green = 0;
if (BLUE( iRow, iCol - 1 ) > b)
blue = 0;
}
if ((iCol + 2) < iWidth)
{
if (RED( iRow, iCol - 1 ) > r)
red = 0;
if (GREEN( iRow, iCol - 1 ) > g)
green = 0;
if (BLUE( iRow, iCol - 1 ) > b)
blue = 0;
}
if (iRow > 0)
{
if (RED( iRow - 1, iCol ) > r)
red = 0;
if (GREEN( iRow - 1, iCol ) > g)
green = 0;
if (BLUE( iRow - 1, iCol ) > b)
blue = 0;
}
if ((iRow + 2) < iHeight)
{
if (RED( iRow + 1, iCol ) > r)
red = 0;
if (GREEN( iRow + 1, iCol ) > g)
green = 0;
if (BLUE( iRow + 1, iCol ) > b)
blue = 0;
}

if (red > green && red > blue)
{
green = 0;
blue = 0;
}
if (green > red && green > blue)
{
red = 0;
blue = 0;
}
if (blue > green && blue > red)
{
red = 0;
green = 0;
}

fprintf( stdout, "%d %d %d\n", red, green, blue );
}
}

#undef RED
#undef GREEN
#undef BLUE

}




int pnm( int argc, char **argv )
{
JSAMPARRAY ppBuffer0;

FILE *fpJpeg = NULL;

int iHeight;
int iWidth;
int iRow;
int iCol;

if (argv[2])
{
if ((fpJpeg = fopen( argv[2], "rb" )) == NULL )
{
perror( "ela(): fopen()" );
return( 1 );
}
}
else
fpJpeg = stdin;

decompressJpeg( fpJpeg, &iWidth, &iHeight, &ppBuffer0 );

fprintf( stdout, "P3\n%d %d\n255\n", iWidth, iHeight );

#define RED(x,y) (GETJSAMPLE( ppBuffer0[(x)][(y) * 3] ))
#define GREEN(x,y) (GETJSAMPLE( ppBuffer0[(x)][(y) * 3 + 1]))
#define BLUE(x,y) (GETJSAMPLE( ppBuffer0[(x)][(y) * 3 + 2]))

for (iRow = 0; iRow < iHeight; iRow++ )
{
for (iCol = 0; iCol < iWidth; iCol++ )
{
fprintf( stdout, "%d %d %d\n", RED( iRow, iCol ), GREEN( iRow, iCol ), BLUE( iRow, iCol ) );
}
}

#undef RED
#undef GREEN
#undef BLUE
return( 0 );
}


int avgdist( int argc, char **argv )
{
JSAMPARRAY ppBuffer0;

FILE *fpJpeg = NULL;

int iHeight;
int iWidth;
int iRow;
int iCol;

int iScale = 4;

if (argv[2])
{
if ((fpJpeg = fopen( argv[2], "rb" )) == NULL )
{
perror( "ela(): fopen()" );
return( 1 );
}
}
else
fpJpeg = stdin;

decompressJpeg( fpJpeg, &iWidth, &iHeight, &ppBuffer0 );

fprintf( stdout, "P3\n%d %d\n255\n", iWidth, iHeight );

for (iRow = 0; iRow < iHeight; iRow++ )
{
for (iCol = 0; iCol < iWidth; iCol++ )
{
int div = 0;

int red = 0;
int green = 0;
int blue = 0;

#define RED(x,y) (GETJSAMPLE( ppBuffer0[(x)][(y) * 3] ))
#define GREEN(x,y) (GETJSAMPLE( ppBuffer0[(x)][(y) * 3] + 1))
#define BLUE(x,y) (GETJSAMPLE( ppBuffer0[(x)][(y) * 3] + 2))

if (iCol > 0)
{
red += RED( iRow, iCol - 1 );
green += GREEN( iRow, iCol - 1 );
blue += BLUE( iRow, iCol - 1 );
div++;
}
if ((iCol + 2) < iWidth)
{
red += RED( iRow, iCol - 1 );
green += GREEN( iRow, iCol - 1 );
blue += BLUE( iRow, iCol - 1 );
div++;
}
if (iRow > 0)
{
red += RED( iRow - 1, iCol );
green += GREEN( iRow - 1, iCol );
blue += BLUE( iRow - 1, iCol );
div++;
}
if ((iRow + 2) < iHeight)
{
red += RED( iRow + 1, iCol );
green += GREEN( iRow + 1, iCol );
blue += BLUE( iRow + 1, iCol );
div++;
}

fprintf( stdout, "%d %d %d\n",
iScale * abs( RED( iRow, iCol ) - (red / div) ),
iScale * abs( GREEN( iRow, iCol ) - (green / div) ),
iScale * abs( BLUE( iRow, iCol ) - (blue / div) )
);
#undef RED
#undef GREEN
#undef BLUE

}
}
return( 0 );
}

int ela( int argc, char **argv )
{
struct jpeg_decompress_struct rDecompressOrigInfo;
struct jpeg_error_mgr rDecompressOrigError;
JSAMPARRAY ppBuffer0;
JSAMPARRAY ppBuffer1;

struct jpeg_compress_struct rCompressInfo;
struct jpeg_error_mgr rCompressError;

struct jpeg_decompress_struct rDecompressNewInfo;
struct jpeg_error_mgr rDecompressNewError;

FILE *fpJpeg = NULL;

int aiPipe0[2];
int aiPipe1[2];

FILE *fpPipe0W = NULL;
FILE *fpPipe1R = NULL;

int i;

int iHeight;
int iWidth;
int iRow;
int iCol;

if (pipe( aiPipe0 ))
{
perror( "ela(): pipe()" );
return( 1 );
}

if (pipe( aiPipe1 ))
{
perror( "ela(): pipe()" );
return( 1 );
}

if (forkBuffer( aiPipe0, aiPipe1 ))
return( 1 );

close( aiPipe0[0] );
close( aiPipe1[1] );
fpPipe0W = fdopen( aiPipe0[1], "wb" );
fpPipe1R = fdopen( aiPipe1[0], "rb" );

/*
** Code below here
*/
rDecompressOrigInfo.err = jpeg_std_error( &rDecompressOrigError );
jpeg_create_decompress( &rDecompressOrigInfo );

if (argv[2])
{
if ((fpJpeg = fopen( argv[2], "rb" )) == NULL )
{
perror( "ela(): fopen()" );
return( 1 );
}
jpeg_stdio_src( &rDecompressOrigInfo, fpJpeg );
}
else
{
jpeg_stdio_src( &rDecompressOrigInfo, stdin );
}

jpeg_read_header( &rDecompressOrigInfo, TRUE );

/*
** set parameters for decompression eventually
*/

jpeg_start_decompress( &rDecompressOrigInfo );

iHeight = rDecompressOrigInfo.output_height;
iWidth = rDecompressOrigInfo.output_width;

ppBuffer0 = (*rDecompressOrigInfo.mem->alloc_sarray)((j_common_ptr) &rDecompressOrigInfo, JPOOL_PERMANENT, rDecompressOrigInfo.output_width * rDecompressOrigInfo.output_components, iHeight );

rCompressInfo.err = jpeg_std_error( &rCompressError );

jpeg_create_compress( &rCompressInfo );

rCompressInfo.image_width = rDecompressOrigInfo.output_width;
rCompressInfo.image_height = rDecompressOrigInfo.output_height;
rCompressInfo.input_components = rDecompressOrigInfo.output_components;
rCompressInfo.in_color_space = rDecompressOrigInfo.out_color_space;

jpeg_set_defaults( &rCompressInfo );

jpeg_stdio_dest( &rCompressInfo, fpPipe0W );

jpeg_set_quality( &rCompressInfo, 80, TRUE );

jpeg_start_compress( &rCompressInfo, TRUE );

/*
fprintf( stderr, "width: %d versus %d\n", rCompressInfo.image_width, rDecompressOrigInfo.output_width );
fprintf( stderr, "height: %d versus %d\n", rCompressInfo.image_height, rDecompressOrigInfo.output_height );
*/

while (rDecompressOrigInfo.output_scanline < rDecompressOrigInfo.image_height )
jpeg_read_scanlines( &rDecompressOrigInfo, ppBuffer0 + rDecompressOrigInfo.output_scanline, 1 );

jpeg_write_scanlines( &rCompressInfo, ppBuffer0, iHeight );

jpeg_finish_decompress( &rDecompressOrigInfo );
jpeg_finish_compress( &rCompressInfo );
fclose( fpPipe0W );

/*
fprintf( stderr, "Decompress warnings: %d\n", rDecompressOrigError.num_warnings );
fprintf( stderr, "Compress warnings: %d\n", rCompressError.num_warnings );
*/

/*
** Here I need to decompress again
*/
rDecompressNewInfo.err = jpeg_std_error( &rDecompressNewError );
jpeg_create_decompress( &rDecompressNewInfo );
jpeg_stdio_src( &rDecompressNewInfo, fpPipe1R );
jpeg_read_header( &rDecompressNewInfo, TRUE );
jpeg_start_decompress( &rDecompressNewInfo );

ppBuffer1 = (*rDecompressNewInfo.mem->alloc_sarray)((j_common_ptr) &rDecompressNewInfo, JPOOL_PERMANENT, rDecompressNewInfo.output_width * rDecompressNewInfo.output_components, iHeight );

while (rDecompressNewInfo.output_scanline < rDecompressNewInfo.image_height )
jpeg_read_scanlines( &rDecompressNewInfo, ppBuffer1 + rDecompressNewInfo.output_scanline, 1 );

/*
** So at this point I should have two buffers full of data to compare.
*/

fprintf( stdout, "P3\n%d %d\n255\n", iWidth, iHeight );

for (iRow = 0; iRow < iHeight; iRow++ )
{
for (iCol = 0; iCol < iWidth; iCol++ )
{
/*
fprintf( stdout, "%d %d %d\n",
GETJSAMPLE( ppBuffer0[iRow][(iCol * 3)] ),
GETJSAMPLE( ppBuffer0[iRow][(iCol * 3) + 1] ),
GETJSAMPLE( ppBuffer0[iRow][(iCol * 3) + 2] ) );
*/
int red = abs( GETJSAMPLE( ppBuffer0[iRow][(iCol * 3)] ) - GETJSAMPLE( ppBuffer1[iRow][(iCol * 3)] ) );
int green = abs( GETJSAMPLE( ppBuffer0[iRow][(iCol * 3) + 1] ) - GETJSAMPLE( ppBuffer1[iRow][(iCol * 3) + 1] ) );
int blue = abs( GETJSAMPLE( ppBuffer0[iRow][(iCol * 3) + 2] ) - GETJSAMPLE( ppBuffer1[iRow][(iCol * 3) + 2] ) );

if ( (red + green + blue) > 10 )
fprintf( stdout, "%d %d %d\n", red * 20, green * 20, blue * 20 );
else
fprintf( stdout, "0 0 0\n" );
}
}

return( 0 );
}


#if 0
#endif



int maxcolor( int argc, char **argv )
{
JSAMPARRAY ppBuffer0;

FILE *fpJpeg = NULL;

int iHeight;
int iWidth;
int iRow;
int iCol;

if (argv[2])
{
if ((fpJpeg = fopen( argv[2], "rb" )) == NULL )
{
perror( "ela(): fopen()" );
return( 1 );
}
}
else
fpJpeg = stdin;

decompressJpeg( fpJpeg, &iWidth, &iHeight, &ppBuffer0 );

fprintf( stdout, "P3\n%d %d\n255\n", iWidth, iHeight );

#define RED(x,y) (GETJSAMPLE( ppBuffer0[(x)][(y) * 3] ))
#define GREEN(x,y) (GETJSAMPLE( ppBuffer0[(x)][(y) * 3 + 1]))
#define BLUE(x,y) (GETJSAMPLE( ppBuffer0[(x)][(y) * 3 + 2]))

for (iRow = 0; iRow < iHeight; iRow++ )
{
for (iCol = 0; iCol < iWidth; iCol++ )
{
int r = RED( iRow, iCol );
int g = GREEN( iRow, iCol );
int b = BLUE( iRow, iCol );
int red = r * (r>=g && r>=b);
int green = g * (g>=r && g>=b);
int blue = b * (b>=r && b>=g);

fprintf( stdout, "%d %d %d\n", red, green, blue );
}
}

#undef RED
#undef GREEN
#undef BLUE

}


int mincolor( int argc, char **argv )
{
JSAMPARRAY ppBuffer0;

FILE *fpJpeg = NULL;

int iHeight;
int iWidth;
int iRow;
int iCol;

if (argv[2])
{
if ((fpJpeg = fopen( argv[2], "rb" )) == NULL )
{
perror( "ela(): fopen()" );
return( 1 );
}
}
else
fpJpeg = stdin;

decompressJpeg( fpJpeg, &iWidth, &iHeight, &ppBuffer0 );

fprintf( stdout, "P3\n%d %d\n255\n", iWidth, iHeight );

#define RED(x,y) (GETJSAMPLE( ppBuffer0[(x)][(y) * 3] ))
#define GREEN(x,y) (GETJSAMPLE( ppBuffer0[(x)][(y) * 3 + 1]))
#define BLUE(x,y) (GETJSAMPLE( ppBuffer0[(x)][(y) * 3 + 2]))

for (iRow = 0; iRow < iHeight; iRow++ )
{
for (iCol = 0; iCol < iWidth; iCol++ )
{
int r = RED( iRow, iCol );
int g = GREEN( iRow, iCol );
int b = BLUE( iRow, iCol );
int red = r * (r<=g && r<=b);
int green = g * (g<=r && g<=b);
int blue = b * (b<=r && b<=g);

fprintf( stdout, "%d %d %d\n", red, green, blue );
}
}

#undef RED
#undef GREEN
#undef BLUE

}


int midcolor( int argc, char **argv )
{
JSAMPARRAY ppBuffer0;

FILE *fpJpeg = NULL;

int iHeight;
int iWidth;
int iRow;
int iCol;

if (argv[2])
{
if ((fpJpeg = fopen( argv[2], "rb" )) == NULL )
{
perror( "ela(): fopen()" );
return( 1 );
}
}
else
fpJpeg = stdin;

decompressJpeg( fpJpeg, &iWidth, &iHeight, &ppBuffer0 );

fprintf( stdout, "P3\n%d %d\n255\n", iWidth, iHeight );

#define RED(x,y) (GETJSAMPLE( ppBuffer0[(x)][(y) * 3] ))
#define GREEN(x,y) (GETJSAMPLE( ppBuffer0[(x)][(y) * 3 + 1]))
#define BLUE(x,y) (GETJSAMPLE( ppBuffer0[(x)][(y) * 3 + 2]))

for (iRow = 0; iRow < iHeight; iRow++ )
{
for (iCol = 0; iCol < iWidth; iCol++ )
{
int r = RED( iRow, iCol );
int g = GREEN( iRow, iCol );
int b = BLUE( iRow, iCol );
int red = r * (!(r>=g && r>=b) && !(r<=g && r<=b));
int green = g * (!(g>=r && g>=b) && !(g<=r && r<=b));
int blue = b * (!(b>=r && b>=g) && (b<=r && r<=g));

fprintf( stdout, "%d %d %d\n", red, green, blue );
}
}

#undef RED
#undef GREEN
#undef BLUE

}



int main( int argc, char **argv, char **envp )
{
if (argc < 2)
{
usage( argv );
exit( 1 );
}

if (0 == strcmp( argv[1], "ela" ))
{
return( ela( argc, argv ) );
}
if (0 == strcmp( argv[1], "mincolor" ))
return( mincolor( argc, argv ));

if (0 == strcmp( argv[1], "maxcolor" ))
return( maxcolor( argc, argv ));

if (0 == strcmp( argv[1], "midcolor" ))
return( midcolor( argc, argv ));

if (0 == strcmp( argv[1], "avgdist" ))
{
return( avgdist( argc, argv ) );
}
if (0 == strcmp( argv[1], "minmax1" ))
{
return( minmax1( argc, argv ) );
}
if (0 == strcmp( argv[1], "minmax2" ))
{
return( minmax2( argc, argv ) );
}
if (0 == strcmp( argv[1], "minmax4" ))
{
return( minmax4( argc, argv ) );
}
if (0 == strcmp( argv[1], "minmax3" ))
{
return( minmax3( argc, argv ) );
}
if (0 == strcmp( argv[1], "minmax5" ))
{
return( minmax5( argc, argv ) );
}
if (0 == strcmp( argv[1], "pnm" ))
{
return( pnm( argc, argv ) );
}


usage( argv );
exit( 1 );
}

 
design & coding: Vladimir Lettiev aka crux © 2004-2005, Andrew Avramenko aka liks © 2007-2008
current maintainer: Michael Shigorin