Esempio n. 1
0
int
main(int    argc,
     char * argv[]) {

    FILE * ifP;
    unsigned char * bitrow;
    unsigned int rows, cols, depth;
    unsigned int padright;
    unsigned int row;
    unsigned int itemCount;
    const char * inputFileName;

    pbm_init(&argc, argv);

    if (argc-1 > 1)
        pm_error("Too many arguments (%u).  "
                 "Only argument is optional input file", argc-1);
    if (argc-1 == 1)
        inputFileName = argv[1];
    else
        inputFileName = "-";
    
    ifP = pm_openr(inputFileName);

    readMgrHeader(ifP, &cols, &rows, &depth, &padright);
    if (depth != 1)
        pm_error("MGR file has depth of %u, must be 1", depth);

    pbm_writepbminit(stdout, cols, rows, 0);

    bitrow = pbm_allocrow_packed(cols + padright);
    
    itemCount = (cols + padright ) / 8;

    for (row = 0; row < rows; ++row) {
        /* The raster formats are nearly identical.
           MGR may have rows padded to 16 or 32 bit boundaries.
        */
        size_t bytesRead;
        bytesRead = fread(bitrow, 1, itemCount, ifP);
        if (bytesRead < itemCount)
            pm_error("fread() failed to read mgr bitmap data");

        pbm_writepbmrow_packed(stdout, bitrow, cols, 0);
    }
    pm_close(ifP);
    pm_close(stdout);
    return 0;
}
Esempio n. 2
0
static void
parseCommandLine(int                 argc, 
                 char **             argv,
                 struct cmdlineInfo *cmdlineP ) {
/*----------------------------------------------------------------------------
   Parse program command line described in Unix standard form by argc
   and argv.  Return the information in the options as *cmdlineP.  

   If command line is internally inconsistent (invalid options, etc.),
   issue error message to stderr and abort program.

   Note that the strings we return are stored in the storage that
   was passed to us as the argv array.  We also trash *argv.
-----------------------------------------------------------------------------*/
    optEntry *option_def = malloc(100*sizeof(optEntry));
        /* Instructions to pm_optParseOptions3 on how to parse our options.
         */
    optStruct3 opt;

    unsigned int option_def_index;

    option_def_index = 0;   /* incremented by OPTENT3 */
    OPTENT3(0, "ccir601",     OPT_FLAG,   NULL,                  
            &cmdlineP->ccir601,       0);

    opt.opt_table = option_def;
    opt.short_allowed = FALSE;  /* We have no short (old-fashioned) options */
    opt.allowNegNum = FALSE;  /* We have no parms that are negative numbers */

    pm_optParseOptions3( &argc, argv, opt, sizeof(opt), 0);
        /* Uses and sets argc, argv, and some of *cmdlineP and others. */


    if (argc-1 !=3)
        pm_error("You must specify 3 arguments.  "
                 "You specified %d", argc-1);
    else {
        int width, height;
        cmdlineP->filenameBase = argv[1];
        width = atoi(argv[2]);
        if (width < 1)
            pm_error("Width must be at least 1.  You specified %d", width);
        height = atoi(argv[3]);
        if (height < 1)
            pm_error("Height must be at least 1.  You specified %d", height);
        cmdlineP->width  = width;
        cmdlineP->height = height;
    }
}
Esempio n. 3
0
static void
getGrayComponent(int * jasperCmptP, jas_image_t * const jasperP) {

    int const rc = 
        jas_image_getcmptbytype(jasperP,
                                JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_GRAY_Y));
    if (rc < 0)
        pm_error("Input says it has Grayscale color space, but contains "
                 "no gray intensity component");
    else
        *jasperCmptP = rc;
    if (jas_image_cmptsgnd(jasperP, 0)) 
        pm_error("Input image says it is grayscale, but has signed values "
                 "for what should be the gray levels.");
}
Esempio n. 4
0
static void
rejectOutOfBounds(unsigned int const cols,
                  unsigned int const rows,
                  int          const leftcol,
                  int          const rightcol,
                  int          const toprow,
                  int          const bottomrow,
                  bool         const pad) {

     /* Reject coordinates off the edge */

    if (!pad) {
        if (leftcol < 0)
            pm_error("You have specified a left edge (%d) that is beyond "
                     "the left edge of the image (0)", leftcol);
        if (leftcol > (int)(cols-1))
            pm_error("You have specified a left edge (%d) that is beyond "
                     "the right edge of the image (%u)", leftcol, cols-1);
        if (rightcol < 0)
            pm_error("You have specified a right edge (%d) that is beyond "
                     "the left edge of the image (0)", rightcol);
        if (rightcol > (int)(cols-1))
            pm_error("You have specified a right edge (%d) that is beyond "
                     "the right edge of the image (%u)", rightcol, cols-1);
        if (toprow < 0)
            pm_error("You have specified a top edge (%d) that is above "
                     "the top edge of the image (0)", toprow);
        if (toprow > (int)(rows-1))
            pm_error("You have specified a top edge (%d) that is below "
                     "the bottom edge of the image (%u)", toprow, rows-1);
        if (bottomrow < 0)
            pm_error("You have specified a bottom edge (%d) that is above "
                     "the top edge of the image (0)", bottomrow);
        if (bottomrow > (int)(rows-1))
            pm_error("You have specified a bottom edge (%d) that is below "
                     "the bottom edge of the image (%u)", bottomrow, rows-1);
    }

    if (leftcol > rightcol)
        pm_error("You have specified a left edge (%d) that is to the right of "
                 "the right edge you specified (%d)",
                 leftcol, rightcol);

    if (toprow > bottomrow)
        pm_error("You have specified a top edge (%d) that is below "
                 "the bottom edge you specified (%d)",
                 toprow, bottomrow);
}
Esempio n. 5
0
static void
interpolate(struct pam * const pamP,
            tuple *      const tuplerow,
            tuple        const first,
            tuple        const last) {

    unsigned int plane;
    
    for (plane = 0; plane < pamP->depth; ++plane) {
        int const spread = last[plane] - first[plane];

        int col;

        if (INT_MAX / pamP->width < abs(spread))
            pm_error("Arithmetic overflow.  You must reduce the width of "
                     "the image (now %u) or the range of color values "
                     "(%u in plane %u) so that their "
                     "product is less than %d",
                     pamP->width, abs(spread), plane, INT_MAX);

        for (col = 0; col < pamP->width; ++col)
            tuplerow[col][plane] =
                first[plane] + (spread * col / (int)pamP->width);
    }
}
Esempio n. 6
0
static void
createTiffGenerator(int          const ofd, 
                    const char * const outFileName,
                    bool         const append,
                    TIFF **      const tifPP) {

    const char * option;

    /* Before 10.12 (November 2002), we set O_NONBLOCK here:

       fcntl( 1, F_SETFL, O_NONBLOCK ) ; 
   
       I have no idea why.  The comment attached said, 

         acooke dec99 - otherwise blocks on read inside 
         next line (Linux i386) 
    */

    validateSeekableOutputFile(ofd, outFileName);

    if (append)
        option = "a";
    else
        option = "w";

    *tifPP = TIFFFdOpen(ofd, outFileName, option);
    if (*tifPP == NULL)
        pm_error("error opening standard output as TIFF file.  "
                 "TIFFFdOpen() failed.");
}
Esempio n. 7
0
static enum colorspace
computeColorSpace(struct jpeg_decompress_struct * const cinfoP,
                  enum inklevel                   const inklevel) {
    
    enum colorspace colorSpace;

    if (cinfoP->out_color_space == JCS_GRAYSCALE)
        colorSpace = GRAYSCALE_COLORSPACE;
    else if (cinfoP->out_color_space == JCS_RGB)
        colorSpace = RGB_COLORSPACE;
    else if (cinfoP->out_color_space == JCS_CMYK) {
        switch (inklevel) {
        case ADOBE:
            colorSpace = CMYK_ADOBE_COLORSPACE; break;
        case NORMAL:
            colorSpace = CMYK_NORMAL_COLORSPACE; break;
        case GUESS:
            colorSpace = CMYK_ADOBE_COLORSPACE; break;
        }
    } else
        pm_error("Internal error: unacceptable output color space from "
                 "JPEG decompressor.");

    return colorSpace;
}
Esempio n. 8
0
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;
}
Esempio n. 9
0
static void
readPpmPalette(const char *   const paletteFileName,
               pixel       (* const ppmPaletteP)[], 
               unsigned int * const paletteSizeP) {

    FILE * pfP;
    pixel ** pixels;
    int cols, rows;
    pixval maxval;
    
    pfP = pm_openr(paletteFileName);

    pixels = ppm_readppm(pfP, &cols, &rows, &maxval);

    pm_close(pfP);
    
    *paletteSizeP = rows * cols;
    if (*paletteSizeP > MAXCOLORS) 
        pm_error("ordered palette image contains %d pixels.  Maximum is %d",
                 *paletteSizeP, MAXCOLORS);

    {
        int j;
        int row;
        j = 0;  /* initial value */
        for (row = 0; row < rows; ++row) {
            int col;
            for (col = 0; col < cols; ++col) 
                (*ppmPaletteP)[j++] = pixels[row][col];
        }
    }
    ppm_freearray(pixels, rows);
}        
Esempio n. 10
0
static struct pfmHeader
makePfmHeader(const struct pam * const pamP,
              float              const scaleFactor,
              enum endian        const endian) {

    struct pfmHeader pfmHeader;

    pfmHeader.width  = pamP->width;
    pfmHeader.height = pamP->height;

    if (strncmp(pamP->tuple_type, "RGB", 3) == 0)
        pfmHeader.color = TRUE;
    else if (strncmp(pamP->tuple_type, "GRAYSCALE", 9) == 0)
        pfmHeader.color = FALSE;
    else if (strncmp(pamP->tuple_type, "BLACKANDWHITE", 13) == 0)
        pfmHeader.color = FALSE;
    else
        pm_error("Invalid PAM input.  Tuple type is '%s'.  "
                 "We understand only RGB* and GRAYSCALE*", pamP->tuple_type);

    pfmHeader.scaleFactor = scaleFactor;
    pfmHeader.endian = endian;

    return pfmHeader;
}
Esempio n. 11
0
static void
createPipeFeeder(void          pipeFeederRtn(int, void *), 
                 void *  const feederParm, 
                 int *   const fdP,
                 pid_t * const pidP) {
/*----------------------------------------------------------------------------
   Create a process and a pipe.  Have the process run program
   'pipeFeederRtn' to fill the pipe and return the file descriptor of the
   other end of the pipe as *fdP.
-----------------------------------------------------------------------------*/
    int pipeToFeed[2];
    pid_t feederPid;

    pipe(pipeToFeed);
    feederPid = fork();
    if (feederPid < 0) {
        pm_error("fork() of stdin feeder failed.  errno=%d (%s)", 
                 errno, strerror(errno));
    } else if (feederPid == 0) {
        /* This is the child -- the stdin feeder process */
        close(pipeToFeed[0]);
        (*pipeFeederRtn)(pipeToFeed[1], feederParm);
        exit(0);
    }
    else {
        /* This is the parent */
        close(pipeToFeed[1]);
        *fdP = pipeToFeed[0];
        *pidP = feederPid;
    }
}
Esempio n. 12
0
struct sourceManager * 
dsCreateSource(const char * const fileName) {

    struct sourceManager * srcP;

    MALLOCVAR(srcP);
    if (srcP == NULL)
        pm_error("Unable to get memory for the Jpeg library source manager.");

    srcP->ifP = pm_openr(fileName);

    srcP->jpegSourceMgr.init_source = dsInitSource;
    srcP->jpegSourceMgr.fill_input_buffer = dsFillInputBuffer;
    srcP->jpegSourceMgr.skip_input_data = dsSkipInputData;
    srcP->jpegSourceMgr.resync_to_restart = jpeg_resync_to_restart;
    srcP->jpegSourceMgr.term_source = dsTermSource;
    
    srcP->prematureEof = FALSE;
    srcP->currentBuffer = srcP->buffer1;
    srcP->nextBuffer = srcP->buffer2;
    srcP->jpegSourceMgr.bytes_in_buffer = 
        fread(srcP->currentBuffer, 1, BUFFER_SIZE, srcP->ifP);
    srcP->jpegSourceMgr.next_input_byte = srcP->currentBuffer;
    srcP->bytesInNextBuffer = 
        fread(srcP->nextBuffer, 1, BUFFER_SIZE, srcP->ifP);

    return srcP;
}
Esempio n. 13
0
static void
cropOneImage(struct cmdlineInfo const cmdline,
             FILE *             const ifP,
             FILE *             const bdfP,
             FILE *             const ofP) {
/*----------------------------------------------------------------------------
   Crop the image to which the stream *ifP is presently positioned
   and write the results to *ofP.  If bdfP is non-null, use the image
   to which stream *bdfP is presently positioned as the borderfile
   (the file that tells us where the existing borders are in the input
   image).  Leave *ifP and *bdfP positioned after the image.

   Both files are seekable.
-----------------------------------------------------------------------------*/
    xelval maxval, bmaxval;
    int format, bformat;
    int rows, cols, brows, bcols;
    bool hasBorders;
    borderSet oldBorder;
        /* The sizes of the borders in the input image */
    cropSet crop;
        /* The crops we have to do on each side */
    xel background;

    pnm_readpnminit(ifP, &cols, &rows, &maxval, &format);

    if (bdfP)
        pnm_readpnminit(bdfP, &bcols, &brows, &bmaxval, &bformat);

    if (bdfP)
        analyzeImage(bdfP, bcols, brows, bmaxval, bformat, cmdline.background,
                     FILEPOS_END,
                     &background, &hasBorders, &oldBorder);
    else
        analyzeImage(ifP, cols, rows, maxval, format, cmdline.background,
                     FILEPOS_BEG,
                     &background, &hasBorders, &oldBorder);

    if (cmdline.verbose) {
        pixel const backgroundPixel = pnm_xeltopixel(background, format);
        pm_message("Background color is %s", 
                   ppm_colorname(&backgroundPixel, maxval, TRUE /*hexok*/));
    }
    if (!hasBorders)
        pm_error("The image is entirely background; "
                 "there is nothing to crop.");

    determineCrops(cmdline, &oldBorder, &crop);

    validateComputableSize(cols, rows, crop);

    if (cmdline.verbose) 
        reportCroppingParameters(crop);

    if (PNM_FORMAT_TYPE(format) == PBM_TYPE)
        writeCroppedPBM(ifP, cols, rows, format, crop, background, ofP);
    else
        writeCroppedNonPbm(ifP, cols, rows, maxval, format, crop,
                           background, ofP);
}
Esempio n. 14
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);
    }
}
Esempio n. 15
0
static IC_Entry 
readICEntry (void) 
{
    IC_Entry entry;

    MALLOCVAR(entry);

    if (entry == NULL)
        pm_error("Unable to allcoate memory for IC entry");

    entry->width         = readU1();
    entry->height        = readU1();
    entry->color_count   = readU1();
    entry->reserved      = readU1();
    entry->planes        = readU2();
    entry->bitcount      = readU2();
    entry->size_in_bytes = readU4();
    entry->file_offset   = readU4();
    entry->colors        = NULL;
    entry->ih            = NULL;
    entry->xorBitmap     = NULL;
    entry->andBitmap     = NULL;
    
    return entry;
}
Esempio n. 16
0
static void
createHist(bool             const colorWanted[3],
           unsigned int     const histWidth,
           unsigned int * (* const histP)[3]) {
/*----------------------------------------------------------------------------
   Allocate the histogram arrays and set each slot count to zero.
-----------------------------------------------------------------------------*/
    unsigned int color;

    for (color = 0; color < 3; ++color) {
        if (colorWanted[color]) {
            unsigned int * hist;
            unsigned int i;
            MALLOCARRAY(hist, histWidth);
            if (hist == NULL)
                pm_error("Not enough memory for histogram arrays (%u bytes)",
                         histWidth * (unsigned)sizeof(hist[0]) * 3);

            for (i = 0; i < histWidth; ++i)
                hist[i] = 0;
            (*histP)[color] = hist;
        } else
            (*histP)[color] = NULL;
    }
}
Esempio n. 17
0
/* ----------------------------------------------------------------------------
** procedure for making the Abel integration for deconvolution of the image
**        y    <-> array with values for deconvolution and results
**        N    <-  width of the array
**        adl  <-  array with pre-calculated weighting factors
*/
static void 
abel ( float *y, int N, double *adl)
{
    register int n;
    double *rho, *rhop;       /* results and new index                       */
    float  *yp;               /* new indizes for the y-array                 */

    MALLOCARRAY(rho, N);
    if( !rho )
        pm_error( "out of memory" );
    rhop = rho;
    yp  = y;

    for (n=0 ; n<N ; n++)
    {
        *(rhop++) = ((*yp++) - Sum(n,rho,N,adl))/(adl[n*N+n]);
/*    *(rhop++) = ((*yp++) - Sum(n,rho,N))/(dr(n,n+0.5,N));  old version */
        if ( *rhop < 0.0 ) *rhop = 0.0;         /*  error correction !       */
/*   if (n > 2) rhop[n-1] = (rho[n-2]+rho[n-1]+rho[n])/3.0;  stabilization*/
    }
    for (n=0 ; n<N ; n++)
        {
            if (( n>=1 )&&( n<N-1 ))
	       (*y++) = ((rho[n-1]*0.5+rho[n]+rho[n+1]*0.5)/2.0);/*1D median filter*/
            else (*y++) = rho[n];
        }
    free(rho);
}
Esempio n. 18
0
static void
computeColorMap(struct pam *   const pamP,
                tuple **       const tupleArray,
                unsigned int * const numColorsP,
                tupletable *   const colormapP,
                tuplehash *    const colorhashP,
                bool           const show) {

    unsigned int numColors;
    tupletable colormap;

    colormap = pnm_computetuplefreqtable(pamP, tupleArray, 0, &numColors);
    if (numColors > 0xFF0)
        pm_error("too many colors; "
                 "use pnmquant to reduce to no more than %u colors", 0xFF0);
    
    if (show) {
        unsigned int colorIndex;
        fprintf(stderr, "Color map:\n");
        fprintf(stderr, "    Index Color\n");
        for (colorIndex = 0; colorIndex < numColors; ++colorIndex) {
            unsigned int plane;
            fprintf(stderr, "    %5u   ", colorIndex);
            for (plane = 0; plane < pamP->depth; ++plane)
                fprintf(stderr, "%3lu ", colormap[colorIndex]->tuple[plane]);
            fprintf(stderr, "\n");
        }
    }

    *colorhashP = pnm_computetupletablehash(pamP, colormap, numColors);

    *numColorsP = numColors;
    *colormapP  = colormap;
}
Esempio n. 19
0
int
main(int argc, char **argv) {

    struct cmdlineInfo cmdline;
    FILE * ifP;
    tuplen ** tuplenarray;
    struct pam inpam;
    double sharpness;

	pnm_init(&argc, argv);

    parseCommandLine(argc, argv, &cmdline);

    ifP = pm_openr(cmdline.inputFilespec);

	tuplenarray = pnm_readpamn(ifP, &inpam, PAM_STRUCT_SIZE(tuple_type));

    if (inpam.height < 3 || inpam.width < 3)
        pm_error("sharpness is undefined for an image less than 3 pixels "
                 "in all directions.  This image is %d x %d",
                 inpam.width, inpam.height);

    computeSharpness(&inpam, tuplenarray, &sharpness);

    printf("Sharpness = %f\n", sharpness);

	pnm_freepamarrayn(tuplenarray, &inpam);
    pm_close(ifP);
	return 0;
}
Esempio n. 20
0
at_bitmap_type
at_bitmap_init(unsigned char * area,
               unsigned short width,
               unsigned short height,
               unsigned int planes) {

    at_bitmap_type bitmap;
    
    if (area)
        bitmap.bitmap = area;
    else {
        if (width * height == 0)
            bitmap.bitmap = NULL;
        else {
            MALLOCARRAY(bitmap.bitmap, width * height * planes);
            if (bitmap.bitmap == NULL)
                pm_error("Unable to allocate %u x %u x %u bitmap array",
                         width, height, planes);
            bzero(bitmap.bitmap,
                  width * height * planes * sizeof(unsigned char));
        }
    }
    
    bitmap.width  = width;
    bitmap.height = height;
    bitmap.np     =  planes;

    return bitmap;  
}
Esempio n. 21
0
static void
validateComputableHeight(int const originTop, 
                         int const overRows) {

    if (originTop < 0) {
        if (originTop < -INT_MAX)
            pm_error("Overlay starts too far above the underlay image to be "
                     "computable.  Overlay can be at most %d rows above "
                     "the underlay.", INT_MAX);
    } else {
        if (INT_MAX - originTop <= overRows)
            pm_error("Too many total rows involved to be computable.  "
                     "You must have a shorter overlay image or compose it "
                     "higher on the underlay image.");
    }
}
Esempio n. 22
0
unsigned int PURE_FN_ATTR
pm_lcm(unsigned int const x, 
       unsigned int const y,
       unsigned int const z,
       unsigned int const limit) {
/*----------------------------------------------------------------------------
  Compute the least common multiple of 'x', 'y', and 'z'.  If it's bigger than
  'limit', though, just return 'limit'.
-----------------------------------------------------------------------------*/
    unsigned int biggest;
    unsigned int candidate;

    if (x == 0 || y == 0 || z == 0)
        pm_error("pm_lcm(): Least common multiple of zero taken.");

    biggest = MAX(x, MAX(y,z));

    candidate = biggest;
    while (((candidate % x) != 0 ||       /* not a multiple of x */
            (candidate % y) != 0 ||       /* not a multiple of y */
            (candidate % z) != 0 ) &&     /* not a multiple of z */
           candidate <= limit)
        candidate += biggest;

    if (candidate > limit) 
        candidate = limit;

    return candidate;
}
Esempio n. 23
0
void
pnm_addtuplefreqoccurrence(struct pam *   const pamP,
                           tuple          const value,
                           tuplehash      const tuplefreqhash,
                           int *          const firstOccurrenceP) {

    unsigned int const hashvalue = pnm_hashtuple(pamP, value);
            
    struct tupleint_list_item * p;

    for (p = tuplefreqhash[hashvalue]; 
         p && !pnm_tupleequal(pamP, p->tupleint.tuple, value);
         p = p->next);

    if (p) {
        /* It's in the hash; just tally one more occurence */
        ++p->tupleint.value;
        *firstOccurrenceP = FALSE;
    } else {
        struct tupleint_list_item * p;

        /* It's not in the hash yet, so add it */
        *firstOccurrenceP = TRUE;

        p = allocTupleIntListItem(pamP);
        if (p == NULL)
            pm_error("out of memory computing hash table");

        pnm_assigntuple(pamP, p->tupleint.tuple, value);
        p->tupleint.value = 1;
        p->next = tuplefreqhash[hashvalue];
        tuplefreqhash[hashvalue] = p;
    }
}
Esempio n. 24
0
static void
getLookup(const char * const lookupFileName, 
          tuple ***    const lookupP,
          struct pam * const lookuppamP) {
/*----------------------------------------------------------------------------
   Get the lookup image (the one that maps integers to tuples, e.g. a
   color index / color map / palette) from the file named 
   'lookupFileName'.

   Return the image as *lookupP and *lookuppamP.
-----------------------------------------------------------------------------*/
    FILE *  lookupfileP;

    struct pam inputLookuppam;
    tuple ** inputLookup;

    lookupfileP = pm_openr(lookupFileName);
    inputLookup = pnm_readpam(lookupfileP, 
                              &inputLookuppam, PAM_STRUCT_SIZE(tuple_type));

    pm_close(lookupfileP);
    
    if (inputLookuppam.height != 1)
        pm_error("The lookup table image must be one row.  "
                 "Yours is %u rows.", 
                 inputLookuppam.height);

    *lookupP = inputLookup;
    *lookuppamP = inputLookuppam;
}
Esempio n. 25
0
static void
parseCommandLine(int argc, 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.
-----------------------------------------------------------------------------*/
    optEntry *option_def = malloc( 100*sizeof( optEntry ) );
        /* Instructions to pm_optParseOptions3 on how to parse our options.
         */
    optStruct3 opt;

    unsigned int option_def_index;

    option_def_index = 0;   /* incremented by OPTENTRY */
    OPTENT3(0, "verbose",   OPT_FLAG,    NULL, &cmdlineP->verbose,  0);

    opt.opt_table = option_def;
    opt.short_allowed = FALSE;  /* We have no short (old-fashioned) options */
    opt.allowNegNum = FALSE;  /* We have no parms that are negative numbers */

    pm_optParseOptions3( &argc, argv, opt, sizeof(opt), 0 );
        /* Uses and sets argc, argv, and some of *cmdlineP and others. */

    if (argc-1 < 1)
        cmdlineP->inputFilespec = "-";
    else if (argc-1 == 1)
        cmdlineP->inputFilespec = argv[1];
    else
        pm_error("Too many arguments.");
}
Esempio n. 26
0
tuplehash
pnm_computetupletablehash(struct pam * const pamP, 
                          tupletable   const tupletable,
                          unsigned int const tupletableSize) {
/*----------------------------------------------------------------------------
   Create a tuple hash containing indices into the tuple table
   'tupletable'.  The hash index for the hash is the value of a tuple;
   the hash value is the tuple table index for the element in the
   tuple table that contains that tuple value.

   Assume there are no duplicate tuple values in the tuple table.

   We allocate space for the main hash table and all the elements of the
   hash chains.
-----------------------------------------------------------------------------*/
    tuplehash tupletablehash;
    unsigned int i;
    bool fits;
    
    tupletablehash = pnm_createtuplehash();

    fits = TRUE;  /* initial assumption */
    for (i = 0; i < tupletableSize && fits; ++i) {
        pnm_addtotuplehash(pamP, tupletablehash, 
                           tupletable[i]->tuple, i, &fits);
    }
    if (!fits) {
        pnm_destroytuplehash(tupletablehash);
        pm_error("Out of memory computing tuple hash from tuple table");
    }
    return tupletablehash;
}
Esempio n. 27
0
static void
getLine(FILE * const ifP,
        char * const buf,
        size_t const size) {

    char * rc;

    rc = fgets(buf, size, ifP);
    if (rc == NULL) {
        if (ferror(ifP))
            pm_error("read error.  fgets() failed, errno=%d (%s)",
                     errno, strerror(errno));
        else
            pm_error("unexpected EOF");
    }
}
Esempio n. 28
0
static void
validateReadableStdout(void) {
/*----------------------------------------------------------------------------
  We validate that Standard Output is readable and fail the program if
  it isn't.

  This is useful because there are situations in which the TIFF library
  must read the output file and if it can't, it fails with an unhelpful
  error message about a file I/O error.  We, on the other hand, produce
  a helpful error message.
-----------------------------------------------------------------------------*/
    int flags;

    flags = fcntl(STDOUT_FILENO, F_GETFL);

    if (flags < 0) {
        /* We couldn't get the flags.  So just assume the file's OK */
    } else {
        if ((flags & O_RDONLY) || (flags & O_RDWR)) {
            /* File is readable.  All is well. */
        } else
            pm_error("Standard Output is not opened for reading.  "
                     "In order to create a multi-image TIFF stream, "
                     "Standard Output must be both readable and writable.");
    }
}
Esempio n. 29
0
static void
getSpecifiedMissingColor(struct pam * const pamP,
                         const char * const colorName,
                         tuple *      const specColorP) {

    tuple specColor;
                             
    specColor = pnm_allocpamtuple(pamP);

    if (colorName) {
        pixel const color = ppm_parsecolor(colorName, pamP->maxval);
        if (pamP->depth == 3) {
            specColor[PAM_RED_PLANE] = PPM_GETR(color);
            specColor[PAM_GRN_PLANE] = PPM_GETG(color);
            specColor[PAM_BLU_PLANE] = PPM_GETB(color);
        } else if (pamP->depth == 1) {
            specColor[0] = ppm_luminosity(color);
        } else {
            pm_error("You may not use -missing with a colormap that is not "
                     "of depth 1 or 3.  Yours has depth %u",
                     pamP->depth);
        }
    }
    *specColorP = specColor;
}
Esempio n. 30
0
static void
validateSeekableOutputFile(int          const ofd,
                           const char * const outFileName) {
/*----------------------------------------------------------------------------
   Validate that the file attached to file descriptor 'ofd' is capable
   of seeking.  If not, fail the program.

   This is useful because the TIFF library requires seekable output and
   fails with an unhelpful error message about a file I/O error if it is
   not.  We, on the other hand, give a helpful error message.

   We leave the file positioned to the beginning.
-----------------------------------------------------------------------------*/
    int rc;

    /* We'd like to get the current position and leave the file positioned
       where we found it.  But that entails the mess with some systems
       having 32 bit file offsets and some having 64 bit file offsets.
    */

    /* Some files can seek ahead, but not back.  So we test for the
       more difficult backward seek.
    */
    lseek(ofd, 1, SEEK_SET);
    rc = lseek(ofd, 0, SEEK_SET);
            
    if (rc < 0)
        pm_error("Output file (%s) is not seekable.  lseek() returned "
                 "errno %d (%s).  "
                 "The TIFF library can write only to "
                 "a seekable file.", 
                 outFileName, errno, strerror(errno));
}