Exemple #1
0
static void
makeDjvurleHeader(FILE *       const ofP,
                  struct pam * const pamP,
                  unsigned int const numColors,
                  tupletable   const colormap) {
    
    unsigned int colorIndex;

    fprintf(ofP, "R6\n");
    fprintf(ofP, "%d %d %d\n", pamP->width, pamP->height, numColors);

    for (colorIndex = 0; colorIndex < numColors; ++colorIndex) {
        sample red, grn, blu;

        if (pamP->depth >= 3) {
            red = colormap[colorIndex]->tuple[PAM_RED_PLANE];
            grn = colormap[colorIndex]->tuple[PAM_GRN_PLANE];
            blu = colormap[colorIndex]->tuple[PAM_BLU_PLANE];
        } else
            red = grn = blu = colormap[colorIndex]->tuple[0];
        
        fputc(pnm_scalesample(red, pamP->maxval, 255), ofP);
        fputc(pnm_scalesample(grn, pamP->maxval, 255), ofP);
        fputc(pnm_scalesample(blu, pamP->maxval, 255), ofP);
    }
}
Exemple #2
0
static void
convertRowNative(struct pam * const pamP, 
                 tuple *      const tuplerow, 
                 unsigned int const ps_maxval, 
                 bool         const rle, 
                 unsigned int const padright) {

    unsigned int plane;

    for (plane = 0; plane < pamP->depth; ++plane) {
        unsigned int col;
        for (col= 0; col < pamP->width; ++col) {
            sample const scaledSample = 
                pnm_scalesample(tuplerow[col][plane], pamP->maxval, ps_maxval);

            if ( rle )
                rleputxelval(scaledSample);
            else
                putxelval(scaledSample);
        }
        for (col = 0; col < padright; ++col)
            if (rle)
                rleputxelval(0);
        else
            putxelval(0);
        if (rle)
            rleflush();
    }
}
Exemple #3
0
char*
pam_colorname(struct pam *         const pamP, 
              tuple                const color, 
              enum colornameFormat const format) {

    unsigned int r, g, b;
    FILE* f;
    static char colorname[200];

    r = pnm_scalesample(color[PAM_RED_PLANE], pamP->maxval, 255);
    g = pnm_scalesample(color[PAM_GRN_PLANE], pamP->maxval, 255);
    b = pnm_scalesample(color[PAM_BLU_PLANE], pamP->maxval, 255);

    f = pm_openColornameFile(NULL, format == PAM_COLORNAME_ENGLISH);
    if (f != NULL) {
        unsigned int best_diff;
        bool done;

        best_diff = 32767;
        done = FALSE;
        while (!done) {
            struct colorfile_entry const ce = pm_colorget(f);
            if (ce.colorname) {
                unsigned int const this_diff = 
                    abs((int)r - (int)ce.r) + 
                    abs((int)g - (int)ce.g) + 
                    abs((int)b - (int)ce.b);

                if (this_diff < best_diff) {
                    best_diff = this_diff;
                    strcpy(colorname, ce.colorname);
                }
            } else
                done = TRUE;
        }
        fclose(f);
        if (best_diff != 32767 && 
            (best_diff == 0 || format == PAM_COLORNAME_ENGLISH))
            return colorname;
    }

    /* Color lookup failed, but caller is willing to take an X11-style
       hex specifier, so return that.
    */
    sprintf(colorname, "#%02x%02x%02x", r, g, b);
    return colorname;
}
Exemple #4
0
static void
putMapEntry(struct pam * const pamP, 
            tuple        const value, 
            int          const size) {

    if (size == 15 || size == 16) {
        /* 5 bits each of red, green, and blue.  Watch for byte order */

        tuple const tuple31 = pnm_allocpamtuple(pamP);

        pnm_scaletuple(pamP, tuple31, value, 31);
        {
            int const mapentry = 
                tuple31[PAM_BLU_PLANE] << 0 |
                tuple31[PAM_GRN_PLANE] << 5 |
                tuple31[PAM_RED_PLANE] << 10;
            
            putchar(mapentry % 256);
            putchar(mapentry / 256);
        }
        pnm_freepamtuple(tuple31);
    } else if (size == 8)
        putchar(pnm_scalesample(value[0], 
                                pamP->maxval, TGA_MAXVAL));
    else {
        /* Must be 24 or 32 */
        putchar(pnm_scalesample(value[PAM_BLU_PLANE], 
                                pamP->maxval, TGA_MAXVAL));
        putchar(pnm_scalesample(value[PAM_GRN_PLANE], 
                                pamP->maxval, TGA_MAXVAL));
        putchar(pnm_scalesample(value[PAM_RED_PLANE], 
                                    pamP->maxval, TGA_MAXVAL));
        if (size == 32)
            putchar(pnm_scalesample(value[PAM_TRN_PLANE], 
                                    pamP->maxval, TGA_MAXVAL));
    }
}
Exemple #5
0
static void
putPixel(struct pam *          const pamP,
         tuple                 const tuple, 
         enum TGAbaseImageType const imgType, 
         bool                  const withAlpha,
         tuplehash             const cht) {
/*----------------------------------------------------------------------------
   Write a single pixel of the TGA raster to Standard Output.  The
   pixel is to have color 'tuple'.  The raster has format 'imgType'.
   The color palette from which the specified color is to be drawn, if
   'imgType' indicates use of a color palette, is 'cht'.
-----------------------------------------------------------------------------*/
    if (imgType == TGA_MAP_TYPE) {
        int retval;
        int found;

        pnm_lookuptuple(pamP, cht, tuple, &found, &retval);
        if (!found)
            pm_error("Internal error: color not found in map that was "
                     "generated from all the colors in the image");
        putchar(retval);
    } else {
        if (imgType == TGA_RGB_TYPE && pamP->depth < 3) {
            /* Make RGB pixel out of a single input plane */
            unsigned int plane;
            
            for (plane = 0; plane < 3; ++plane) 
                putchar(pnm_scalesample(tuple[0], 
                                        pamP->maxval, TGA_MAXVAL));
        } else if (imgType == TGA_MONO_TYPE)
            putchar(pnm_scalesample(tuple[0], 
                                    pamP->maxval, TGA_MAXVAL));
        else {
            putchar(pnm_scalesample(tuple[PAM_BLU_PLANE], 
                                    pamP->maxval, TGA_MAXVAL));
            putchar(pnm_scalesample(tuple[PAM_GRN_PLANE], 
                                    pamP->maxval, TGA_MAXVAL));
            putchar(pnm_scalesample(tuple[PAM_RED_PLANE], 
                                    pamP->maxval, TGA_MAXVAL));
            if (withAlpha)
                putchar(pnm_scalesample(tuple[PAM_TRN_PLANE], 
                                        pamP->maxval, TGA_MAXVAL));
        }
    }
}