src/pdfio.c
l_int32
saConvertFilesToPdfData(SARRAY      *sa,
                        l_int32      res,
                        l_float32    scalefactor,
                        l_int32      quality,
                        const char  *title,
                        l_uint8    **pdata,
                        size_t      *pnbytes)
{
char     *fname;
l_uint8  *imdata;
l_int32   i, n, ret, type, npages, scaledres;
size_t    imbytes;
L_BYTEA  *ba;
PIX      *pixs, *pix;
L_PTRA   *pa_data;

    PROCNAME("saConvertFilesToPdfData");

    if (!sa)
        return ERROR_INT("sa not defined", procName, 1);
    if (scalefactor <= 0.0) scalefactor = 1.0;

        /* Generate all the encoded pdf strings */
    n = sarrayGetCount(sa);
    pa_data = ptraCreate(n);
    for (i = 0; i < n; i++) {
        fname = sarrayGetString(sa, i, L_NOCOPY);
        if ((pixs = pixRead(fname)) == NULL) {
            L_ERROR_STRING("image not readable from file %s", procName, fname);
            continue;
        }
        if (scalefactor != 1.0)
            pix = pixScale(pixs, scalefactor, scalefactor);
        else
            pix = pixClone(pixs);
        scaledres = (l_int32)(res * scalefactor);
        if (selectDefaultPdfEncoding(pix, &type)) {
            L_ERROR_STRING("encoding type selection failed for file %s",
                           procName, fname);
            pixDestroy(&pix);
            continue;
        }
        ret = pixConvertToPdfData(pix, type, quality, &imdata, &imbytes,
                                  0, 0, scaledres, NULL, 0, title);
        pixDestroy(&pix);
        pixDestroy(&pixs);
        if (ret) {
            L_ERROR_STRING("pdf encoding failed for %s", procName, fname);
            continue;
        }
        ba = l_byteaInitFromMem(imdata, imbytes);
        if (imdata) FREE(imdata);
        ptraAdd(pa_data, ba);
    }
    ptraGetActualCount(pa_data, &npages);
    if (npages == 0) {
        L_ERROR("no pdf files made", procName);
        ptraDestroy(&pa_data, FALSE, FALSE);
        return 1;
    }

        /* Concatenate them */
    ret = ptraConcatenatePdfToData(pa_data, NULL, pdata, pnbytes);

    ptraGetActualCount(pa_data, &npages);  /* recalculate in case it changes */
    for (i = 0; i < npages; i++) {
        ba = (L_BYTEA *)ptraRemove(pa_data, i, L_NO_COMPACTION);
        l_byteaDestroy(&ba);
    }
    ptraDestroy(&pa_data, FALSE, FALSE);
    return ret;
}
/*!
 *  saConvertFilesToPdfData()
 *
 *      Input:  sarray (of pathnames for images)
 *              res (input resolution of all images)
 *              scalefactor (scaling factor applied to each image)
 *              quality (used for JPEG only; 0 for default (75))
 *              title (<optional> pdf title; if null, taken from the first
 *                     image filename)
 *              &data (<return> output pdf data (of all images)
 *              &nbytes (<return> size of output pdf data)
 *      Return: 0 if OK, 1 on error
 *
 *  Notes:
 *      (1) The images are encoded with G4 if 1 bpp; JPEG if 8 bpp without
 *          colormap and many colors, or 32 bpp; FLATE for anything else.
 */


