Commit 8b9c389d authored by Lee Shallis's avatar Lee Shallis
Browse files

Filter types 0 - 3 all work properly now, 4 resembles the expected

image with some corruption about
parent e5815b46
......@@ -641,7 +641,7 @@ int main( int argc, char *argv[] )
{
for ( i = 0; i < IMAGE_CHANNEL_A; ++i )
{
ulong index = ImagePixelX( &Image, pixel );
ulong index = ImagePixelX( &Image, row, col );
Pixel = ImagePixel( &Image, index, i );
fprintf( pic_ppm, "%s%5lu", whitespace, *Pixel );
whitespace = " ";
......
......@@ -522,7 +522,7 @@ int LoadImagePNG( IMAGE *Image, bool loop )
(
View,
sizeof(ulong),
ImagePixelX(Image,Image->Pixels) * IMAGE_CHANNEL_COUNT
(Image->Rows + 2) * (Image->Cols + 1) * IMAGE_CHANNEL_COUNT
);
if ( !view )
......@@ -576,12 +576,14 @@ void EchoImageDetails( FILE *file, IMAGE *Image )
fprintf( file, "Max Colours %u\n", colours );
}
uchar PaethPredictor( uchar a, uchar b, uchar c )
ulong ImageFilterPaethPredictor( IMAGE *Image, ulong a, ulong b, ulong c )
{
uchar p = (a + b) - c;
uchar pa = p - a;
uchar pb = p - b;
uchar pc = p - c;
ulong depth = Image->Depth;
ulong keep = ~(~0 << depth);
ulong p = (((a + b) & keep) - c) & keep;
ulong pa = (p - a) & keep;
ulong pb = (p - b) & keep;
ulong pc = (p - c) & keep;
if ( pa <= pb && pa <= pc )
return a;
......@@ -609,42 +611,44 @@ ulong * ImagePixel( IMAGE *Image, ulong pixel, IMAGE_CHANNELS channel )
return view + (pixel * IMAGE_CHANNEL_COUNT) + channel;
}
ulong ImageFilter0( IMAGE *Image, ulong channel, ulong pixel )
ulong ImageFilter0( IMAGE *Image, ulong channel, ulong row, ulong col )
{
(void)pixel;
(void)row;
(void)col;
(void)Image;
(void)channel;
return 0;
}
ulong ImageFilter1( IMAGE *Image, ulong channel, ulong pixel )
ulong ImageFilter1( IMAGE *Image, ulong channel, ulong row, ulong col )
{
return
(pixel % Image->Cols) ?
*ImagePixel( Image, ImagePixelA( Image, pixel ), channel ) : 0;
return *ImagePixel( Image, ImagePixelA( Image, row, col ), channel );
}
ulong ImageFilter2( IMAGE *Image, ulong channel, ulong pixel )
ulong ImageFilter2( IMAGE *Image, ulong channel, ulong row, ulong col )
{
return *ImagePixel( Image, ImagePixelB( Image, pixel ), channel );
return *ImagePixel( Image, ImagePixelB( Image, row, col ), channel );
}
ulong ImageFilter3( IMAGE *Image, ulong channel, ulong pixel )
ulong ImageFilter3( IMAGE *Image, ulong channel, ulong row, ulong col )
{
ulong *PA = ImagePixel( Image, ImagePixelA( Image, pixel ), channel );
ulong *PB = ImagePixel( Image, ImagePixelB( Image, pixel ), channel );
ulong *PA = ImagePixel( Image, ImagePixelA( Image, row, col ), channel );
ulong *PB = ImagePixel( Image, ImagePixelB( Image, row, col ), channel );
return (*PA + *PB) / 2;
}
ulong ImageFilter4( IMAGE *Image, ulong channel, ulong pixel )
ulong ImageFilter4( IMAGE *Image, ulong channel, ulong row, ulong col )
{
ulong *PA = ImagePixel( Image, ImagePixelA( Image, pixel ), channel );
ulong *PB = ImagePixel( Image, ImagePixelB( Image, pixel ), channel );
ulong *PC = ImagePixel( Image, ImagePixelC( Image, pixel ), channel );
return PaethPredictor( *PA, *PB, *PC );
return ImageFilterPaethPredictor
(
Image,
ImageFilter1( Image, channel, row, col ),
ImageFilter2( Image, channel, row, col ),
ImageFilter3( Image, channel, row, col )
);
}
typedef ulong (*ImageFilterT)( IMAGE*, ulong, ulong );
typedef ulong (*ImageFilterT)( IMAGE* Image, ulong channel, ulong row, ulong col );
ImageFilterT ImageFilters[5] =
{
......@@ -668,9 +672,7 @@ int ScanLines( IMAGE *Image )
BUFFER *Data = AccessBuffer( Buffers, Image->DataID );
uchar *data = Data->addr;
BIT pos;
FILE *errors = Buffers->Alloc->errors;
FILE *verbose = Buffers->Alloc->verbose;
FILE *detailed = Buffers->Alloc->detailed;
float gamma = Image->GammaF;
#if 0
STREAM Stream = {0};
......@@ -699,92 +701,28 @@ int ScanLines( IMAGE *Image )
{
ulong filter = ImageValueFromIDAT( Image, &pos, depth );
ECHO
(
detailed,
fprintf( detailed, "%lu [", filter )
);
if ( filter > 4 )
{
if ( detailed ) fputc( '\n', detailed );
ECHO( errors, ECHO_ERR( errors, EINVAL ) );
return EINVAL;
}
for ( col = 0; col < Cols; ++col, ++pixel )
{
ulong channel = 0;
for ( ; channel < channels; ++channel )
{
ulong index = ImagePixelX( Image, pixel );
ulong index = ImagePixelX( Image, row, col );
ulong val = ImageValueFromIDAT( Image, &pos, depth );
ulong *PX = ImagePixel( Image, index, channel );
ulong loc = ImageFilters[filter]( Image, channel, pixel );
ulong loc = ImageFilters[filter]( Image, channel, row, col );
*PX = (val + loc) & keep;
if ( detailed )
{
fputc( ' ', detailed );
EchoBits( detailed, &val, 0, depth );
}
}
if ( detailed )
fprintf( detailed, ", " );
for ( ; channel < IMAGE_CHANNEL_A; ++channel )
{
ulong index = ImagePixelX( Image, pixel );
ulong index = ImagePixelX( Image, row, col );
ulong *PX = ImagePixel( Image, index, channel );
ulong *P0 = ImagePixel( Image, index, 0 );
*PX = *P0;
}
}
if ( detailed ) fprintf( detailed, " ]\n" );
}
#if 0
ECHO( detailed, fputs("Image IDAT bytes:\n", detailed ) );
x = 0;
SetBit( &pos, data, 0 );
for ( row = 0, x = 0; row < Rows; ++row )
{
ulong channel;
ulong filter = ImageValueFromIDAT( Image, &pos, bits );
if ( detailed ) fprintf( detailed, " %lu [", filter );
for ( col = 0; col < Cols; ++col )
{
val = ImageValueFromIDAT( Image, &pos, depth );
if ( detailed ) fprintf( detailed, " %04lX", val );
}
if ( detailed ) fprintf( detailed, " ]\n" );
}
#endif
#if 0
p = 0;
for ( row = 0; row < Rows; ++row )
{
for ( col = 0; col < Cols; ++col )
{
for ( b = 0; b < channels; ++b, ++p )
{
float val;
Pixel = ImagePixel( Image, pixel, b );
val = *Pixel;
val /= max;
val *= (float)CHAR_MAX;
*Pixel = floorf(val);
}
}
}
#endif
return 0;
}
......@@ -54,11 +54,25 @@ typedef enum _IMAGE_CHANNELS
/*Use with the below functions to get equivalant of raw_ptr + pixel */
ulong * ImagePixel( IMAGE *Image, ulong pixel, IMAGE_CHANNELS channel );
/* Adds any offset/s to produce the index for channel 0 of said pixel */
#define ImagePixelX( IMAGE, PIXEL ) (((IMAGE)->Cols * 2) + PIXEL)
#define ImageRows(IMAGE) ((IMAGE)->Rows)
#define ImageCols(IMAGE) ((IMAGE)->Cols)
#define ImagePfxRows 2
#define ImagePfxCols 1
#define ImageRowOffest(IMAGE,ROW) ((ROW) * (ImageCols(IMAGE) + ImagePfxCols))
#define ImagePixelX(IMAGE, ROW, COL ) \
(ImageRowOffest(IMAGE,(ROW)+ImagePfxRows) + (COL))
/* Adds any offset/s for local pixel indices, index 0 is used for nil, do NOT
* fill that index */
#define ImagePixelA( IMAGE, PIXEL ) (ImagePixelX(IMAGE,PIXEL) - 1)
#define ImagePixelB( IMAGE, PIXEL ) (ImagePixelX(IMAGE,PIXEL) - (IMAGE)->Cols)
#define ImagePixelC( IMAGE, PIXEL ) (ImagePixelB(IMAGE,PIXEL) - 1)
#define ImagePixelA(IMAGE, ROW, COL ) \
(ImagePixelX(IMAGE,ROW,COL) - 1)
#define ImagePixelB(IMAGE, ROW, COL ) \
((ImagePixelX(IMAGE,ROW,COL) - ImageCols(IMAGE)) - ImagePfxCols)
#define ImagePixelC(IMAGE, ROW, COL ) \
(ImagePixelB(IMAGE,ROW,COL) - 1)
#endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment