|
Appendix C MATH.C Sample Program
/*---------------------------------------------------------------
-----------*/
/* */
/* Copyright (C) 1995, W. David Schwaderer */
/* Some rights reserved - see WdsLogo() for details */
/* */
/* Function: This program displays sample image processing output */
/* for number sequences. See the text description in: */
/* */
/* Digital Imaging in C and the World Wide Web */
/* W. David Schwaderer */
/* Wordware Publishing, Inc., 1997 */
/* */
/* Compiler: Borland C++ 3.1 Compile command: bcc -ml Math.C */
/* */
/* This is a large model PC MS-DOS(TM) program. */
/* */
/* Call to programmers... */
/* */
/* If you are interested enough to read this code, your name */
/* probably should be in it. I will place contributing programmer's */
/* names in source code when their suggestion(s) results in bug fixes */
/* or program enhancements. Contributions must be new. Send written */
/* suggestions on paper to W. David Schwaderer via Wordware Publishing, */
/* Inc., 1997. */
/* */
/* Complete or massive rewrites are not helpful (though possibly */
/* warranted!). Contributions become the property of W. David */
/* Schwaderer who reserves suggestion final disposition determination. */
/* */
/* Thanks in advance for the consideration. */
/* W. David Schwaderer */
/* */
/* Version 1.0 3/15/95 */
/* */
/* Contributor Enhancement */
/* ----------------------------
----------------------------------------- */
/* Your name here... Your edited suggestion here... */
/* Hometown, country Your edited suggestion here... */
/* */
/*----------------------------------------------------------------
----------*/
#include <stdio.h>
int InputArray[ ] = {
0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 8,
2, 2, 4, 4, 6, 6, 8, 8, 8,
8, 6, 4, 2, 2, 2,
8, 8, 8, 2, 2, 2,
8, 8, 8, 4, 4, 4,
8, 8, 8, 6, 6, 6,
8, 6, 4, 2, 0,
8, 8, 8, 7, 6, 5, 4, 3, 2, 1, 0, };
#define MAX_GRAPH_ROW 15
#define MAX_MASK_WIDTH 3
#define ARRAY_OFFSET (MAX_MASK_WIDTH / 2)
int OutputArray [(sizeof(InputArray) / sizeof(int))];
int SmoothingArray[(sizeof(InputArray) / sizeof(int))];
int StagingArray[(sizeof(InputArray) / sizeof(int)) + (2 * ARRAY_OFFSET)];
#define INPUT_ELEMENT_COUNT ((sizeof(InputArray) / sizeof(int)))
#define STAGING_ARRAY_SIZE ((sizeof(StagingArray) / sizeof(int)))
void LoadStagingArray(void);
void PerformSmoothing(void);
void PerformSharpening(void);
void PerformUnsharpMasking(void);
void GraphArray(int *IntArrayPtr);
void DisplayInputOutputDifference(void);
int main()
{
printf("\nmain: INPUT_ELEMENT_COUNT == %d, ARRAY_OFFSET == %d",
INPUT_ELEMENT_COUNT , ARRAY_OFFSET );
printf("\nmain: sizeof(StagingArray) == %d",
sizeof(StagingArray) );
LoadStagingArray();
printf("\n\fmain: InputArray[] follows...");
GraphArray(InputArray);
PerformSmoothing();
PerformSharpening();
PerformUnsharpMasking();
printf("\n\fmain: Returning...\n");
return 0;
}
void LoadStagingArray(void)
{
unsigned Index;
for (Index = 0; Index < ARRAY_OFFSET; Index++)
StagingArray[Index] = InputArray[0];
for (Index = 0; Index < INPUT_ELEMENT_COUNT; Index++)
StagingArray[Index + ARRAY_OFFSET] = InputArray[Index];
for (Index = 0; Index < ARRAY_OFFSET; Index++)
StagingArray[Index + INPUT_ELEMENT_COUNT + 1] =
InputArray[INPUT_ELEMENT_COUNT - 1];
return;
}
void DisplayStagingArray(void)
{
unsigned Index;
putchar('\n');
for (Index = 0; Index < STAGING_ARRAY_SIZE; Index++)
{
printf("\nStagingArray[%3d] = %d",
Index, StagingArray[Index]);
}
return;
}
void GraphArray(int *IntArrayPtr)
{
int Row, Column;
printf("\n");
for (Row = 0; Row < MAX_GRAPH_ROW -1 ; Row++)
{
printf("\n%4d |", (MAX_GRAPH_ROW - Row - 1));
for (Column = 0; Column < INPUT_ELEMENT_COUNT; Column++)
if ((MAX_GRAPH_ROW - Row - 1) > IntArrayPtr[Column])
putchar('-');
else
putchar('*');
printf("|%3u", (MAX_GRAPH_ROW - Row - 1));
}
printf("\n%4d +", 0);
for (Column = 0; Column < INPUT_ELEMENT_COUNT; Column++)
putchar('-');
printf("+%3u", 0);
printf("\nInput:");
for (Column = 0; Column < INPUT_ELEMENT_COUNT; Column++)
printf("%1u", InputArray[Column]);
printf("\n ");
for (Column = 0; Column < INPUT_ELEMENT_COUNT; Column++)
if (IntArrayPtr[Column] == InputArray[Column])
putchar('=');
else
if (IntArrayPtr[Column] > InputArray[Column])
putchar('+');
else
putchar('-');
return;
}
void DisplayInputOutputDifference(void)
{
unsigned Index;
for (Index = 0; Index < INPUT_ELEMENT_COUNT; Index++)
{
printf("\nGraphArray: InputArray[%3d] == %3d -> %3d",
Index, InputArray[Index], OutputArray[Index]);
if (InputArray[Index] != OutputArray[Index])
printf(" * (%3d)",(OutputArray[Index] - InputArray[Index]));
}
}
void PerformSmoothing(void)
{
unsigned Index;
printf("\n\f");
printf("PerformSmoothing: 1/3 [+1,+1,+1] OutputArray[]...");
for (Index = ARRAY_OFFSET;
Index < ARRAY_OFFSET + INPUT_ELEMENT_COUNT;
Index++)
{
OutputArray[Index - ARRAY_OFFSET] =
(StagingArray[Index - 1] +
StagingArray[Index ] +
StagingArray[Index + 1])
/
3;
}
GraphArray(OutputArray);
printf("\n\nPerformSmoothing: 1/4 [+1,+2,+1] OutputArray[]...");
for (Index = ARRAY_OFFSET;
Index < ARRAY_OFFSET + INPUT_ELEMENT_COUNT;
Index++)
{
OutputArray[Index - ARRAY_OFFSET] =
(StagingArray[Index - 1] +
2 * StagingArray[Index ] + StagingArray[Index + 1])
/
4;
}
GraphArray(OutputArray);
printf("\n\nPerformSmoothing: 1/2 [+1,0,+1] OutputArray[]...");
for (Index = ARRAY_OFFSET;
Index < ARRAY_OFFSET + INPUT_ELEMENT_COUNT;
Index++)
{
OutputArray[Index - ARRAY_OFFSET] =
(StagingArray[Index - 1] + StagingArray[Index + 1])
/
2;
}
GraphArray(OutputArray);
return;
}
void PerformSharpening(void)
{
unsigned Index;
printf("\n\f");
printf("PerformSharpening: 1/2 [-1,+2,+1] OutputArray[]...");
for (Index = ARRAY_OFFSET;
Index < ARRAY_OFFSET + INPUT_ELEMENT_COUNT;
Index++)
{
OutputArray[Index - ARRAY_OFFSET] =
StagingArray[Index] +
((StagingArray[Index+1] - StagingArray[Index-1]) / 2);
}
GraphArray(OutputArray);
printf("\n\nPerformSharpening: 1/2 [+1,+2,-1] OutputArray[]...");
for (Index = ARRAY_OFFSET;
Index < ARRAY_OFFSET+ INPUT_ELEMENT_COUNT;
Index++)
{
OutputArray[Index - ARRAY_OFFSET] =
StagingArray[Index] +
((StagingArray[Index-1] - StagingArray[Index+1]) / 2);
}
GraphArray(OutputArray);
printf("\n\fPerformSharpening: [+1,+1,-1] OutputArray[]...");
for (Index = ARRAY_OFFSET;
Index < ARRAY_OFFSET+ INPUT_ELEMENT_COUNT;
Index++)
{
OutputArray[Index - ARRAY_OFFSET] = StagingArray[Index-1] +
StagingArray[Index ] -
StagingArray[Index+1];
}
GraphArray(OutputArray);
printf("\n\nPerformSharpening: [-1,+1,+1] OutputArray[]...");
for (Index = ARRAY_OFFSET;
Index < ARRAY_OFFSET+ INPUT_ELEMENT_COUNT;
Index++)
{
OutputArray[Index - ARRAY_OFFSET] = StagingArray[Index+1] +
StagingArray[Index ] -
StagingArray[Index-1];
}
GraphArray(OutputArray);
return;
}
void PerformUnsharpMasking(void)
{
unsigned Index;
printf("\n\f");
printf("PerformUnsharpMasking: 1/3 [+1,+1,+1] OutputArray[]...");
for (Index = 0; Index < INPUT_ELEMENT_COUNT; Index++)
{
SmoothingArray[Index] =
(StagingArray[Index - 1 + ARRAY_OFFSET] +
StagingArray[Index + ARRAY_OFFSET] +
StagingArray[Index + 1 + ARRAY_OFFSET])
/
3;
}
for (Index = 0;
Index < INPUT_ELEMENT_COUNT;
Index++)
OutputArray[Index] = 2 * InputArray[Index] - SmoothingArray[Index];
GraphArray(OutputArray);
printf("\n\nPerformUnsharpMasking: 1/4 [+1,+2,+1] OutputArray[]...");
for (Index = 0; Index < INPUT_ELEMENT_COUNT; Index++)
{
SmoothingArray[Index] =
(StagingArray[Index - 1 + ARRAY_OFFSET] +
2 * StagingArray[Index + ARRAY_OFFSET] +
StagingArray[Index + 1 + ARRAY_OFFSET])
/
4;
}
for (Index = 0; Index < INPUT_ELEMENT_COUNT; Index++)
OutputArray[Index] = 2 * InputArray[Index] - SmoothingArray[Index];
GraphArray(OutputArray);
printf("\n\nPerformUnsharpMasking: 1/4 [+1,+2,+1] OutputArray[]...");
for (Index = 0; Index < INPUT_ELEMENT_COUNT; Index++)
OutputArray[Index] =
(2 * InputArray[Index]) -
( (StagingArray[Index - 1 + ARRAY_OFFSET] +
2 * StagingArray[Index + ARRAY_OFFSET] +
StagingArray[Index + 1 + ARRAY_OFFSET])
/
4);
GraphArray(OutputArray);
return;
}
/*============================ End of File
============================*/
|