src/pdfio.c
l_int32
selectDefaultPdfEncoding(PIX      *pix,
                         l_int32  *ptype)
{
l_int32   w, h, d, factor, ncolors;
PIXCMAP  *cmap;

    PROCNAME("selectDefaultPdfEncoding");

    if (!pix)
        return ERROR_INT("pix not defined", procName, 1);
    if (!ptype)
        return ERROR_INT("&type not defined", procName, 1);
    *ptype = L_FLATE_ENCODE;  /* default universal encoding */
    pixGetDimensions(pix, &w, &h, &d);
    cmap = pixGetColormap(pix);
    if (d == 8 && !cmap) {
        factor = L_MAX(1, (l_int32)sqrt((l_float64)(w * h) / 20000.));
        pixNumColors(pix, factor, &ncolors);
        if (ncolors < 20)
            *ptype = L_FLATE_ENCODE;
        else
            *ptype = L_JPEG_ENCODE;
    }
    else if (d == 1)
        *ptype = L_G4_ENCODE;
    else if (cmap || d == 2 || d == 4)
        *ptype = L_FLATE_ENCODE;
    else if (d == 8 || d == 32)
        *ptype = L_JPEG_ENCODE;
    else
        return ERROR_INT("type selection failure", procName, 1);

    return 0;
}
/*!
 *  selectDefaultPdfEncoding()
 *
 *      Input:  pix
 *              &type (<return> L_G4_ENCODE, L_JPEG_ENCODE, L_FLATE_ENCODE)
 *
 *  Notes:
 *      (1) This attempts to choose an encoding for the pix that results
 *          in the smallest file, assuming that if jpeg encoded, it will
 *          use quality = 75.  The decision is approximate, in that
 *          (a) all colormapped images will be losslessly encoded with
 *          gzip (flate), and (b) an image with less than about 20 colors
 *          is likely to be smaller if flate encoded than if encoded
 *          as a jpeg (dct).  For example, an image made by pixScaleToGray3()
 *          will have 10 colors, and flate encoding will give about
 *          twice the compression as jpeg with quality = 75.
 */


(1) This attempts to choose an encoding for the pix that results
in the smallest file, assuming that if jpeg encoded, it will
use quality = 75.  The decision is approximate, in that
(a) all colormapped images will be losslessly encoded with
gzip (flate), and (b) an image with less than about 20 colors
is likely to be smaller if flate encoded than if encoded
as a jpeg (dct).  For example, an image made by pixScaleToGray3()
will have 10 colors, and flate encoding will give about
twice the compression as jpeg with quality = 75.
( 1 ) This attempts to choose an encoding for the pix that results in the smallest file assuming that if jpeg encoded it will use quality = 75 . The decision is approximate in that ( a ) all colormapped images will be losslessly encoded with gzip ( flate ) and ( b ) an image with less than about 20 colors is likely to be smaller if flate encoded than if encoded as a jpeg ( dct ) . For example an image made by pixScaleToGray3 () will have 10 colors and flate encoding will give about twice the compression as jpeg with quality = 75 .
l_int32
selectDefaultPdfEncoding(PIX      *pix,
                         l_int32  *ptype)
{
l_int32   w, h, d, factor, ncolors;
PIXCMAP  *cmap;
    PROCNAME("selectDefaultPdfEncoding");
    if (!pix)
        return ERROR_INT("pix not defined", procName, 1);
    if (!ptype)
        return ERROR_INT("&type not defined", procName, 1);
    *ptype = L_FLATE_ENCODE;  
    pixGetDimensions(pix, &w, &h, &d);
    cmap = pixGetColormap(pix);
    if (d == 8 && !cmap) {
        factor = L_MAX(1, (l_int32)sqrt((l_float64)(w * h) / 20000.));
        pixNumColors(pix, factor, &ncolors);
        if (ncolors < 20)
            *ptype = L_FLATE_ENCODE;
        else
            *ptype = L_JPEG_ENCODE;
    }
    else if (d == 1)
        *ptype = L_G4_ENCODE;
    else if (cmap || d == 2 || d == 4)
        *ptype = L_FLATE_ENCODE;
    else if (d == 8 || d == 32)
        *ptype = L_JPEG_ENCODE;
    else
        return ERROR_INT("type selection failure", procName, 1);
    return 0;
}
l_int32 selectDefaultPdfEncoding ( PIX * pix , l_int32 * ptype ) { l_int32 w , h , d , factor , ncolors ; PIXCMAP * cmap ; PROCNAME ( " " ) ; if ( ! pix ) return ERROR_INT ( " " , procName , 1 ) ; if ( ! ptype ) return ERROR_INT ( " " , procName , 1 ) ; * ptype = L_FLATE_ENCODE ; pixGetDimensions ( pix , & w , & h , & d ) ; cmap = pixGetColormap ( pix ) ; if ( d == 8 && ! cmap ) { factor = L_MAX ( 1 , ( l_int32 ) sqrt ( ( l_float64 ) ( w * h ) / 20000. ) ) ; pixNumColors ( pix , factor , & ncolors ) ; if ( ncolors < 20 ) * ptype = L_FLATE_ENCODE ; else * ptype = L_JPEG_ENCODE ; } else if ( d == 1 ) * ptype = L_G4_ENCODE ; else if ( cmap || d == 2 || d == 4 ) * ptype = L_FLATE_ENCODE ; else if ( d == 8 || d == 32 ) * ptype = L_JPEG_ENCODE ; else return ERROR_INT ( " " , procName , 1 ) ; return 0 ; }
selectDefaultPdfEncoding()
      Input:  pix
              &type (<return> L_G4_ENCODE, L_JPEG_ENCODE, L_FLATE_ENCODE)
selectDefaultPdfEncoding () Input : pix &type ( <return > L_G4_ENCODE L_JPEG_ENCODE L_FLATE_ENCODE )
/* default universal encoding */