コード例 #1
0
ファイル: ppmtopcx.c プロジェクト: Eleanor66613/CS131
static void
putPcxColorInHash(colorhash_table const cht,
                  pixel           const newPcxColor,
                  unsigned int    const newColorIndex,
                  pixval          const maxval) {

    pixel ppmColor;
        /* Same color as 'newPcxColor', but at the PPM image's color
           resolution: 'maxval'
        */
    int rc;

    PPM_DEPTH(ppmColor, newPcxColor, PCX_MAXVAL, maxval);
        
    rc = ppm_lookupcolor(cht, &ppmColor);

    if (rc == -1)
        /* This color is not in the hash yet, so we just add it */
        ppm_addtocolorhash(cht, &ppmColor, newColorIndex);
    else {
        /* This color is already in the hash.  That's because the
           subject image has less color resolution than PCX (i.e.
           'maxval' is less than PCX_MAXVAL), and two distinct
           colors in the standard palette are indistinguishable at
           subject image color resolution.
           
           So we have to figure out wether color 'newPcxColor' or
           'existingPcxColor' is a better match for 'ppmColor'.
        */

        unsigned int const existingColorIndex = rc;

        pixel idealPcxColor;
        pixel existingPcxColor;

        PPM_DEPTH(idealPcxColor, ppmColor, maxval, PCX_MAXVAL);
        
        PPM_ASSIGN(existingPcxColor, 
                   stdPalette[existingColorIndex].r,
                   stdPalette[existingColorIndex].g,
                   stdPalette[existingColorIndex].b);

        if (PPM_DISTANCE(newPcxColor, idealPcxColor) <
            PPM_DISTANCE(existingPcxColor, idealPcxColor)) {
            /* The new PCX color is a better match.  Make it the new
               translation of image color 'ppmColor'.
            */
            ppm_delfromcolorhash(cht, &ppmColor);
            ppm_addtocolorhash(cht, &ppmColor, newColorIndex);
        }
    }
}
コード例 #2
0
ファイル: pnmtohex.c プロジェクト: shugaoye/fbtest
static void normalize_ppm(xel **pnm, int cols, int rows, int maxval)
{
    int i, j;

    for (i = 0 ; i < rows; i++)
	for (j = 0; j < cols; j++)
	    PPM_DEPTH(pnm[i][j], pnm[i][j], maxval, 255);
}
コード例 #3
0
pixel
ppm_color_from_bk_color(bk_color const bkColor,
                        pixval   const maxval) {

    pixel const color255 = bkColorMap[bkColor];

    pixel retval;

    if (maxval != 255) {
        PPM_DEPTH(retval, color255, 255, maxval);
    } else
        retval = color255;

    return retval;
}
コード例 #4
0
ファイル: ppmtopcx.c プロジェクト: Eleanor66613/CS131
static void
makePcxColormapFromImage(pixel **               const pixels,
                         int                    const cols,
                         int                    const rows,
                         pixval                 const maxval,
                         struct pcxCmapEntry ** const pcxcmapP,
                         colorhash_table *      const chtP,
                         int *                  const colorsP,
                         bool *                 const tooManyColorsP) {
/*----------------------------------------------------------------------------
   Make a colormap (palette) for the PCX header that can be used
   for the image described by 'pixels', 'cols', 'rows', and 'maxval'.

   Return it in newly malloc'ed storage and return its address as
   *pcxcmapP.

   Also return a lookup hash to relate a color in the image to the
   appropriate index in *pcxcmapP.  Return that in newly malloc'ed 
   storage as *chtP.

   Iff there are too many colors to do that (i.e. more than 256), 
   return *tooManyColorsP == TRUE.
-----------------------------------------------------------------------------*/
    int colors;
    colorhist_vector chv;

    pm_message("computing colormap...");

    chv = ppm_computecolorhist(pixels, cols, rows, MAXCOLORS, &colors);
    if (chv == NULL)
        *tooManyColorsP = TRUE;
    else {
        int i;
        struct pcxCmapEntry * pcxcmap;

        *tooManyColorsP = FALSE;

        pm_message("%d colors found", colors);
        
        moveBlackToIndex0(chv, colors);

        MALLOCARRAY_NOFAIL(pcxcmap, MAXCOLORS);

        *pcxcmapP = pcxcmap;

        for (i = 0; i < colors; ++i) {
            pixel p;

            PPM_DEPTH(p, chv[i].color, maxval, PCX_MAXVAL);

            pcxcmap[i].r = PPM_GETR(p);
            pcxcmap[i].g = PPM_GETG(p);
            pcxcmap[i].b = PPM_GETB(p);
        }

        /* Fill it out with black */
        for ( ; i < MAXCOLORS; ++i) {
            pcxcmap[i].r = 0;
            pcxcmap[i].g = 0;
            pcxcmap[i].b = 0;
        }

        *chtP = ppm_colorhisttocolorhash(chv, colors);

        *colorsP = colors;

        ppm_freecolorhist(chv);
    }
}
コード例 #5
0
ファイル: ppmtoxpm.c プロジェクト: chneukirchen/netpbm-mirror
static void
genCmap(colorhist_vector const chv, 
        int              const ncolors, 
        pixval           const maxval, 
        colorhash_table  const colornameHash,
        const char *     const colornames[],
        bool             const includeTransparent,
        cixel_map **     const cmapP, 
        unsigned int *   const transIndexP,
        unsigned int *   const cmapSizeP,
        unsigned int *   const charsPerPixelP) {
/*----------------------------------------------------------------------------
   Generate the XPM color map in cixel_map format (which is just a step
   away from the actual text that needs to be written the XPM file).  The
   color map is defined by 'chv', which contains 'ncolors' colors which
   have maxval 'maxval'.

   Output is in newly malloc'ed storage, with its address returned as
   *cmapP.  We return the number of entries in it as *cmapSizeP.

   This map includes an entry for transparency, whether the raster uses
   it or not.  We return its index as *transIndexP.

   In the map, identify colors by the names given by 'colornameHash' and
   colornames[].  'colornameHash' maps a color in 'pixel' form to an
   index into colornames[]; colornames[] contains the text to identify the
   color in the XPM format.  The colors in 'colornameHash' have maxval 255.
   If a color is not in 'colornameHash', use hexadecimal notation in the
   output colormap.

   But if 'colornameHash' is null, don't use color names at all.  Just use
   hexadecimal notation.

   Return as *charsPerPixel the number of characters, or digits, that
   will be needed in the XPM raster to form an index into this color map.
-----------------------------------------------------------------------------*/
    unsigned int const cmapSize = ncolors + (includeTransparent ? 1 : 0);

    cixel_map * cmap;  /* malloc'ed */
    unsigned int cmapIndex;
    unsigned int charsPerPixel;
    unsigned int xpmMaxval;
    
    MALLOCARRAY(cmap, cmapSize);
    if (cmapP == NULL)
        pm_error("Out of memory allocating %u bytes for a color map.",
                 (unsigned)sizeof(cixel_map) * (ncolors+1));

    xpmMaxval = xpmMaxvalFromMaxval(maxval);

    charsPerPixel = charsPerPixelForSize(cmapSize);

    /*
     * Generate the character-pixel string and the rgb name for each
     * colormap entry. 
     */
    for (cmapIndex = 0; cmapIndex < ncolors; ++cmapIndex) {
        pixel const color = chv[cmapIndex].color;

        pixel color255;
            /* The color, scaled to maxval 255 */
        const char * colorname;  /* malloc'ed */
        /*
         * The character-pixel string is simply a printed number in base
         * MAXPRINTABLE where the digits of the number range from
         * printable[0] .. printable[MAXPRINTABLE-1] and the printed length
         * of the number is 'charsPerPixel'. 
         */
        cmap[cmapIndex].cixel = genNumstr(cmapIndex, charsPerPixel);
        
        PPM_DEPTH(color255, color, maxval, 255);

        if (colornameHash == NULL)
            colorname = NULL;
        else {
            int colornameIndex;
            colornameIndex = ppm_lookupcolor(colornameHash, &color255);
            if (colornameIndex >= 0)
                colorname = strdup(colornames[colornameIndex]);
            else
                colorname = NULL;
        }
        if (colorname)
            cmap[cmapIndex].rgbname = colorname;
        else {
            /* Color has no name; represent it in hexadecimal */

            pixel scaledColor;
            const char * hexString;  /* malloc'ed */

            PPM_DEPTH(scaledColor, color, maxval, xpmMaxval);

            pm_asprintf(&hexString, xpmMaxval == 0x000F ? "#%X%X%X" :
                        xpmMaxval == 0x00FF ? "#%02X%02X%02X" :
                        xpmMaxval == 0x0FFF ? "#%03X%03X%03X" :
                        "#%04X%04X%04X", 
                        PPM_GETR(scaledColor),
                        PPM_GETG(scaledColor),
                        PPM_GETB(scaledColor)
                );

            if (hexString == NULL)
                pm_error("Unable to allocate storage for hex string");
            cmap[cmapIndex].rgbname = hexString;
        }
    }

    if (includeTransparent) {
        /* Add the special transparency entry to the colormap */
        unsigned int const transIndex = ncolors;
        cmap[transIndex].rgbname = strdup("None");
        cmap[transIndex].cixel = genNumstr(transIndex, charsPerPixel);
        *transIndexP = transIndex;
    }
    *cmapP          = cmap;
    *cmapSizeP      = cmapSize;
    *charsPerPixelP = charsPerPixel;
}