(1) The images are encoded with G4 if 1 bpp; JPEG if 8 bpp without
colormap and many colors, or 32 bpp; FLATE for anything else.
( 1 ) The images are encoded with G4 if 1 bpp ; JPEG if 8 bpp without colormap and many colors or 32 bpp ; FLATE for anything else .
l_int32
saConvertFilesToPdfData(SARRAY      *sa,
                        l_int32      res,
                        l_float32    scalefactor,
                        l_int32      quality,
                        const char  *title,
                        l_uint8    **pdata,
                        size_t      *pnbytes)
{
char     *fname;
l_uint8  *imdata;
l_int32   i, n, ret, type, npages, scaledres;
size_t    imbytes;
L_BYTEA  *ba;
PIX      *pixs, *pix;
L_PTRA   *pa_data;
    PROCNAME("saConvertFilesToPdfData");
    if (!sa)
        return ERROR_INT("sa not defined", procName, 1);
    if (scalefactor <= 0.0) scalefactor = 1.0;
    n = sarrayGetCount(sa);
    pa_data = ptraCreate(n);
    for (i = 0; i < n; i++) {
        fname = sarrayGetString(sa, i, L_NOCOPY);
        if ((pixs = pixRead(fname)) == NULL) {
            L_ERROR_STRING("image not readable from file %s", procName, fname);
            continue;
        }
        if (scalefactor != 1.0)
            pix = pixScale(pixs, scalefactor, scalefactor);
        else
            pix = pixClone(pixs);
        scaledres = (l_int32)(res * scalefactor);
        if (selectDefaultPdfEncoding(pix, &type)) {
            L_ERROR_STRING("encoding type selection failed for file %s",
                           procName, fname);
            pixDestroy(&pix);
            continue;
        }
        ret = pixConvertToPdfData(pix, type, quality, &imdata, &imbytes,
                                  0, 0, scaledres, NULL, 0, title);
        pixDestroy(&pix);
        pixDestroy(&pixs);
        if (ret) {
            L_ERROR_STRING("pdf encoding failed for %s", procName, fname);
            continue;
        }
        ba = l_byteaInitFromMem(imdata, imbytes);
        if (imdata) FREE(imdata);
        ptraAdd(pa_data, ba);
    }
    ptraGetActualCount(pa_data, &npages);
    if (npages == 0) {
        L_ERROR("no pdf files made", procName);
        ptraDestroy(&pa_data, FALSE, FALSE);
        return 1;
    }
    ret = ptraConcatenatePdfToData(pa_data, NULL, pdata, pnbytes);
    ptraGetActualCount(pa_data, &npages);  
    for (i = 0; i < npages; i++) {
        ba = (L_BYTEA *)ptraRemove(pa_data, i, L_NO_COMPACTION);
        l_byteaDestroy(&ba);
    }
    ptraDestroy(&pa_data, FALSE, FALSE);
    return ret;
}
l_int32 saConvertFilesToPdfData ( SARRAY * sa , l_int32 res , l_float32 scalefactor , l_int32 quality , const char * title , l_uint8 * * pdata , size_t * pnbytes ) { char * fname ; l_uint8 * imdata ; l_int32 i , n , ret , type , npages , scaledres ; size_t imbytes ; L_BYTEA * ba ; PIX * pixs , * pix ; L_PTRA * pa_data ; PROCNAME ( " " ) ; if ( ! sa ) return ERROR_INT ( " " , procName , 1 ) ; if ( scalefactor <= 0.0 ) scalefactor = 1.0 ; n = sarrayGetCount ( sa ) ; pa_data = ptraCreate ( n ) ; for ( i = 0 ; i < n ; i ++ ) { fname = sarrayGetString ( sa , i , L_NOCOPY ) ; if ( ( pixs = pixRead ( fname ) ) == NULL ) { L_ERROR_STRING ( " " , procName , fname ) ; continue ; } if ( scalefactor != 1.0 ) pix = pixScale ( pixs , scalefactor , scalefactor ) ; else pix = pixClone ( pixs ) ; scaledres = ( l_int32 ) ( res * scalefactor ) ; if ( selectDefaultPdfEncoding ( pix , & type ) ) { L_ERROR_STRING ( " " , procName , fname ) ; pixDestroy ( & pix ) ; continue ; } ret = pixConvertToPdfData ( pix , type , quality , & imdata , & imbytes , 0 , 0 , scaledres , NULL , 0 , title ) ; pixDestroy ( & pix ) ; pixDestroy ( & pixs ) ; if ( ret ) { L_ERROR_STRING ( " " , procName , fname ) ; continue ; } ba = l_byteaInitFromMem ( imdata , imbytes ) ; if ( imdata ) FREE ( imdata ) ; ptraAdd ( pa_data , ba ) ; } ptraGetActualCount ( pa_data , & npages ) ; if ( npages == 0 ) { L_ERROR ( " " , procName ) ; ptraDestroy ( & pa_data , FALSE , FALSE ) ; return 1 ; } ret = ptraConcatenatePdfToData ( pa_data , NULL , pdata , pnbytes ) ; ptraGetActualCount ( pa_data , & npages ) ; for ( i = 0 ; i < npages ; i ++ ) { ba = ( L_BYTEA * ) ptraRemove ( pa_data , i , L_NO_COMPACTION ) ; l_byteaDestroy ( & ba ) ; } ptraDestroy ( & pa_data , FALSE , FALSE ) ; return ret ; }
saConvertFilesToPdfData()
      Input:  sarray (of pathnames for images)
              res (input resolution of all images)
              scalefactor (scaling factor applied to each image)
              quality (used for JPEG only; 0 for default (75))
              title (<optional> pdf title; if null, taken from the first
                     image filename)
              &data (<return> output pdf data (of all images)
              &nbytes (<return> size of output pdf data)
      Return: 0 if OK, 1 on error
saConvertFilesToPdfData () Input : sarray ( of pathnames for images ) res ( input resolution of all images ) scalefactor ( scaling factor applied to each image ) quality ( used for JPEG only ; 0 for default ( 75 )) title ( <optional > pdf title ; if null taken from the first image filename ) &data ( <return > output pdf data ( of all images ) &nbytes ( <return > size of output pdf data ) Return : 0 if OK 1 on error
/* Generate all the encoded pdf strings */ /* Concatenate them */ /* recalculate in case it changes */