[LONG] help with old source code from book



hi clc.

i was browsing an old book about image processing in C, and i was not able to compile most of the source code given there.

for example:

$ cat LIST2_1.C
/***************************************************************************
* File: arithlut.c *
* *
* Desc: This program performs arithmetic point operations via LUTs *

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


#include <stdio.h>
#include <string.h>
#include <alloc.h>
#include "ip.h"

#define operation(VALUE) ((float) VALUE * 1.9)

extern void write_pnm(image_ptr ptr, char filein[], int rows,
int cols, int magic_number);
extern image_ptr read_pnm(char *filename, int *rows, int *cols,
int *type);

int main(int argc, char *argv[])
{
char filein[100]; /* name of input file */
char fileout[100]; /* name of output file */
int rows, cols; /* image rows and columns */
unsigned long i; /* counting index */
unsigned long bytes_per_pixel; /* number of bytes per image pixel */
unsigned char LUT[256]; /* array for Look-up table */
image_ptr buffer; /* pointer to image buffer */
unsigned long number_of_pixels; /* total number of pixels in image */
int temp; /* temporary variable */
int type; /* what type of image data */


/* set input filename and output file name */
if(argc == 3)
{
strcpy(filein, argv[1]);
strcpy(fileout, argv[2]);
}
else
{
printf("Input name of input file\n");
gets(filein);
printf("\nInput name of output file\n");
gets(fileout);
printf("\n");
}

buffer = read_pnm(filein, &rows, &cols, &type);

/* initialize Look-up table */
for(i=0; i<256; i++)
{
temp = operation(i);
CLIP(temp, 0, 255);
LUT[i] = temp;
}

/* determine bytes_per_pixel, 3 for color, 1 for gray-scale */
if(type == PPM)
bytes_per_pixel = 3;
else
bytes_per_pixel = 1;

number_of_pixels = bytes_per_pixel * rows * cols;

/* process image via the Look-up table */
for(i=0; i<number_of_pixels; i++)
buffer[i] = LUT[buffer[i]];

write_pnm(buffer, fileout, rows, cols, type);
IP_FREE(buffer);
return 0;
}
$ cat ip.h
/****************************************************************************
* file - ip.h *
* *
* For DOS compilers, the following pointers may need to be defined as huge *
* or large depending on your compiler: image_ptr, pixel_ptr, double_ptr, *
* and complex_ptr. IP_MALLOC and IP_FREE may also need to be redefined. *
* For the Borland Compiler, they should be defined as farmalloc and *
* and farfree. *
* *

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

/* typedefs */

typedef unsigned char *image_ptr;
typedef double *double_ptr;
typedef struct
{
unsigned char r,g,b;
} pixel;

typedef pixel *pixel_ptr;

typedef struct
{
int width;
int height;
float *x_data;
float *y_data;
} mesh;

typedef struct
{
double re;
double im;
} COMPLEX;

typedef COMPLEX *complex_ptr;

typedef struct
{
int x;
int y;
} POINT;

typedef struct
{
POINT P;
POINT Q;
int dx, dy;
float length;
long length_squared;
} LINE;

typedef struct
{
POINT P;
POINT Q;
} LINE_SEGMENT;

typedef struct
{
int number; /* number of segments to follow */
LINE_SEGMENT line[100];
char *filename; /* name of file holding the line list */
} LINE_LIST;

/* defines */

#define PI 3.14159265358979323846
#define CLIP(val, low, high) {if(val<low) val=low; if(val>high) val=high;}
#define CLAMP(val, low, high) ((val<low) ? low : ((val>high) ? high : val))
#define MAX(A,B) ((A) > (B) ? (A) : (B))
#define MIN(A,B) ((A) < (B) ? (A) : (B))
#define IP_MALLOC(X) malloc(X)
#define IP_FREE(X) free(X)
#define PBM 4
#define PGM 5
#define PPM 6

