aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorgiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2020-12-27 03:36:15 +0000
committergiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2020-12-27 03:36:15 +0000
commit02cd57520ca330a51954682a9540b191e9d7b849 (patch)
treed31de1d87e7143874d69e18280caf8729bc82300
parent1b52ec5b04ff3b873ff49e57f635330817c3445d (diff)
downloadnetpbm-mirror-02cd57520ca330a51954682a9540b191e9d7b849.tar.gz
netpbm-mirror-02cd57520ca330a51954682a9540b191e9d7b849.tar.xz
netpbm-mirror-02cd57520ca330a51954682a9540b191e9d7b849.zip
cleanup
git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@4010 9d0c8265-081b-0410-96cb-a4ca84ce46f8
-rw-r--r--converter/pgm/rawtopgm.c163
1 files changed, 87 insertions, 76 deletions
diff --git a/converter/pgm/rawtopgm.c b/converter/pgm/rawtopgm.c
index f34815a7..7eb68694 100644
--- a/converter/pgm/rawtopgm.c
+++ b/converter/pgm/rawtopgm.c
@@ -10,6 +10,7 @@
** implied warranty.
*/
+#include <stdbool.h>
#include <math.h>
#include "pm_c_util.h"
@@ -17,7 +18,7 @@
#include "shhopt.h"
#include "pgm.h"
-struct cmdline_info {
+struct CmdlineInfo {
/* All the information the user supplied in the command line,
in a form easy for the program to use.
*/
@@ -37,8 +38,8 @@ struct cmdline_info {
static void
-parse_command_line(int argc, char ** argv,
- struct cmdline_info *cmdlineP) {
+parseCommandLine(int argc, const char ** argv,
+ struct CmdlineInfo * const cmdlineP) {
/*----------------------------------------------------------------------------
Note that the file spec array we return is stored in the storage that
was passed to us as the argv array.
@@ -73,7 +74,7 @@ parse_command_line(int argc, char ** argv,
NULL, 0);
/* Set the defaults */
- cmdlineP->bottomfirst = FALSE;
+ cmdlineP->bottomfirst = false;
cmdlineP->headerskip = 0;
cmdlineP->rowskip = 0.0;
cmdlineP->bpp = 1;
@@ -81,26 +82,26 @@ parse_command_line(int argc, char ** argv,
cmdlineP->maxval = -1;
opt.opt_table = option_def;
- opt.short_allowed = FALSE; /* We have no short (old-fashioned) options */
- opt.allowNegNum = FALSE; /* We may have parms that are negative numbers */
+ opt.short_allowed = false; /* We have no short (old-fashioned) options */
+ opt.allowNegNum = false; /* We may have parms that are negative numbers */
- pm_optParseOptions3(&argc, argv, opt, sizeof(opt), 0);
+ pm_optParseOptions3(&argc, (char **)argv, opt, sizeof(opt), 0);
/* Uses and sets argc, argv, and some of *cmdlineP and others. */
if (argc-1 == 0) {
cmdlineP->inputFileName = "-";
- cmdlineP->autosize = TRUE;
+ cmdlineP->autosize = true;
} else if (argc-1 == 1) {
cmdlineP->inputFileName = argv[1];
- cmdlineP->autosize = TRUE;
+ cmdlineP->autosize = true;
} else if (argc-1 == 2) {
cmdlineP->inputFileName = "-";
- cmdlineP->autosize = FALSE;
+ cmdlineP->autosize = false;
cmdlineP->width = pm_parse_width(argv[1]);
cmdlineP->height = pm_parse_height(argv[2]);
} else if (argc-1 == 3) {
cmdlineP->inputFileName = argv[3];
- cmdlineP->autosize = FALSE;
+ cmdlineP->autosize = false;
cmdlineP->width = pm_parse_width(argv[1]);
cmdlineP->height = pm_parse_height(argv[2]);
} else
@@ -133,50 +134,57 @@ parse_command_line(int argc, char ** argv,
static void
-compute_image_size(const struct cmdline_info cmdline, const long nread,
- int * const rows_p, int * const cols_p) {
+computeImageSize(struct CmdlineInfo const cmdline,
+ long const nRead,
+ unsigned int * const rowsP,
+ unsigned int * const colsP) {
if (cmdline.autosize) {
- int sqrt_trunc =
- (int) sqrt((double) (nread-cmdline.headerskip));
- if (sqrt_trunc*sqrt_trunc+cmdline.headerskip != nread)
+ int sqrtTrunc =
+ (int) sqrt((double) (nRead - cmdline.headerskip));
+ if (sqrtTrunc * sqrtTrunc + cmdline.headerskip != nRead)
pm_error( "You must specify the dimensions of the image unless "
"it is a quadratic image. This one is not quadratic: "
"The number of "
"pixels in the input is %ld, which is not a perfect "
- "square.", nread-cmdline.headerskip);
- *rows_p = *cols_p = sqrt_trunc;
- pm_message( "Image size: %d cols, %d rows", *cols_p, *rows_p);
+ "square.", nRead - cmdline.headerskip);
+ *rowsP = *colsP = sqrtTrunc;
+ pm_message( "Image size: %u cols, %u rows", *colsP, *rowsP);
} else {
- *rows_p = cmdline.height;
- *cols_p = cmdline.width;
+ *rowsP = cmdline.height;
+ *colsP = cmdline.width;
}
}
static void
-skip_header(FILE *ifp, const int headerskip) {
+skipHeader(FILE * const ifP,
+ unsigned int const headerskip) {
+
int i;
- for ( i = 0; i < headerskip; ++i ) {
+ for (i = 0; i < headerskip; ++i) {
/* Read a byte out of the file */
int val;
- val = getc( ifp );
- if ( val == EOF )
- pm_error("EOF / read error reading Byte %d in the header", i );
+ val = getc(ifP);
+ if (val == EOF)
+ pm_error("EOF / read error reading Byte %u in the header", i );
}
}
static gray
-read_from_file(FILE *ifp, const int bpp, const int row, const int col,
- const int littleendian) {
+readFromFile(FILE * const ifP,
+ unsigned int const bpp,
+ unsigned int const row,
+ unsigned int const col,
+ bool const littleEndian) {
/*----------------------------------------------------------------------------
- Return the next sample value from the input file 'ifp', assuming the
+ Return the next sample value from the input file *ifP, assuming the
input stream is 'bpp' bytes per pixel (1 or 2). In the case of two
- bytes, if 'littleendian', assume least significant byte is first.
+ bytes, if 'littleEndian', assume least significant byte is first.
Otherwise, assume MSB first.
In error messages, say this is Column 'col', Row 'row'. Exit program if
@@ -186,18 +194,18 @@ read_from_file(FILE *ifp, const int bpp, const int row, const int col,
if (bpp == 1) {
int val;
- val = getc(ifp);
+ val = getc(ifP);
if (val == EOF)
- pm_error( "EOF / read error at Row %d Column %d",
+ pm_error( "EOF / read error at Row %u Column %u",
row, col);
retval = (gray) val;
} else {
short val;
int rc;
- rc = littleendian ?
- pm_readlittleshort(ifp, &val) : pm_readbigshort(ifp, &val);
+ rc = littleEndian ?
+ pm_readlittleshort(ifP, &val) : pm_readbigshort(ifP, &val);
if (rc != 0)
- pm_error( "EOF / read error at Row %d Column %d",
+ pm_error( "EOF / read error at Row %u Column %u",
row, col);
retval = (gray) val;
}
@@ -207,89 +215,92 @@ read_from_file(FILE *ifp, const int bpp, const int row, const int col,
int
-main(int argc, char *argv[] ) {
+main(int argc, const char ** argv) {
- struct cmdline_info cmdline;
- FILE* ifp;
- gray* grayrow;
- int rows, cols;
+ struct CmdlineInfo cmdline;
+ FILE * ifP;
+ gray * grayrow;
+ unsigned int rows, cols;
gray maxval;
- char* buf;
- /* pixels_1 and pixels_2 are the array of pixels in the input buffer
- (assuming we are using an input buffer). pixels_1 is the array
- as if the pixels are one byte each. pixels_2 is the array as if
+ char * buf;
+ /* pixels1 and pixels2 are the array of pixels in the input buffer
+ (assuming we are using an input buffer). pixels1 is the array
+ as if the pixels are one byte each. pixels2 is the array as if
they are two bytes each.
*/
- unsigned char *pixels_1;
- unsigned short *pixels_2;
- long nread;
- int row;
+ unsigned char * pixels1;
+ unsigned short * pixels2;
+ long nRead;
+ unsigned int row;
float toskip;
- pgm_init( &argc, argv );
+ pm_proginit(&argc, argv);
- parse_command_line(argc, argv, &cmdline);
+ parseCommandLine(argc, argv, &cmdline);
- ifp = pm_openr(cmdline.inputFileName);
+ ifP = pm_openr(cmdline.inputFileName);
if (cmdline.autosize || cmdline.bottomfirst) {
- buf = pm_read_unknown_size( ifp, &nread );
- pixels_1 = (unsigned char *) buf;
- pixels_2 = (unsigned short *) buf;
+ buf = pm_read_unknown_size(ifP, &nRead);
+ pixels1 = (unsigned char *) buf;
+ pixels2 = (unsigned short *) buf;
} else
buf = NULL;
- compute_image_size(cmdline, nread, &rows, &cols);
+ computeImageSize(cmdline, nRead, &rows, &cols);
if (!buf)
- skip_header(ifp, cmdline.headerskip);
+ skipHeader(ifP, cmdline.headerskip);
toskip = 0.00001;
if (cmdline.maxval == -1)
maxval = (cmdline.bpp == 1 ? (gray) 255 : (gray) 65535);
else
- maxval = (gray) cmdline.maxval;
+ maxval = cmdline.maxval;
+
+ pgm_writepgminit(stdout, cols, rows, maxval, 0);
- pgm_writepgminit( stdout, cols, rows, maxval, 0 );
- grayrow = pgm_allocrow( cols );
+ grayrow = pgm_allocrow(cols);
- for ( row = 0; row < rows; ++row) {
- int col;
+ for (row = 0; row < rows; ++row) {
+ unsigned int col;
unsigned int rowpos; /* index of this row in pixel array */
+
if (cmdline.bottomfirst)
rowpos = (rows-row-1) * cols;
else
rowpos = row * cols;
- for ( col = 0; col < cols; ++col )
+ for (col = 0; col < cols; ++col) {
if (buf) {
if (cmdline.bpp == 1)
- grayrow[col] = pixels_1[rowpos+col];
+ grayrow[col] = pixels1[rowpos+col];
else
- grayrow[col] = pixels_2[rowpos+col];
+ grayrow[col] = pixels2[rowpos+col];
} else {
- grayrow[col] = read_from_file(ifp, cmdline.bpp,
- row, col,
- cmdline.littleendian);
+ grayrow[col] = readFromFile(ifP, cmdline.bpp,
+ row, col,
+ cmdline.littleendian > 0);
}
- for ( toskip += cmdline.rowskip; toskip >= 1.0; toskip -= 1.0 ) {
+ }
+ for (toskip += cmdline.rowskip; toskip >= 1.0; toskip -= 1.0) {
/* Note that if we're using a buffer, cmdline.rowskip is zero */
int val;
- val = getc( ifp );
- if ( val == EOF )
- pm_error( "EOF / read error skipping bytes at the end "
- "of Row %d.", row);
+ val = getc(ifP);
+ if (val == EOF)
+ pm_error("EOF / read error skipping bytes at the end "
+ "of Row %u.", row);
}
- pgm_writepgmrow( stdout, grayrow, cols, maxval, 0 );
+ pgm_writepgmrow(stdout, grayrow, cols, maxval, 0);
}
if (buf)
free(buf);
- pm_close( ifp );
- pm_close( stdout );
+ pm_close(ifP);
+ pm_close(stdout);
- exit( 0 );
+ return 0;
}