/* Last edited on 2006-11-21 03:26:26 by stolfi */ void read_input_file_header(FILE *rd, pnm_image_t **hd_in, pnm_format_t *fmt_in) { /* Allocate header: */ pnm_image_t *hd = (pnm_image_t *)malloc(sizeof(pnm_image_t)); assert(hd != NULL); /* Leave pixel arrays as NULL: */ hd->pgm_pix = (pixval**)NULL; hd->ppm_pix = (pixel**)NULL; /* Read header: */ int fmt; pnm_readpnminit(rd, &(hd->cols), &(hd->rows), &(hd->maxval), &fmt); pm_message("input format = %i%i maxval = %d cols = %d rows = %d", (fmt >> 8)&255, fmt&255, hd->maxval, hd->rows, hd->cols); (*hd_in) = hd; (*fmt_in) = fmt; } void make_output_file_header(pnm_image_t *hd_in, pnm_format_t fmt_in, pnm_image_t **hd_ot, pnm_format_t *fmt_ot) { /* Allocate header: */ pnm_image_t *hd = (pnm_image_t *)malloc(sizeof(pnm_image_t)); assert(hd != NULL); /* Copy input size and type: */ hd->cols = hd_in->cols; hd->rows = hd_in->rows; hd->maxval = hd_in->maxval; hd->pixtype = hd_in->pixtype; /* Leave pixel arrays as NULL: */ hd->pgm_pix = (pixval**)NULL; hd->ppm_pix = (pixel**)NULL; /* Choose format: */ pnm_format_t fmt = fmt_in; unsigned int imv = hd_in->maxval; /* To supress a bogus compiler warning */ switch (fmt_in) { case PPM_FORMAT: case RPPM_FORMAT: if (imv > PPM_MAXMAXVAL) { pm_error("PPM maxval must be at most %d", PPM_MAXMAXVAL); } break; case PGM_FORMAT: case RPGM_FORMAT: if (imv > PGM_OVERALLMAXVAL) { pm_error("PGM maxval must be at most %d", PGM_OVERALLMAXVAL); } break; case PBM_FORMAT: case RPBM_FORMAT: if (imv != 1) { pm_error("PBM maxval should be 1"); } /* Promote PBM to PGM: */ hd->maxval = 255; fmt = ( fmt_in == PBM_FORMAT ? PGM_FORMAT : RPGM_FORMAT ); pm_message("promoting input PBM file to PGM (maxval = %d)", hd->maxval); break; default: pm_error("bad input file format"); } (*hd_ot) = hd; (*fmt_ot) = fmt; } void write_output_file_header(FILE *wr, pnm_image_t *hd_ot, pnm_format_t fmt_ot) { pnm_writepnminit(wr, hd_ot->cols, hd_ot->rows, hd_ot->maxval, fmt_ot, FALSE); }