$ cat iplib.c
/***************************************************************************

* File: iplib.c *
* *
* Desc: general purpose image processing routines *

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


#include <alloc.h>
#include <stdio.h>
#include <stdlib.h>
#include "ip.h"

image_ptr read_pnm(char *filename, int *rows, int *cols, int *type);
int getnum(FILE *fp);

/***************************************************************************
* Func: read_pnm *
* *
* Desc: reads a portable bitmap file *
* *
* Params: filename - name of image file to read *
* rows - number of rows in the image *
* cols - number of columns in the image *
* type - file type *
* *
* Returns: pointer to the image just read into memory *

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

image_ptr read_pnm(char *filename, int *rows, int *cols, int *type)
{
int i; /* index variable */
int row_size; /* size of image row in bytes */
int maxval; /* maximum value of pixel */
FILE *fp; /* input file pointer */
int firstchar, secchar; /* first 2 characters in the input file */
image_ptr ptr; /* pointer to image buffer */
unsigned long offset; /* offset into image buffer */
unsigned long total_size; /* size of image in bytes */
unsigned long total_bytes; /* number of total bytes written to file */
float scale; /* number of bytes per pixel */

/* open input file */
if((fp = fopen(filename, "rb")) == NULL)
{
printf("Unable to open %s for reading\n",filename);
exit(1);
}

firstchar = getc(fp);
secchar = getc(fp);

if(firstchar != 'P')
{
printf("You silly goof... This is not a PPM file!\n");
exit(1);
}

*cols = getnum(fp);
*rows = getnum(fp);
*type = secchar - '0';

switch(secchar)
{
case '4': /* PBM */
scale = 0.125;
maxval = 1;
break;
case '5': /* PGM */
scale = 1.0;
maxval = getnum(fp);
break;
case '6': /* PPM */
scale = 3.0;
maxval = getnum(fp);
break;
default : /* Error */
printf("read_pnm: This is not a Portable bitmap RAWBITS file\n");
exit(1);
break;
}

row_size = (*cols) * scale;
total_size = (unsigned long) (*rows) * row_size;

ptr = (image_ptr) IP_MALLOC(total_size);

if(ptr == NULL)
{
printf("Unable to malloc %lu bytes\n",total_size);
exit(1);
}

total_bytes=0;
offset = 0;
for(i=0; i<(*rows); i++)
{
total_bytes+=fread(ptr+offset, 1, row_size, fp);
offset += row_size;
}

if(total_size != total_bytes)
{
printf("Failed miserably trying to read %ld bytes\nRead %ld bytes\n",
total_size, total_bytes);
exit(1);
}

fclose(fp);
return ptr;
}

/***************************************************************************
* Func: getnum *
* *
* Desc: reads an ASCII number from a portable bitmap file header *
* *
* Param: fp - pointer to file being read *
* *
* Returns: the number read *

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

int getnum(FILE *fp)
{
char c; /* character read in from file */
int i; /* number accumulated and returned */

do
{
c = getc(fp);
}
while((c==' ') || (c=='\t') || (c=='\n') || (c=='\r'));

if((c<'0') || (c>'9'))
if(c == '#') /* chew off comments */
{
while(c == '#')
{
while(c != '\n')
c = getc(fp);
c = getc(fp);
}
}
else
{
printf("Garbage in ASCII fields\n");
exit(1);
}

i=0;
do
{
i=i*10+(c-'0'); /* convert ASCII to int */
c = getc(fp);
}
while((c>='0') && (c<='9'));

return i;
}

/***************************************************************************
* Func: write_pnm *
* *
* Desc: writes out a portable bitmap file *
* *
* Params: ptr - pointer to image in memory *
* filename _ name of file to write image to *
* rows - number of rows in the image *
* cols - number of columns in the image *
* magic_number - number that defines what type of file it is *
* *
* Returns: nothing *

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

void write_pnm(image_ptr ptr, char *filename, int rows,
int cols, int magic_number)
{
FILE *fp; /* file pointer for output file */
long offset; /* current offset into image buffer */
long total_bytes; /* number of bytes written to output file */
long total_size; /* size of image buffer */
int row_size; /* size of row in bytes */
int i; /* index variable */
float scale; /* number of bytes per image pixel */

switch(magic_number)
{
case 4: /* PBM */
scale = 0.125;
break;
case 5: /* PGM */
scale = 1.0;
break;
case 6: /* PPM */
scale = 3.0;
break;
default : /* Error */
printf("write_pnm: This is not a Portable bitmap RAWBITS file\n");
exit(1);
break;
}

/* open new output file */
if((fp=fopen(filename, "wb")) == NULL)
{
printf("Unable to open %s for output\n",filename);
exit(1);
}

