int main(int argc, const char ** argv) { FILE * ifP; struct CmdlineInfo cmdline; struct SbigHeader hdr; pm_proginit(&argc, argv); parseCommandLine(argc, argv, &cmdline); ifP = pm_openr(cmdline.inputFileName); readSbigHeader(ifP, &hdr); pm_message("SBIG '%s' %ux%u %s image, saturation level = %u", (hdr.haveCameraType ? hdr.cameraType : "ST-?"), hdr.cols, hdr.rows, hdr.isCompressed ? "compressed" : "uncompressed", hdr.maxval); if (hdr.maxval > PGM_OVERALLMAXVAL) { pm_error("Saturation level (%u levels) is too large" "This program's limit is %u.", hdr.maxval, PGM_OVERALLMAXVAL); } pgm_writepgminit(stdout, hdr.cols, hdr.rows, hdr.maxval, 0); writeRaster(ifP, hdr, stdout); pm_close(ifP); pm_close(stdout); return 0; }
void pnm_writepnminit(FILE * const fileP, int const cols, int const rows, xelval const maxval, int const format, int const forceplain) { bool const plainFormat = forceplain || pm_plain_output; switch (PNM_FORMAT_TYPE(format)) { case PPM_TYPE: ppm_writeppminit(fileP, cols, rows, (pixval) maxval, plainFormat); break; case PGM_TYPE: pgm_writepgminit(fileP, cols, rows, (gray) maxval, plainFormat); break; case PBM_TYPE: pbm_writepbminit(fileP, cols, rows, plainFormat); break; default: pm_error("invalid format argument received by pnm_writepnminit(): %d" "PNM_FORMAT_TYPE(format) must be %d, %d, or %d", format, PBM_TYPE, PGM_TYPE, PPM_TYPE); } }
void PGMImageWriter::beginOfImage(int cols, int rows) { _cols = cols; _outputRow = pgm_allocrow(cols); BOOST_VERIFY(_outputRow != NULL); pgm_writepgminit(_output, cols, rows, 255, 1); }
void pgm_writepgm( FILE* file, gray** grays, int cols, int rows, gray maxval, int forceplain ) { int row; pgm_writepgminit( file, cols, rows, maxval, forceplain ); for ( row = 0; row < rows; row++ ) pgm_writepgmrow( file, grays[row], cols, maxval, forceplain ); }
static void writeMap(const char * const wmapFileName, const gray * const lumamap, xelval const maxval) { FILE * const wmapfP = pm_openw(wmapFileName); pgm_writepgminit(wmapfP, maxval+1, 1, maxval, 0); pgm_writepgmrow(wmapfP, lumamap, maxval+1, maxval, 0); pm_close(wmapfP); }
int main(int argc, char *argv[]) { FILE* ifP; const char * inputFilespec; int eof; ppm_init( &argc, argv ); if (argc-1 > 1) pm_error("The only argument is the (optional) input filename"); if (argc == 2) inputFilespec = argv[1]; else inputFilespec = "-"; ifP = pm_openr(inputFilespec); eof = FALSE; /* initial assumption */ while (!eof) { ppm_nextimage(ifP, &eof); if (!eof) { int rows, cols, format; pixval maxval; pixel* inputRow; gray* outputRow; ppm_readppminit(ifP, &cols, &rows, &maxval, &format); pgm_writepgminit(stdout, cols, rows, maxval, 0); inputRow = ppm_allocrow(cols); outputRow = pgm_allocrow(cols); convertRaster(ifP, cols, rows, maxval, format, inputRow, outputRow, stdout); ppm_freerow(inputRow); pgm_freerow(outputRow); } } pm_close(ifP); pm_close(stdout); return 0; }
int pnm_image_pgmWrite(FILE *fp, pnm_image_t *image) { int i,j; gray *row; if (!image || !fp) return (-1); row = pgm_allocrow(image->cols); for (i=0; i < image->channels; i++) { pgm_writepgminit(fp, image->cols, image->rows, image->maxval, 1 /* plain PGM format file, no raw */); for (j=0; j < image->rows; j++) { int k; for (k=0; k < image->cols; k++) row[k] = (gray) image->pixels[i][j][k]; pgm_writepgmrow(fp, row, image->cols, image->maxval, 1 /* plain PGM format file, no raw */); } } pgm_freerow(row); return (0); }
int main(int argc, char *argv[]) { FILE *in = stdin; FILE *out = stdout; FILE *sig = NULL; char output_name[MAXPATHLEN] = "(stdout)"; char input_name[MAXPATHLEN] = "(stdin)"; char signature_name[MAXPATHLEN]; int i, c, w; int row; int n; double alpha = 0.0; double beta = 0.0; int filter = 0; int method = -1; int level = 0; char filter_name[MAXPATHLEN] = ""; int verbose = 0; gray **image; Image_tree dwts; gray maxval; int rows, cols, format; double *watermark; progname = argv[0]; pgm_init(&argc, argv); wm_init(); while ((c = getopt(argc, argv, "a:b:e:f:F:h?o:s:v:")) != EOF) { switch (c) { case 'a': alpha = atof(optarg); if (alpha <= 0.0) { fprintf(stderr, "%s: alpha factor %f out of range\n", progname, alpha); exit(1); } break; case 'b': beta = atof(optarg); if (beta <= 0.0) { fprintf(stderr, "%s: beta factor %f out of range\n", progname, beta); exit(1); } break; case 'e': method = atoi(optarg); if (method < 0) { fprintf(stderr, "%s: wavelet filtering method %d out of range\n", progname, method); exit(1); } break; case 'f': filter = atoi(optarg); if (filter <= 0) { fprintf(stderr, "%s: filter number %d out of range\n", progname, filter); exit(1); } break; case 'F': strcpy(filter_name, optarg); break; case 'h': case '?': usage(); break; case 'o': if ((out = fopen(optarg, "wb")) == NULL) { fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg); exit(1); } strcpy(output_name, optarg); break; case 's': if ((sig = fopen(optarg, "r")) == NULL) { fprintf(stderr, "%s: unable to open signature file %s\n", progname, optarg); exit(1); } strcpy(signature_name, optarg); break; case 'v': verbose = atoi(optarg); if (verbose < 0) { fprintf(stderr, "%s: verbosity level %d out of range\n", progname, verbose); exit(1); } break; } } argc -= optind; argv += optind; if (argc > 1) { usage(); exit(1); } if (argc == 1 && *argv[0] != '-') { if ((in = fopen(argv[0], "rb")) == NULL) { fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]); exit(1); } else strcpy(input_name, argv[0]); } if (sig) { char line[32]; fgets(line, sizeof(line), sig); if (strspn(line, "WGSG") >= 4) { fscanf(sig, "%d\n", &n); if (alpha == 0.0) fscanf(sig, "%lf\n", &alpha); else fscanf(sig, "%*f\n"); if (beta == 0.0) fscanf(sig, "%lf\n", &beta); else fscanf(sig, "%*f\n"); if (method < 0) fscanf(sig, "%d\n", &method); else fscanf(sig, "%*d\n"); if (filter == 0) fscanf(sig, "%d\n", &filter); else fscanf(sig, "%*d\n"); if (!strcmp(filter_name, "")) fscanf(sig, "%[^\n\r]\n", filter_name); else fscanf(sig, "%*[^\n\r]\n"); } else { fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name); exit(1); } } else { fprintf(stderr, "%s: signature file not specified, use -s file option\n", progname); exit(1); } watermark = malloc(n * sizeof(double)); for (i = 0; i < n; i++) fscanf(sig, "%lf\n", &watermark[i]); fclose(sig); pgm_readpgminit(in, &cols, &rows, &maxval, &format); image = pgm_allocarray(cols, rows); for (row = 0; row < rows; row++) pgm_readpgmrow(in, image[row], cols, maxval, format); fclose(in); // complete decomposition level = find_deepest_level(cols, rows) - 1; // wavelet transform init_dwt(cols, rows, filter_name, filter, level, method); #ifdef POLLEN_STUFF #include "pollen_stuff.c" #endif #ifdef PARAM_STUFF #include "param_stuff.c" #endif dwts = fdwt(image); // build tree for subband selection, calculate subband thresholds init_subbands(dwts); set_subbands_type_beta(HORIZONTAL, beta); set_subbands_type_beta(VERTICAL, beta); calc_subbands_threshold(); w = 0; while (w < n) { Subband_data s; // select subband with max. threshold s = select_subband(); if (verbose > 1) fprintf(stderr, "%s: selected subband %s%d, T=%lf, beta=%lf\n", progname, subband_name(s->type), s->level, s->T, s->beta); // watermark significant coefficients and set them selected // check is entire signature has been embedded c = select_subband_coeff(s); do { double p; if (c < 0) // no more significant coefficients in subband break; p = get_subband_coeff(s, c); if (p < s->Cmax) { if (verbose > 2) fprintf(stderr, "%s: embedding sig. coeff. #%d (= %lf)\n into %s%d coeff. #%d\n", progname, w, watermark[w], subband_name(s->type), s->level, c); p = p + alpha * s->beta * s->T * watermark[w]; set_subband_coeff(s, c, p); w++; } mark_subband_coeff(s, c); // select next significant coefficient c = select_subband_coeff_from(s, c); } while (w < n); // update subband threshold s->T /= 2.0; } free_subbands(); free(watermark); idwt(dwts, image); pgm_writepgminit(out, cols, rows, maxval, 0); for (row = 0; row < rows; row++) pgm_writepgmrow(out, image[row], cols, maxval, 0); fclose(out); pgm_freearray(image, rows); exit(0); }
int main(int argc, char *argv[]) { FILE *in = stdin; FILE *out = stdout; FILE *sig = NULL; char output_name[MAXPATHLEN] = "(stdout)"; char input_name[MAXPATHLEN] = "(stdin)"; char signature_name[MAXPATHLEN]; int c; int row, col; int n; double quality = 0.0; int filter = 0; int method = -1; int level = 0; char filter_name[MAXPATHLEN] = ""; int seed; int verbose = 0; gray **image; Image_tree dwts; gray maxval; int rows, cols, colors, format; progname = argv[0]; pgm_init(&argc, argv); #ifdef __EMX__ _fsetmode(in, "b"); _fsetmode(out, "b"); #endif while ((c = getopt(argc, argv, "e:f:F:h?l:o:q:s:v:")) != EOF) { switch (c) { case 'e': method = atoi(optarg); if (method < 0) { fprintf(stderr, "%s: wavelet filtering method %d out of range\n", progname, method); exit(1); } break; case 'f': filter = atoi(optarg); if (filter <= 0) { fprintf(stderr, "%s: filter number %d out of range\n", progname, filter); exit(1); } break; case 'F': strcpy(filter_name, optarg); break; case 'h': case '?': usage(); break; case 'l': level = atoi(optarg); if (level < 1) { fprintf(stderr, "%s: embedding level out of range\n", progname); exit(1); } break; case 'o': if ((out = fopen(optarg, "wb")) == NULL) { fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg); exit(1); } strcpy(output_name, optarg); break; case 'q': quality = atoi(optarg); if (quality <= 0) { fprintf(stderr, "%s: quality factor %d out of range\n", progname, quality); exit(1); } break; case 's': if ((sig = fopen(optarg, "r")) == NULL) { fprintf(stderr, "%s: unable to open signature file %s\n", progname, optarg); exit(1); } strcpy(signature_name, optarg); break; case 'v': verbose = atoi(optarg); if (verbose < 0) { fprintf(stderr, "%s: verbosity level %d out of range\n", progname, verbose); exit(1); } break; } } argc -= optind; argv += optind; if (argc > 1) { usage(); exit(1); } if (argc == 1 && *argv[0] != '-') if ((in = fopen(argv[0], "rb")) == NULL) { fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]); exit(1); } else strcpy(input_name, argv[0]); if (sig) { char line[32]; fgets(line, sizeof(line), sig); if (strspn(line, "KDSG") >= 4) { fscanf(sig, "%d\n", &n); if (quality == 0.0) fscanf(sig, "%lf\n", &quality); else fscanf(sig, "%*f\n"); if (method < 0) fscanf(sig, "%d\n", &method); else fscanf(sig, "%*d\n"); if (filter == 0) fscanf(sig, "%d\n", &filter); else fscanf(sig, "%*d\n"); if (!strcmp(filter_name, "")) fscanf(sig, "%[^\n\r]\n", filter_name); else fscanf(sig, "%*[^\n\r]\n"); if (level == 0) fscanf(sig, "%d\n", &level); else fscanf(sig, "%*d\n"); fscanf(sig, "%d\n", &seed); srandom(seed); n_signature = NBITSTOBYTES(nbit_signature); fread(signature, sizeof(char), n_signature, sig); fscanf(sig, "\n"); } else { fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name); exit(1); } fclose(sig); } else { fprintf(stderr, "%s: signature file not specified, use -s file option\n", progname); exit(1); } pgm_readpgminit(in, &cols, &rows, &maxval, &format); image = pgm_allocarray(cols, rows); for (row = 0; row < rows; row++) pgm_readpgmrow(in, image[row], cols, maxval, format); fclose(in); // check watermark dimensions and decomposition level // decomposition of image init_dwt(cols, rows, filter_name, filter, level, method); #ifdef POLLEN_STUFF { double alpha, beta; char *alpha_str = getenv("POLLEN_ALPHA"), *beta_str = getenv("POLLEN_BETA"); if (alpha_str && beta_str) { alpha = atof(alpha_str); beta = atof(beta_str); if (alpha < -M_PI || alpha >= M_PI) { fprintf(stderr, "%s: pollen - alpha %f out of range\n", progname, alpha); exit(1); } if (beta < -M_PI || beta >= M_PI) { fprintf(stderr, "%s: pollen - beta %f out of range\n", progname, beta); exit(1); } if (verbose > 7) fprintf(stderr, "%s: pollen - alpha %f, beta %f\n", progname, alpha, beta); dwt_pollen_filter(alpha, beta); } } #endif dwts = fdwt(image); // create 'image' from binary watermark // decomposition of watermark init_dwt(cols, rows, filter_name, filter, 1, method); // dwts = fdwt(watermark); // calculate mean value of image and set alpha // setup of contrast sensitivity matrix // segment detail images at each level // calculate DFT of each segment // compute salience for each segment // calculate gamma or each detail image // embed watermark // reconstruction of watermarked image idwt(dwts, image); pgm_writepgminit(out, cols, rows, maxval, 0); for (row = 0; row < rows; row++) pgm_writepgmrow(out, image[row], cols, maxval, 0); fclose(out); pgm_freearray(image, rows); exit(0); }
int main(int argc, char * argv[]) { struct cmdlineInfo cmdline; FILE * ifP; int cols, rows; int median; enum medianMethod medianMethod; pgm_init(&argc, argv); parseCommandLine(argc, argv, &cmdline); ifP = pm_openr(cmdline.inputFileName); ccolso2 = cmdline.width / 2; crowso2 = cmdline.height / 2; pgm_readpgminit(ifP, &cols, &rows, &maxval, &format); pgm_writepgminit(stdout, cols, rows, maxval, forceplain); /* Allocate space for number of rows in mask size. */ grays = pgm_allocarray(cols, cmdline.height); grayrow = pgm_allocrow(cols); /* Read in and write out initial rows that won't get changed. */ for (row = 0; row < cmdline.height - 1; ++row) { pgm_readpgmrow(ifP, grays[row], cols, maxval, format); /* Write out the unchanged row. */ if (row < crowso2) pgm_writepgmrow(stdout, grays[row], cols, maxval, forceplain); } median = (cmdline.height * cmdline.width) / 2; /* Choose which sort to run. */ if (cmdline.type == MEDIAN_UNSPECIFIED) { if ((maxval / ((cmdline.width * cmdline.height) - 1)) < cmdline.cutoff) medianMethod = HISTOGRAM_SORT_MEDIAN; else medianMethod = SELECT_MEDIAN; } else medianMethod = cmdline.type; switch (medianMethod) { case SELECT_MEDIAN: selectMedian(ifP, cmdline.width, cmdline.height, cols, rows, median); break; case HISTOGRAM_SORT_MEDIAN: histogramSortMedian(ifP, cmdline.width, cmdline.height, cols, rows, median); break; case MEDIAN_UNSPECIFIED: pm_error("INTERNAL ERROR: median unspecified"); } pm_close(ifP); pm_close(stdout); pgm_freearray(grays, cmdline.height); pgm_freerow(grayrow); return 0; }
int main(int argc, char *argv[]) { gray *outrow, maxval; int right, left, down, up; bit **inbits; int rows, cols; FILE *ifd; int row; int width, height; const char * const usage = "<w> <h> [pbmfile]"; pgm_init( &argc, argv ); if (argc > 4 || argc < 3) pm_usage(usage); width = atoi(argv[1]); height = atoi(argv[2]); if (width < 1 || height < 1) pm_error("width and height must be > 0"); left=width/2; right=width-left; up=width/2; down=height-up; if (argc == 4) ifd = pm_openr(argv[3]); else ifd = stdin ; inbits = pbm_readpbm(ifd, &cols, &rows) ; if (width > cols) pm_error("You specified a sample width (%u columns) which is greater " "than the image width (%u columns)", height, rows); if (height > rows) pm_error("You specified a sample height (%u rows) which is greater " "than the image height (%u rows)", height, rows); outrow = pgm_allocrow(cols) ; maxval = MIN(PGM_OVERALLMAXVAL, width*height); pgm_writepgminit(stdout, cols, rows, maxval, 0) ; for (row = 0; row < rows; row++) { int const t = (row > up) ? (row-up) : 0; int const b = (row+down < rows) ? (row+down) : rows; int const onv = height - (t-row+up) - (row+down-b); unsigned int col; for (col = 0; col < cols; col++) { int const l = (col > left) ? (col-left) : 0; int const r = (col+right < cols) ? (col+right) : cols; int const onh = width - (l-col+left) - (col+right-r); int value; int x; value = 0; /* initial value */ for (x = l; x < r; ++x) { int y; for (y = t; y < b; ++y) if (inbits[y][x] == PBM_WHITE) ++value; } outrow[col] = maxval*value/(onh*onv); } pgm_writepgmrow(stdout, outrow, cols, maxval, 0) ; } pm_close(ifd); return 0; }