/* print out the portable bitmap header */
fprintf(fp, "P%d\n%d %d\n", magic_number, cols, rows);
if(magic_number != 4)
fprintf(fp, "255\n");

row_size = cols * scale;
total_size = (long) row_size *rows;
offset = 0;
total_bytes = 0;
for(i=0; i<rows; i++)
{
total_bytes += fwrite(ptr+offset, 1, row_size, fp);
offset += row_size;
}

if(total_bytes != total_size)
printf("Tried to write %ld bytes...Only wrote %ld\n",
total_size, total_bytes);

fclose(fp);
}

/****************************************************************************
* Func: pnm_open *
* *
* Desc: opens a pnm file and determines rows, cols, and maxval *
* *
* Params: rows- pointer to number of rows in the image *
* cols - pointer number of columns in the image *
* maxval - pointer to max value *
* filename - name of image file *

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

FILE *pnm_open(int *rows, int *cols, int *maxval, char *filename)
{
int firstchar, secchar;
float scale;
unsigned long row_size;
FILE *fp;

if((fp = fopen(filename, "rb")) == NULL)
{
printf("Unable to open %s for reading\n",filename);
exit(1);
}

firstchar = getc(fp);
secchar = getc(fp);

if(firstchar != 'P')
{
printf("You silly goof... This is not a PPM file!\n");
exit(1);
}

*cols = getnum(fp);
*rows = getnum(fp);

switch(secchar)
{
case '4': /* PBM */
scale = 0.125;
*maxval = 1;
break;
case '5': /* PGM */
scale = 1.0;
*maxval = getnum(fp);
break;
case '6': /* PPM */
scale = 3.0;
*maxval = getnum(fp);
break;
default : /* Error */
printf("read_pnm: This is not a Portable bitmap RAWBITS file\n");
exit(1);
break;
}

row_size = (*cols) * scale;
return fp;
}


/****************************************************************************
* Func: read_mesh *
* *
* Desc: reads mesh data into a mesh structure *
* *
* Params: filename - name of input mesh file *
* *
* Returns: mesh structure storing width, height, x data and y data *

****************************************************************************/
mesh *read_mesh(char *filename)
{
FILE *fp;
mesh *mesh_data;
int width, height, mesh_size;

/* open mesh file for input */
if((fp = fopen(filename, "rb")) == NULL)
{
printf("Unable to open mesh file %s for reading\n", filename);
exit(1);
}

mesh_data = malloc(sizeof(mesh));
/* read dimensions of mesh */
fread(&width, sizeof(int), 1, fp);
fread(&height, sizeof(int), 1, fp);
mesh_data->width = width;
mesh_data->height = height;
mesh_size = width * height;

/* allocate memory for mesh data */
mesh_data->x_data = malloc(sizeof(float) * mesh_size);
mesh_data->y_data = malloc(sizeof(float) * mesh_size);

fread(mesh_data->x_data, sizeof(float), mesh_size, fp);
fread(mesh_data->y_data, sizeof(float), mesh_size, fp);

return(mesh_data);
}
$

i have no idea where to start from to make it compilable...

anyone help?

bye
.



Relevant Pages

  • Re: error from mfilename
    ... typedef struct tagTRANSMITTER_CONFIGURATION ... PCIBIRD_API int InitializeBIRDSystem; ... Parameters Passed: USHORT sensorID, ...
    (comp.soft-sys.matlab)
  • Re: C# - getting binary data from .lib
    ... int create(int id, int scale, unsigned char *image); ... unsigned char* ptrImage = NULL; ... // Read through entire pointer byte by byte and put into managed array ...
    (microsoft.public.dotnet.framework.interop)
  • Re: C# - getting binary data from .lib
    ... Use Marshal.AllocHGlobal to allocate the memory and pass this IntPtr to the ... int retCode = create(id, scale, ptrImage); ... You now control the pointer returned. ...
    (microsoft.public.dotnet.framework.interop)
  • Re: C# - getting binary data from .lib
    ... then there is a problem with the ptrImage ... int retCode = create(id, scale, ptrImage); ... You now control the pointer returned. ...
    (microsoft.public.dotnet.framework.interop)
  • A little light code review requested
    ... typedef struct s_operator Oper; ... void opcall (int opcode, ...) { ... X(boolean, unsigned char b) \ ...
    (comp.lang.c)