Ejemplo n.º 1
0
/*
** Return file count
** setvals==1 to set all values based on options
**        ==2 to set only ansi, user interface, exit on complete
**        ==0 to not set any values
** procfiles == 1 to process files
**           == 0 to count files only
*/
int parse_cmd_args(K2PDFOPT_SETTINGS *k2settings,STRBUF *env,STRBUF *cmdline,
                   STRBUF *usermenu,int setvals,int procfiles)

{
    CMDLINEINPUT _cl,*cl;
    STRBUF *allopts,_allopts;
    int filecount,readnext;

    allopts=&_allopts;
    strbuf_init(allopts);
    strbuf_cpy(allopts,env->s);
    strbuf_cat(allopts,cmdline->s);
    strbuf_cat(allopts,usermenu->s);
    cl=&_cl;
    filecount=0;
    cmdlineinput_init(cl,0,NULL,allopts->s);
    readnext=1;
    while (1)
    {
        if (readnext && cmdlineinput_next(cl)==NULL)
            break;
        readnext=1;
        if (!stricmp(cl->cmdarg,"-?") || !stricmp(cl->cmdarg,"-?-"))
        {
            if (setvals==2)
                k2settings->show_usage = cl->cmdarg[2]=='-' ? 0 : 1;
            continue;
        }
        if (!stricmp(cl->cmdarg,"-a") || !stricmp(cl->cmdarg,"-a-"))
        {
            if (setvals>=1)
                ansi_set(cl->cmdarg[2]=='-' ? 0 : 1);
            continue;
        }
        if (!stricmp(cl->cmdarg,"-x") || !stricmp(cl->cmdarg,"-x-"))
        {
            if (setvals>=1)
                k2settings->exit_on_complete=(cl->cmdarg[2]=='-' ? 0 : 1);
            continue;
        }
        if (!strnicmp(cl->cmdarg,"-ui",3))
        {
            if (setvals>=1)
            {
                if (cl->cmdarg[3]!='-')
                    k2settings->query_user_explicit=1;
                k2settings->query_user=(cl->cmdarg[3]!='-') ? 1 : 0;
            }
            continue;
        }
        if (!stricmp(cl->cmdarg,"-evl"))
        {
            if (cmdlineinput_next(cl)==NULL)
                break;
            if (setvals==1)
                k2settings->erase_vertical_lines=atoi(cl->cmdarg);
            continue;
        }
        if (!stricmp(cl->cmdarg,"-vls"))
        {
            if (cmdlineinput_next(cl)==NULL)
                break;
            if (setvals==1)
                k2settings->vertical_line_spacing=atof(cl->cmdarg);
            continue;
        }
        if (!stricmp(cl->cmdarg,"-vm"))
        {
            if (cmdlineinput_next(cl)==NULL)
                break;
            if (setvals==1)
            {
                k2settings->vertical_multiplier=fabs(atof(cl->cmdarg));
                if (k2settings->vertical_multiplier < 0.1)
                    k2settings->vertical_multiplier = 0.1;
            }
            continue;
        }
        if (!stricmp(cl->cmdarg,"-vs"))
        {
            if (cmdlineinput_next(cl)==NULL)
                break;
            if (setvals==1)
                k2settings->max_vertical_gap_inches=atof(cl->cmdarg);
            continue;
        }
        if (!stricmp(cl->cmdarg,"-de"))
        {
            if (cmdlineinput_next(cl)==NULL)
                break;
            if (setvals==1)
                k2settings->defect_size_pts=atof(cl->cmdarg);
            continue;
        }
        if (!stricmp(cl->cmdarg,"-dev"))
        {
            if (cmdlineinput_next(cl)==NULL)
                break;
            if (setvals==2 && !strcmp(cl->cmdarg,"?"))
            {
                devprofiles_echo(stdout);
                k2sys_exit(k2settings,0);
            }
            if (setvals==1)
            {
                if (!k2pdfopt_settings_set_to_device(k2settings,devprofile_get(cl->cmdarg)))
                    aprintf(TTEXT_WARN "\aDevice profile '%s' not known." TTEXT_NORMAL "\n",cl->cmdarg);
            }
            continue;
        }
        if (!stricmp(cl->cmdarg,"-pi") || !stricmp(cl->cmdarg,"-pi-"))
        {
            if (setvals==1)
                k2settings->preserve_indentation=(cl->cmdarg[3]=='-') ? 0 : 1;
            continue;
        }
        if (!strnicmp(cl->cmdarg,"-wrap",5))
        {
            if (setvals==1)
            {
                k2settings->text_wrap=(cl->cmdarg[5]=='-') ? 0 : (cl->cmdarg[5]=='+' ? 2 : 1);
                if (k2settings->text_wrap)
                    k2settings->use_crop_boxes=0;
            }
            continue;
        }
#ifdef HAVE_MUPDF_LIB
        if (!stricmp(cl->cmdarg,"-gs") || !stricmp(cl->cmdarg,"-gs-")
                || !stricmp(cl->cmdarg,"-gs--"))
        {
            if (setvals==1)
                k2settings->user_usegs=(cl->cmdarg[3]=='-' ? (cl->cmdarg[4]=='-' ? -1 : 0) : 1);
            continue;
        }
        if (!stricmp(cl->cmdarg,"-n") || !stricmp(cl->cmdarg,"-n-"))
        {
            if (setvals==1)
            {
                k2settings->use_crop_boxes=(cl->cmdarg[2]=='-') ? 0 : 1;
                if (k2settings->use_crop_boxes)
                {
                    k2settings->text_wrap=0;
#ifdef HAVE_OCR_LIB
                    k2settings->dst_ocr=0;
#endif
                }
            }
            continue;
        }
#endif
        if (!stricmp(cl->cmdarg,"-neg") || !stricmp(cl->cmdarg,"-neg-"))
        {
            if (setvals==1)
                k2settings->dst_negative=(cl->cmdarg[4]=='-') ? 0 : 1;
            continue;
        }
        if (!stricmp(cl->cmdarg,"-r") || !stricmp(cl->cmdarg,"-r-"))
        {
            if (setvals==1)
                k2settings->src_left_to_right=(cl->cmdarg[2]=='-') ? 1 : 0;
            continue;
        }
        if (!strnicmp(cl->cmdarg,"-hy",3))
        {
            if (setvals==1)
                k2settings->hyphen_detect=(cl->cmdarg[3]=='-') ? 0 : 1;
            continue;
        }
        if (!strnicmp(cl->cmdarg,"-ls",3))
        {
            if (setvals==1)
                k2settings->dst_landscape=(cl->cmdarg[3]=='-') ? 0 : 1;
            continue;
        }
        if (!stricmp(cl->cmdarg,"-mode"))
        {
            if (cmdlineinput_next(cl)==NULL)
                break;
            if (setvals==1)
            {
                if (!stricmp(cl->cmdarg,"pdfr")
                        || !stricmp(cl->cmdarg,"copy"))
                {
                    /* -n- -wrap- -col 1 -vb -2 -w -1 -h -1 -dpi 150 -rt 0 -c -t- -f2p -2 */
                    /* -m 0 -om 0 -pl 0 -pr 0 -pt 0 -pb 0 -mc- */
                    k2settings->use_crop_boxes=0;
                    k2settings->text_wrap=0;
                    k2settings->max_columns=1;
                    k2settings->vertical_break_threshold=-2;
                    k2settings->dst_userwidth=-1.0;
                    k2settings->dst_userwidth_units=UNITS_PIXELS;
                    k2settings->dst_userheight=-1.0;
                    k2settings->dst_userheight_units=UNITS_PIXELS;
                    k2settings->dst_dpi=150;
                    k2settings->src_rot=0.;
                    k2settings->dst_color=1;
                    k2settings->src_trim=0;
                    k2settings->dst_fit_to_page=-2;
                    k2settings->mar_left=k2settings->mar_top=k2settings->mar_right=k2settings->mar_bot=0.;
                    k2settings->dst_mar=k2settings->dst_marleft=k2settings->dst_martop=k2settings->dst_marright=k2settings->dst_marbot=0.;
                    k2settings->pad_left=k2settings->pad_top=k2settings->pad_bottom=k2settings->pad_right=0;
                    k2settings->mark_corners=0;
                }
                else if (!stricmp(cl->cmdarg,"fw")
                         || !stricmp(cl->cmdarg,"sopdf")
                         || !stricmp(cl->cmdarg,"fitwidth"))
                {
                    /* -wrap- -col 1 -vb -2 -t -ls */
                    k2settings->use_crop_boxes=1;
                    k2settings->text_wrap=0;
                    k2settings->max_columns=1;
                    k2settings->vertical_break_threshold=-2;
                    k2settings->src_trim=1;
                    k2settings->dst_landscape=1;
                }
                else if (!stricmp(cl->cmdarg,"2col")
                         || !stricmp(cl->cmdarg,"col2"))
                {
                    k2settings->use_crop_boxes=1;
                    k2settings->text_wrap=0;
                    k2settings->max_columns=2;
                    k2settings->vertical_break_threshold=-2;
                    k2settings->src_trim=1;
                }
                else if (!stricmp(cl->cmdarg,"def")
                         || !stricmp(cl->cmdarg,"default")
                         || !stricmp(cl->cmdarg,"std")
                         || !stricmp(cl->cmdarg,"standard"))
                {
                    k2pdfopt_settings_set_to_device(k2settings,devprofile_get("k2"));
                    k2settings->use_crop_boxes=1;
                    k2settings->text_wrap=1;
                    k2settings->max_columns=2;
                    k2settings->vertical_break_threshold=1.75;
                    k2settings->src_rot=SRCROT_AUTO;
                    k2settings->src_trim=1;
                    k2settings->dst_fit_to_page=0;
                    k2settings->mar_left=k2settings->mar_top=k2settings->mar_right=k2settings->mar_bot=0.25;
                    k2settings->dst_mar=k2settings->dst_marleft=k2settings->dst_martop=k2settings->dst_marright=k2settings->dst_marbot=0.02;
                }
                else
                    aprintf(TTEXT_WARN "\a\n** Unknown mode:  %s **\n\n" TTEXT_NORMAL,
                            cl->cmdarg);
            }
            continue;
        }
        if (!stricmp(cl->cmdarg,"-o"))
        {
            if (cmdlineinput_next(cl)==NULL)
                break;
            if (setvals==1)
            {
                strncpy(k2settings->dst_opname_format,cl->cmdarg,127);
                k2settings->dst_opname_format[127]='\0';
            }
            continue;
        }
        if (!stricmp(cl->cmdarg,"-ow") || !stricmp(cl->cmdarg,"-ow-"))
        {
            int always_prompt;
            char *ptr;
            always_prompt = (cl->cmdarg[3]=='-');
            if (((ptr=cmdlineinput_next(cl))==NULL) || !is_a_number(cl->cmdarg))
            {
                readnext=0;
                if (setvals==1)
                    k2settings->overwrite_minsize_mb= always_prompt ? 0 : -1;
                if (ptr==NULL)
                    break;
                continue;
            }
            if (setvals==1)
                k2settings->overwrite_minsize_mb=atof(cl->cmdarg);
            continue;
        }
        if (!stricmp(cl->cmdarg,"-grid"))
        {
            if (cmdlineinput_next(cl)==NULL)
                break;
            if (setvals==1)
            {
                char buf[128];
                double v[3];
                int na,i;

                strncpy(buf,cl->cmdarg,127);
                buf[127]='\0';
                k2settings->src_grid_order=0;
                for (i=0; buf[i]!='\0'; i++)
                {
                    if (tolower(buf[i])=='x')
                        buf[i]=' ';
                    if (buf[i]=='+' && buf[i+1]=='\0')
                        k2settings->src_grid_order=1;
                }
                na=string_read_doubles(buf,v,3);
                if (na>=2)
                {
                    k2settings->src_grid_cols=(int)(v[0]+.5);
                    k2settings->src_grid_rows=(int)(v[1]+.5);
                    if (na>2)
                        k2settings->src_grid_overlap_percentage=(int)(v[2]+.5);
                }
                else
                    k2settings->src_grid_cols = k2settings->src_grid_rows = -1;
                if (k2settings->src_grid_cols>0 && k2settings->src_grid_rows>0)
                {
                    k2settings->use_crop_boxes=1;
                    k2settings->dst_fit_to_page=-2;
                    k2settings->vertical_break_threshold=-2;
                    k2settings->text_wrap=1;
                    k2settings->max_columns=1;
                }
            }
            continue;
        }
        if (!stricmp(cl->cmdarg,"-f2p"))
        {
            if (cmdlineinput_next(cl)==NULL)
                break;
            if (setvals==1)
            {
                k2settings->dst_fit_to_page=atoi(cl->cmdarg);
                if (k2settings->dst_fit_to_page == -2)
                    k2settings->vertical_break_threshold=-2.;
            }
            continue;
        }
        if (!stricmp(cl->cmdarg,"-vb"))
        {
            if (cmdlineinput_next(cl)==NULL)
                break;
            if (setvals==1)
                k2settings->vertical_break_threshold=atof(cl->cmdarg);
            continue;
        }
        if (!stricmp(cl->cmdarg,"-sm") || !stricmp(cl->cmdarg,"-sm-"))
        {
            if (setvals==1)
                k2settings->show_marked_source=(cl->cmdarg[3]=='-' ? 0 : 1);
            continue;
        }
        if (!stricmp(cl->cmdarg,"-bp") || !stricmp(cl->cmdarg,"-bp-"))
        {
            if (cl->cmdarg[3]=='-')
            {
                if (setvals==1)
                    k2settings->dst_break_pages=0;
                continue;
            }
            if (cmdlineinput_next(cl)==NULL)
                break;
            if (is_a_number(cl->cmdarg))
            {
                if (setvals==1)
                    k2settings->dst_break_pages= -1 - (int)(atof(cl->cmdarg)*1000.+.5);
            }
            else
            {
                if (setvals==1)
                    k2settings->dst_break_pages=1;
                readnext=0;
            }
            continue;
        }
        if (!strnicmp(cl->cmdarg,"-fc",3))
        {
            if (setvals==1)
                k2settings->fit_columns=(cl->cmdarg[3]=='-') ? 0 : 1;
            continue;
        }
        if (!stricmp(cl->cmdarg,"-d") || !stricmp(cl->cmdarg,"-d-"))
        {
            if (setvals==1)
                k2settings->dst_dither=(cl->cmdarg[2]=='-') ? 0 : 1;
            continue;
        }
        if (!stricmp(cl->cmdarg,"-c") || !stricmp(cl->cmdarg,"-c-"))
        {
            if (setvals==1)
            {
                k2settings->dst_color=(cl->cmdarg[2]=='-') ? 0 : 1;
                /* wrapbmp_set_color(k2settings->dst_color); */
            }
            continue;
        }
        if (!strnicmp(cl->cmdarg,"-v",2))
        {
            if (setvals==1)
                k2settings->verbose=(cl->cmdarg[2]=='-') ? 0 : 1;
            continue;
        }
        if (!strnicmp(cl->cmdarg,"-png",4))
        {
            if (setvals==1)
                k2settings->jpeg_quality=(cl->cmdarg[4]=='-') ? 90 : -1;
            continue;
        }
        if (!strnicmp(cl->cmdarg,"-mc",3))
        {
            if (setvals==1)
                k2settings->mark_corners=(cl->cmdarg[3]=='-') ? 0 : 1;
            continue;
        }
        if (!stricmp(cl->cmdarg,"-ocrlang") || !stricmp(cl->cmdarg,"-l"))
        {
            if (cmdlineinput_next(cl)==NULL)
                break;
#ifdef HAVE_TESSERACT_LIB
            strncpy(k2settings->dst_ocr_lang,cl->cmdarg,15);
            k2settings->dst_ocr_lang[15]='\0';
#endif
            continue;
        }
        if (!stricmp(cl->cmdarg,"-ocrvis"))
        {
            if (cmdlineinput_next(cl)==NULL)
                break;
#ifdef HAVE_OCR_LIB
            if (setvals==1)
            {
                k2settings->dst_ocr_visibility_flags=0;
                if (in_string(cl->cmdarg,"s")>=0)
                    k2settings->dst_ocr_visibility_flags |= 1;
                if (in_string(cl->cmdarg,"t")>=0)
                    k2settings->dst_ocr_visibility_flags |= 2;
                if (in_string(cl->cmdarg,"b")>=0)
                    k2settings->dst_ocr_visibility_flags |= 4;
            }
#endif
            continue;
        }
        if (!stricmp(cl->cmdarg,"-ocrhmax"))
        {
            if (cmdlineinput_next(cl)==NULL)
                break;
#ifdef HAVE_OCR_LIB
            if (setvals==1)
                k2settings->ocr_max_height_inches=atof(cl->cmdarg);
#endif
            continue;
        }
        if (!stricmp(cl->cmdarg,"-ocr") || !stricmp(cl->cmdarg,"-ocr-"))
        {
#ifndef HAVE_OCR_LIB
            if (setvals==1)
            {
                static int warned=0;
                if (!warned)
                    aprintf(TTEXT_WARN "\a\n** No OCR capability in this compile of k2pdfopt! **\n\n"
                            TTEXT_NORMAL);
                warned=1;
            }
#endif
            if (cl->cmdarg[4]=='-')
            {
#ifdef HAVE_OCR_LIB
                if (setvals==1)
                    k2settings->dst_ocr=0;
#endif
                continue;
            }
            if (cmdlineinput_next(cl)==NULL || !stricmp(cl->cmdarg,"t"))
            {
#ifdef HAVE_OCR_LIB
                if (setvals==1)
                {
                    k2settings->dst_ocr='t';
                    k2settings->use_crop_boxes=0;
                }
#endif
                continue;
            }
            if (!stricmp(cl->cmdarg,"g") || !stricmp(cl->cmdarg,"j"))
            {
#ifdef HAVE_OCR_LIB
                if (setvals==1)
                {
                    k2settings->dst_ocr='g';
                    k2settings->use_crop_boxes=0;
                }
#endif
                continue;
            }
#ifdef HAVE_OCR_LIB
            if (setvals==1)
            {
#ifdef HAVE_TESSERACT_LIB
                k2settings->dst_ocr='t';
#else
                k2settings->dst_ocr='g';
#endif
                k2settings->use_crop_boxes=0;
            }
#endif
            readnext=0;
            continue;
        }
        if (!stricmp(cl->cmdarg,"-t") || !stricmp(cl->cmdarg,"-t-"))
        {
            if (setvals==1)
                k2settings->src_trim=(cl->cmdarg[2]=='-') ? 0 : 1;
            continue;
        }
        if (!stricmp(cl->cmdarg,"-s") || !stricmp(cl->cmdarg,"-s-"))
        {
            if (setvals==1)
                k2settings->dst_sharpen=(cl->cmdarg[2]=='-') ? 0 : 1;
            continue;
        }
        if (!stricmp(cl->cmdarg,"-as"))
        {
            if (setvals==1)
                k2settings->src_autostraighten=4.;
            if (cmdlineinput_next(cl)==NULL)
                break;
            if (is_a_number(cl->cmdarg))
            {
                if (setvals==1)
                    k2settings->src_autostraighten=atof(cl->cmdarg);
            }
            else
                readnext=0;
            if (k2settings->src_autostraighten > 45.)
                k2settings->src_autostraighten = 45.;
            continue;
        }
        if (!stricmp(cl->cmdarg,"-rt"))
        {
            if (cmdlineinput_next(cl)==NULL)
                break;
            if (setvals==1)
            {
                if (!stricmp(cl->cmdarg,"auto"))
                    k2settings->src_rot=SRCROT_AUTO;
                else if (!stricmp(cl->cmdarg,"aep"))
                    k2settings->src_rot=SRCROT_AUTOEP;
                else
                    k2settings->src_rot=atoi(cl->cmdarg);
            }
            continue;
        }
        if (!stricmp(cl->cmdarg,"-crgh"))
        {
            if (cmdlineinput_next(cl)==NULL)
                break;
            if (setvals==1)
            {
                k2settings->column_row_gap_height_in=atof(cl->cmdarg);
                if (k2settings->column_row_gap_height_in < 0.001)
                    k2settings->column_row_gap_height_in = 0.001;
            }
            continue;
        }
        if (!stricmp(cl->cmdarg,"-cgr"))
        {
            if (cmdlineinput_next(cl)==NULL)
                break;
            if (setvals==1)
            {
                k2settings->column_gap_range=atof(cl->cmdarg);
                if (k2settings->column_gap_range < 0.)
                    k2settings->column_gap_range = 0.;
                if (k2settings->column_gap_range > 1.0)
                    k2settings->column_gap_range = 1.0;
            }
            continue;
        }
        if (!stricmp(cl->cmdarg,"-comax"))
        {
            if (cmdlineinput_next(cl)==NULL)
                break;
            if (setvals==1)
            {
                k2settings->column_offset_max=atof(cl->cmdarg);
                if (k2settings->column_offset_max > 1.0)
                    k2settings->column_offset_max = 1.0;
            }
            continue;
        }
        if (!stricmp(cl->cmdarg,"-col"))
        {
            if (cmdlineinput_next(cl)==NULL)
                break;
            if (setvals==1)
            {
                k2settings->max_columns=atoi(cl->cmdarg);
                if (k2settings->max_columns<1)
                    k2settings->max_columns=1;
                if (k2settings->max_columns>2)
                    k2settings->max_columns=4;
            }
            continue;
        }
        if (!strnicmp(cl->cmdarg,"-jpg",4) || !strnicmp(cl->cmdarg,"-jpeg",5))
        {
            int ic;
            ic = (tolower(cl->cmdarg[3])=='g') ? 4 : 5;
            if (cl->cmdarg[ic]=='-')
            {
                if (setvals==1)
                    k2settings->jpeg_quality=-1;
            }
            else
            {
                if (cmdlineinput_next(cl)==NULL)
                {
                    if (setvals==1)
                        k2settings->jpeg_quality=90;
                }
                else if (is_an_integer(cl->cmdarg))
                {
                    if (setvals==1)
                        k2settings->jpeg_quality=atoi(cl->cmdarg);
                }
                else
                {
                    readnext=0;
                    if (setvals==1)
                        k2settings->jpeg_quality=90;
                }
            }
            if (k2settings->jpeg_quality>100)
                k2settings->jpeg_quality=100;
            continue;
        }
        if (!stricmp(cl->cmdarg,"-col"))
        {
            if (cmdlineinput_next(cl)==NULL)
                break;
            if (setvals==1)
            {
                k2settings->max_columns=atoi(cl->cmdarg);
                if (k2settings->max_columns<1)
                    k2settings->max_columns=1;
                if (k2settings->max_columns>2)
                    k2settings->max_columns=4;
            }
            continue;
        }
        if (!stricmp(cl->cmdarg,"-p"))
        {
            if (cmdlineinput_next(cl)==NULL)
                break;
            if (setvals==1)
            {
                strncpy(k2settings->pagelist,cl->cmdarg,1023);
                k2settings->pagelist[1023]='\0';
            }
            continue;
        }
        if (!stricmp(cl->cmdarg,"-bpc"))
        {
            if (cmdlineinput_next(cl)==NULL)
                break;
            if (setvals==1)
            {
                k2settings->dst_bpc=atoi(cl->cmdarg);
                if (k2settings->dst_bpc>=6)
                    k2settings->dst_bpc=8;
                else if (k2settings->dst_bpc>=3)
                    k2settings->dst_bpc=4;
                else if (k2settings->dst_bpc<1)
                    k2settings->dst_bpc=1;
            }
            continue;
        }
        if (!stricmp(cl->cmdarg,"-g"))
        {
            if (cmdlineinput_next(cl)==NULL)
                break;
            if (setvals==1)
            {
                k2settings->dst_gamma=atof(cl->cmdarg);
                if (k2settings->dst_gamma<.01)
                    k2settings->dst_gamma=.01;
                if (k2settings->dst_gamma>100.)
                    k2settings->dst_gamma=100.;
            }
            continue;
        }
        if (!stricmp(cl->cmdarg,"-cg"))
        {
            if (cmdlineinput_next(cl)==NULL)
                break;
            if (setvals==1)
                k2settings->min_column_gap_inches=atof(cl->cmdarg);
            continue;
        }
        if (!stricmp(cl->cmdarg,"-cgmax"))
        {
            if (cmdlineinput_next(cl)==NULL)
                break;
            if (setvals==1)
                k2settings->max_column_gap_inches=atof(cl->cmdarg);
            continue;
        }
        if (!stricmp(cl->cmdarg,"-gtr"))
        {
            if (cmdlineinput_next(cl)==NULL)
                break;
            if (setvals==1)
            {
                k2settings->gtr_in=atof(cl->cmdarg);
                if (k2settings->gtr_in<0.)
                    k2settings->gtr_in=0.;
            }
            continue;
        }
        if (!stricmp(cl->cmdarg,"-gtc"))
        {
            if (cmdlineinput_next(cl)==NULL)
                break;
            if (setvals==1)
            {
                k2settings->gtc_in=atof(cl->cmdarg);
                if (k2settings->gtc_in<0.)
                    k2settings->gtc_in=0.;
            }
            continue;
        }
        if (!stricmp(cl->cmdarg,"-gtw"))
        {
            if (cmdlineinput_next(cl)==NULL)
                break;
            if (setvals==1)
            {
                k2settings->gtw_in=atof(cl->cmdarg);
                if (k2settings->gtw_in<0.)
                    k2settings->gtw_in=0.;
            }
            continue;
        }
        /*
                if (i<argc-1 && !stricmp(cl->cmdarg,"-cd"))
                    {
                    if (setvals==1)
                        {
                        cdthresh=atof(argv[++i]);
                        if (cdthresh<0.)
                            cdthresh=0.;
                        else if (cdthresh>100.)
                            cdthresh=100.;
                        }
                    else
                        i++;
                    continue;
                    }
        */
        if (!stricmp(cl->cmdarg,"-cmax"))
        {
            if (cmdlineinput_next(cl)==NULL)
                break;
            if (setvals==1)
                k2settings->contrast_max=atof(cl->cmdarg);
            continue;
        }
        if (!stricmp(cl->cmdarg,"-ch"))
        {
            if (cmdlineinput_next(cl)==NULL)
                break;
            if (setvals==1)
                k2settings->min_column_height_inches=atof(cl->cmdarg);
            continue;
        }
        if (!stricmp(cl->cmdarg,"-ds"))
        {
            if (cmdlineinput_next(cl)==NULL)
                break;
            if (setvals==1 && atof(cl->cmdarg)>0.)
                k2settings->document_scale_factor=atof(cl->cmdarg);
            continue;
        }
        if (!stricmp(cl->cmdarg,"-idpi"))
        {
            if (cmdlineinput_next(cl)==NULL)
                break;
            if (setvals==1 && atof(cl->cmdarg)!=0.)
                k2settings->user_src_dpi=atof(cl->cmdarg);
            continue;
        }
        if (!stricmp(cl->cmdarg,"-odpi") || !stricmp(cl->cmdarg,"-dpi"))
        {
            if (cmdlineinput_next(cl)==NULL)
                break;
            if (setvals==1)
                k2settings->dst_dpi=atoi(cl->cmdarg);
            continue;
        }
        if (!stricmp(cl->cmdarg,"-jf"))
        {
            if (cmdlineinput_next(cl)==NULL)
                break;
            if (setvals==1)
                k2settings->dst_figure_justify=atoi(cl->cmdarg);
            if (cmdlineinput_next(cl)==NULL)
                break;
            if (!is_a_number(cl->cmdarg))
            {
                readnext=0;
                continue;
            }
            if (setvals==1)
                k2settings->dst_min_figure_height_in=atof(cl->cmdarg);
            continue;
        }
        if (!stricmp(cl->cmdarg,"-j"))
        {
            if (cmdlineinput_next(cl)==NULL)
                break;
            if (setvals==1)
            {
                k2settings->dst_justify=atoi(cl->cmdarg);
                if (in_string(cl->cmdarg,"+")>=0)
                    k2settings->dst_fulljustify=1;
                else if (in_string(&cl->cmdarg[1],"-")>=0)
                    k2settings->dst_fulljustify=0;
                else
                    k2settings->dst_fulljustify=-1;
            }
            continue;
        }
        if (!stricmp(cl->cmdarg,"-dr"))
        {
            if (cmdlineinput_next(cl)==NULL)
                break;
            if (setvals==1)
                k2settings->dst_display_resolution=atof(cl->cmdarg);
            continue;
        }
        if (!stricmp(cl->cmdarg,"-h"))
        {
            if (cmdlineinput_next(cl)==NULL)
                break;
            if (setvals==1)
                set_value_with_units(cl->cmdarg,&k2settings->dst_userheight,&k2settings->dst_userheight_units);
            continue;
        }
        if (!stricmp(cl->cmdarg,"-ws"))
        {
            if (cmdlineinput_next(cl)==NULL)
                break;
            if (setvals==1)
                k2settings->word_spacing=atof(cl->cmdarg);
            continue;
        }
        if (!stricmp(cl->cmdarg,"-wt"))
        {
            if (cmdlineinput_next(cl)==NULL)
                break;
            if (setvals==1)
            {
                k2settings->src_whitethresh=atoi(cl->cmdarg);
                if (k2settings->src_whitethresh>255)
                    k2settings->src_whitethresh=255;
            }
            continue;
        }
        if (!stricmp(cl->cmdarg,"-w"))
        {
            if (cmdlineinput_next(cl)==NULL)
                break;
            if (setvals==1)
                set_value_with_units(cl->cmdarg,&k2settings->dst_userwidth,&k2settings->dst_userwidth_units);
            continue;
        }
        if (!stricmp(cl->cmdarg,"-omb"))
        {
            if (cmdlineinput_next(cl)==NULL)
                break;
            if (setvals==1)
                k2settings->dst_marbot=atof(cl->cmdarg);
            continue;
        }
        if (!stricmp(cl->cmdarg,"-omt"))
        {
            if (cmdlineinput_next(cl)==NULL)
                break;
            if (setvals==1)
                k2settings->dst_martop=atof(cl->cmdarg);
            continue;
        }
        if (!stricmp(cl->cmdarg,"-omr"))
        {
            if (cmdlineinput_next(cl)==NULL)
                break;
            if (setvals==1)
                k2settings->dst_marright=atof(cl->cmdarg);
            continue;
        }
        if (!stricmp(cl->cmdarg,"-oml"))
        {
            if (cmdlineinput_next(cl)==NULL)
                break;
            if (setvals==1)
                k2settings->dst_marleft=atof(cl->cmdarg);
            continue;
        }
        if (!stricmp(cl->cmdarg,"-om"))
        {
            if (cmdlineinput_next(cl)==NULL)
                break;
            if (setvals==1)
            {
                double v[4];
                int na;
                na=string_read_doubles(cl->cmdarg,v,4);
                if (na>=1)
                    k2settings->dst_mar=k2settings->dst_marleft=k2settings->dst_martop=k2settings->dst_marright=k2settings->dst_marbot=v[0];
                if (na>=2)
                    k2settings->dst_martop=k2settings->dst_marright=k2settings->dst_marbot=v[1];
                if (na>=3)
                    k2settings->dst_marright=k2settings->dst_marbot=v[2];
                if (na>=4)
                    k2settings->dst_marbot=v[3];
            }
            continue;
        }
        if (!stricmp(cl->cmdarg,"-mb"))
        {
            if (cmdlineinput_next(cl)==NULL)
                break;
            if (setvals==1)
                k2settings->mar_bot=atof(cl->cmdarg);
            continue;
        }
        if (!stricmp(cl->cmdarg,"-mt"))
        {
            if (cmdlineinput_next(cl)==NULL)
                break;
            if (setvals==1)
                k2settings->mar_top=atof(cl->cmdarg);
            continue;
        }
        if (!stricmp(cl->cmdarg,"-mr"))
        {
            if (cmdlineinput_next(cl)==NULL)
                break;
            if (setvals==1)
                k2settings->mar_right=atof(cl->cmdarg);
            continue;
        }
        if (!stricmp(cl->cmdarg,"-ml"))
        {
            if (cmdlineinput_next(cl)==NULL)
                break;
            if (setvals==1)
                k2settings->mar_left=atof(cl->cmdarg);
            continue;
        }
        if (!stricmp(cl->cmdarg,"-pb"))
        {
            if (cmdlineinput_next(cl)==NULL)
                break;
            if (setvals==1)
                k2settings->pad_bottom=atoi(cl->cmdarg);
            continue;
        }
        if (!stricmp(cl->cmdarg,"-pt"))
        {
            if (cmdlineinput_next(cl)==NULL)
                break;
            if (setvals==1)
                k2settings->pad_top=atoi(cl->cmdarg);
            continue;
        }
        if (!stricmp(cl->cmdarg,"-pr"))
        {
            if (cmdlineinput_next(cl)==NULL)
                break;
            if (setvals==1)
                k2settings->pad_right=atoi(cl->cmdarg);
            continue;
        }
        if (!stricmp(cl->cmdarg,"-pl"))
        {
            if (cmdlineinput_next(cl)==NULL)
                break;
            if (setvals==1)
                k2settings->pad_left=atoi(cl->cmdarg);
            continue;
        }
        if (!stricmp(cl->cmdarg,"-m"))
        {
            if (cmdlineinput_next(cl)==NULL)
                break;
            if (setvals==1)
            {
                double v[4];
                int na;
                na=string_read_doubles(cl->cmdarg,v,4);
                if (na>=1)
                    k2settings->mar_left=k2settings->mar_top=k2settings->mar_right=k2settings->mar_bot=v[0];
                if (na>=2)
                    k2settings->mar_top=k2settings->mar_right=k2settings->mar_bot=v[1];
                if (na>=3)
                    k2settings->mar_right=k2settings->mar_bot=v[2];
                if (na>=4)
                    k2settings->mar_bot=v[3];
            }
            continue;
        }
        if (!strnicmp(cl->cmdarg,"-hq",3))
        {
            if (setvals==1)
                continue;
            if (cl->cmdarg[3]=='-')
            {
                k2settings->dst_dpi=167;
                k2settings->user_src_dpi = -2.0;
                k2settings->dst_userwidth=DEFAULT_WIDTH;
                k2settings->dst_userwidth_units=UNITS_PIXELS;
                k2settings->dst_userheight=DEFAULT_HEIGHT;
                k2settings->dst_userheight_units=UNITS_PIXELS;
            }
            else
            {
                k2settings->dst_dpi=333;
                k2settings->user_src_dpi = -2.0;
                k2settings->dst_userwidth=DEFAULT_WIDTH*2;
                k2settings->dst_userheight=DEFAULT_HEIGHT*2;
                k2settings->dst_userwidth_units=UNITS_PIXELS;
                k2settings->dst_userheight_units=UNITS_PIXELS;
            }
            continue;
        }
        if (!stricmp(cl->cmdarg,"-debug"))
        {
            if (setvals==1)
                k2settings->debug=1;
            if (cmdlineinput_next(cl)==NULL)
                break;
            if (is_an_integer(cl->cmdarg))
            {
                if (setvals==1)
                    k2settings->debug=atoi(cl->cmdarg);
            }
            else
                readnext=0;
            continue;
        }
        /*
        ** UNDOCUMENTED COMMAND-LINE ARGS
        */
        if (!stricmp(cl->cmdarg,"-whmax"))
        {
            if (cmdlineinput_next(cl)==NULL)
                break;
            if (setvals==1)
                k2settings->no_wrap_height_limit_inches=atof(cl->cmdarg);
            continue;
        }
        if (!stricmp(cl->cmdarg,"-arlim"))
        {
            if (cmdlineinput_next(cl)==NULL)
                break;
            if (setvals==1)
                k2settings->no_wrap_ar_limit=atof(cl->cmdarg);
            continue;
        }
        if (!stricmp(cl->cmdarg,"-rwmin"))
        {
            if (cmdlineinput_next(cl)==NULL)
                break;
            if (setvals==1)
                k2settings->little_piece_threshold_inches=atof(cl->cmdarg);
            continue;
        }
        filecount++;
        /*
        if (filecount==1 && firstfile!=NULL)
            {
            strncpy(firstfile,cl->cmdarg,255);
            firstfile[255]='\0';
            }
        */
        if (procfiles)
            k2pdfopt_proc_wildarg(k2settings,cl->cmdarg);
    }
    strbuf_free(allopts);
    return(filecount);
}
Ejemplo n.º 2
0
/*
** k2pdfopt_proc_one() is the main source file processing function in k2pdfopt.
** 
** Depending on the value of rot_deg, it either determines the correct rotation of
** the passed file, or it processes it and converts it.
**
** The basic idea is to parse the source document into rectangular regions
** (held in the BMPREGION structures) and then to place these regions into
** the master destination bitmap (kept track of in MASTERINFO structure).
** You can think of this bitmap as a sort of "infinitely scrolling" output
** bitmap which is then cut into output pages.
**
** The bmpregion_source_page_add() function parses the source file.
**
** The masterinfo_publish() cuts the output bitmap into destination pages.
**
** If rot_deg == SRCROT_AUTO, then the rotation correction of the source
** file is computed and returned, but no other processing is done.
**
** Otherwise, the source file is processed.
*/
static double k2pdfopt_proc_one(K2PDFOPT_SETTINGS *k2settings0,char *filename,double rot_deg,
                                K2PDFOPT_OUTPUT *k2out)

    {
    static K2PDFOPT_SETTINGS _k2settings,*k2settings;
    static MASTERINFO _masterinfo,*masterinfo;
    static PDFFILE _mpdf,*mpdf;
    char dstfile[MAXFILENAMELEN];
    char markedfile[MAXFILENAMELEN];
    char rotstr[128];
    WILLUSBITMAP _src,*src;
    WILLUSBITMAP _srcgrey,*srcgrey;
    WILLUSBITMAP _marked,*marked;
    WILLUSBITMAP preview_internal;
    int i,status,pw,np,src_type,second_time_through,or_detect,orep_detect,preview;
    int pagecount,pagestep,pages_done,local_tocwrites;
    int errcnt,pixwarn;
    FILELIST *fl,_fl;
    int folder,dpi;
    double size,bormean;
    char *mupdffilename;
    extern int k2mark_page_count;
    static char *funcname="k2pdfopt_proc_one";
    static char *readerr=TTEXT_WARN "\a\n ** ERROR reading page %d from " TTEXT_BOLD2 "%s" TTEXT_WARN ".\n\n" TTEXT_NORMAL;
    static char *readlimit=TTEXT_WARN "\a\n ** (No more read errors will be echoed for file %s.)\n\n" TTEXT_NORMAL;
/*
extern void willus_mem_debug_update(char *);
*/

#if (WILLUSDEBUGX & 1)
printf("@k2pdfopt_proc_one(%s)\n",filename);
#endif
/*
printf("@k2pdfopt_proc_one(filename='%s', rot_deg=%g, preview_bitmap=%p)\n",filename,rot_deg,k2out->bmp);
*/

    /*
    ** Check to see if we're only echoing page info
    */
    if (k2settings0->info)
        {
#ifdef HAVE_MUPDF_LIB
        char *buf;
        int *pagelist;
        pagelist_get_array(&pagelist,k2settings0->pagelist);
/*
{
int i;
for (i=0;pagelist!=NULL&&pagelist[i]>=0;i++)
printf("pagelist[%d]=%d\n",i,pagelist[i]);
printf("pagelist[%d]=%d\n",i,pagelist[i]);
}
*/
        wmupdfinfo_get(filename,pagelist,&buf);
        printf("%s",buf);
        if (buf!=NULL)
            free(buf);
        if (pagelist!=NULL)
            free(pagelist);
#else
        printf("FILE: %s\n",filename);
        printf("Cannot print file info.  MuPDF not compiled into application.\n");
#endif
        return(0.);
        }
    local_tocwrites=0;
    k2out->status = 1;
    k2settings=&_k2settings;
    k2pdfopt_settings_copy(k2settings,k2settings0);
#ifdef HAVE_K2GUI
    if (k2gui_active())
        k2gui_cbox_set_filename(filename);
#endif
    mpdf=&_mpdf;
    /* Must be called once per conversion to init margins / devsize / output size */
    k2pdfopt_settings_sanity_check(k2settings);
    k2pdfopt_settings_new_source_document_init(k2settings);
    errcnt=0;
    pixwarn=0;
    mupdffilename=_masterinfo.srcfilename;
    strncpy(mupdffilename,filename,MAXFILENAMELEN-1);
    mupdffilename[MAXFILENAMELEN-1]='\0';
    or_detect=OR_DETECT(rot_deg);
    orep_detect=OREP_DETECT(k2settings);
    if ((fabs(k2settings->src_rot-SRCROT_AUTO)<.5 || orep_detect) && !or_detect)
        second_time_through=1;
    else
        second_time_through=0;
    /* Don't care about rotation if just echoing page count */
    if (k2settings->echo_source_page_count && second_time_through==0)
        return(0.);
    if (or_detect && k2settings->src_dpi>300)
        dpi=300;
    else
        dpi=k2settings->src_dpi;
    folder=(wfile_status(filename)==2);
    /*
    if (folder && !second_time_through)
        k2printf("Processing " TTEXT_INPUT "BITMAP FOLDER %s" TTEXT_NORMAL "...\n",
               filename);
    */
    /*
    else
        k2printf("Processing " TTEXT_BOLD2 "PDF FILE %s" TTEXT_NORMAL "...\n",
               filename);
    */
    fl=&_fl;
    filelist_init(fl);
    if (folder)
        {
        char basename[MAXFILENAMELEN];
        static char *iolist[]={"*.png","*.jpg",""};
        static char *eolist[]={""};

        wfile_basespec(basename,filename);
        if (!second_time_through)
            k2printf("Searching folder " TTEXT_BOLD2 "%s" TTEXT_NORMAL " ... ",basename);
        fflush(stdout);
        filelist_fill_from_disk(fl,filename,iolist,eolist,0,0);
        if (fl->n<=0)
            {
            if (!second_time_through)
                k2printf(TTEXT_WARN "\n** No bitmaps found in folder %s.\n\n" 
                        TTEXT_NORMAL,filename);
            k2out->status=2;
            return(0.);
            }
        if (!second_time_through)
            k2printf("%d bitmaps found in %s.\n",(int)fl->n,filename);
        filelist_sort_by_name(fl);
        }
    src=&_src;
    srcgrey=&_srcgrey;
    marked=&_marked;
    bmp_init(src);
    bmp_init(srcgrey);
    bmp_init(marked);
    pw=0;
    src_type = get_source_type(filename);
#ifndef HAVE_DJVU_LIB
    if (src_type==SRC_TYPE_DJVU)
        {
        if (!or_detect)
            k2printf(TTEXT_WARN
                    "\a\n\n** DjVuLibre not compiled into this version of k2pdfopt. **\n\n"
                          "** Cannot process file %s. **\n\n" TTEXT_NORMAL,filename);
        k2out->status=3;
        return(0.);
        }
#endif
    if (src_type==SRC_TYPE_PS)
        k2settings->usegs=1;
    /*
    ** Turn off native PDF output if source is not PDF
    */
    if (src_type!=SRC_TYPE_PDF)
        {
        if (k2settings->use_crop_boxes && !or_detect)
            k2printf(TTEXT_WARN
                     "\n** Native PDF output mode turned off on file %s. **\n"
                     "** (It is not a PDF file.) **\n\n",filename);
        k2settings->use_crop_boxes=0;
#ifdef HAVE_OCR_LIB
        if (k2settings->dst_ocr=='m')
            k2settings->dst_ocr=0;
#endif
        }
    masterinfo=&_masterinfo;
    masterinfo_init(masterinfo,k2settings);
    if (k2settings->preview_page!=0 && !or_detect)
        {
        preview=1;
        if (k2out->bmp!=NULL)
            masterinfo->preview_bitmap=k2out->bmp;
        else
            {
            masterinfo->preview_bitmap=&preview_internal;
            bmp_init(masterinfo->preview_bitmap);
            }
        }
    else
        preview=0;
    if (!or_detect && !preview)
        {
        static int dstfilecount=0;

        wfile_newext(dstfile,filename,"");
        dstfilecount++;
        filename_substitute(dstfile,k2settings->dst_opname_format,filename,dstfilecount,"pdf");
#ifdef HAVE_OCR_LIB
        if (k2settings->ocrout[0]!='\0' && k2settings->dst_ocr)
            filename_substitute(masterinfo->ocrfilename,k2settings->ocrout,filename,dstfilecount,"txt");
        else
#endif
            masterinfo->ocrfilename[0]='\0';
        if (!filename_comp(dstfile,filename))
            {
            k2printf(TTEXT_WARN "\n\aSource file and ouput file have the same name!" TTEXT_NORMAL "\n\n");
            k2printf("    Source file = '%s'\n",filename);
            k2printf("    Output file = '%s'\n",dstfile);
            k2printf("    Output file name format string = '%s'\n",k2settings->dst_opname_format);
            k2printf("\nOperation aborted.\n");
            k2sys_exit(k2settings,50);
            }
        if ((status=overwrite_fail(dstfile,k2settings->overwrite_minsize_mb))!=0)
            {
            masterinfo_free(masterinfo,k2settings);
            if (folder)
                filelist_free(fl);
            if (status<0)
                k2sys_exit(k2settings,20);
            k2out->status=4;
            return(0.);
            }
        {
        int can_write;
        if (!k2settings->use_crop_boxes)
            can_write = (pdffile_init(&masterinfo->outfile,dstfile,1)!=NULL);
        else
            {
            FILE *f1;
            f1 = wfile_fopen_utf8(dstfile,"w");
            can_write = (f1!=NULL);
            if (f1!=NULL)
                {
                fclose(f1);
                wfile_remove_utf8(dstfile);
                }
            if (!can_write)
                {
                k2printf(TTEXT_WARN "\n\aCannot open PDF file %s for output!" TTEXT_NORMAL "\n\n",dstfile);
#ifdef HAVE_K2GUI
                if (k2gui_active())
                    {
                    k2gui_okay("Failed to open output file",
                               "Cannot open PDF file %s for output!\n"
                               "Maybe another application has it open already?\n"
                               "Conversion failed!",dstfile);
                    k2out->status=4;
                    return(0.);
                    }
#endif
                k2sys_exit(k2settings,30);
                }
            }
        }
        k2out->outname=NULL;
        /* Return output file name in k2out for GUI */
        willus_mem_alloc((double **)&k2out->outname,(long)(strlen(dstfile)+1),funcname);
        if (k2out->outname!=NULL)
            strcpy(k2out->outname,dstfile);
        if (k2settings->use_crop_boxes)
            pdffile_close(&masterinfo->outfile);
        if (k2settings->show_marked_source)
            {
            filename_substitute(markedfile,"%s_marked",filename,0,"pdf");
            if (pdffile_init(mpdf,markedfile,1)==NULL)
                {
                k2printf(TTEXT_WARN "\n\aCannot open PDF file %s for marked output!" TTEXT_NORMAL "\n\n",markedfile);
                k2sys_exit(k2settings,40);
                }
            }
        }
    if (src_type==SRC_TYPE_PDF || src_type==SRC_TYPE_DJVU)
        {
        np=file_numpages(filename,mupdffilename,src_type,&k2settings->usegs);
#ifdef HAVE_MUPDF_LIB
        if (src_type==SRC_TYPE_PDF)
            {
            /* Get bookmarks / outline from PDF file */
            if (!or_detect && k2settings->use_toc!=0 && !toclist_valid(k2settings->toclist,NULL))
                {
                masterinfo->outline=wpdfoutline_read_from_pdf_file(mupdffilename);
                /* Save TOC if requested */
                if (k2settings->tocsavefile[0]!='\0')
                    {
                    FILE *f;
                    f=fopen(k2settings->tocsavefile,tocwrites==0?"w":"a");
                    if (f!=NULL)
                        {
                        int i;
                        fprintf(f,"%sFILE: %s\n",tocwrites==0?"":"\n\n",mupdffilename);
                        for (i=strlen(mupdffilename)+6;i>0;i--)
                            fputc('-',f);
                        fprintf(f,"\n");
                        if (masterinfo->outline!=NULL)
                            wpdfoutline_echo2(masterinfo->outline,0,f);
                        else
                            fprintf(f,"(No outline info in file.)\n");
                        fclose(f);
                        tocwrites++;
                        local_tocwrites++;
                        }
                    }
                }
            }
#endif
        }
    else if (src_type==SRC_TYPE_BITMAPFOLDER)
        np=fl->n;
    else
        np=-1;
    if (k2settings->echo_source_page_count)
        {
        printf("\"%s\" page count = %d\n",mupdffilename,np);
        masterinfo_free(masterinfo,k2settings);
        if (folder)
            filelist_free(fl);
        return(0.);
        }
    masterinfo->srcpages = np;
    if (!or_detect && toclist_valid(k2settings->toclist,stdout))
        {
        if (pagelist_valid_page_range(k2settings->toclist))
            masterinfo->outline=wpdfoutline_from_pagelist(k2settings->toclist,masterinfo->srcpages);
        else
            masterinfo->outline=wpdfoutline_read_from_text_file(k2settings->toclist);
        }
    pagecount = np<0 ? -1 : double_pagelist_count(k2settings->pagelist,k2settings->pagexlist,np);
#ifdef HAVE_K2GUI
    if (k2gui_active())
        {
        k2gui_cbox_set_num_pages(pagecount<0 ? 1 : pagecount);
        k2gui_cbox_set_pages_completed(0,NULL);
        }
#endif
    if (pagecount<0 || !or_detect)
        pagestep=1;
    else
        {
        pagestep=pagecount/10;
        if (pagestep<1)
            pagestep=1;
        }
    pages_done=0;
    if (np>0 && pagecount==0)
        {
        if (!second_time_through)
            k2printf("\a\n" TTEXT_WARN "No %ss to convert (-p %s -px %s)!" TTEXT_NORMAL "\n\n",
                     folder?"file":"page",k2settings->pagelist,k2settings->pagexlist);
        masterinfo_free(masterinfo,k2settings);
        if (folder)
            filelist_free(fl);
        k2out->status=5;
        return(0.);
        }
    if (!second_time_through)
        {
        k2printf("Reading ");
        if (pagecount>0)
           {
           if (pagecount<np)
               k2printf("%d out of %d %s%s",pagecount,np,folder?"file":"page",np>1?"s":"");
           else
               k2printf("%d %s%s",np,folder?"file":"page",np>1?"s":"");
           }
        else
           k2printf("%ss",folder?"file":"page");
        k2printf(" from " TTEXT_BOLD2 "%s" TTEXT_NORMAL " ...\n",filename);
        }
    if (or_detect)
        k2printf("\nDetecting document orientation ... ");
    bormean=1.0;
    for (i=0;1;i+=pagestep)
        {
        char bmpfile[MAXFILENAMELEN];
        int pageno,nextpage;
/*
sprintf(bmpfile,"i=%d",i);
willus_mem_debug_update(bmpfile);
*/
        pageno=0;
        if (pagecount>0 && i+1>pagecount)
            break;
        pageno = double_pagelist_page_by_index(k2settings->pagelist,k2settings->pagexlist,i,np);
        nextpage = (i+2>pagecount) ? -1 : double_pagelist_page_by_index(k2settings->pagelist,
                                                             k2settings->pagexlist,i+1,np);
        /* Removed in v2.32 */
        /* This always returned non-zero */
        /*
        if (!pagelist_page_by_index(k2settings->pagelist,pageno,np))
            continue;
        */
        if (folder)
            {
            if (pageno-1>=fl->n)
                continue;
            wfile_fullname(bmpfile,fl->dir,fl->entry[pageno-1].name);
            status=bmp_read(src,bmpfile,stdout);
            if (status<0)
                {
                if (!second_time_through)
                    k2printf(TTEXT_WARN "\n\aCould not read file %s.\n" TTEXT_NORMAL,bmpfile);
                continue;
                }
            }
        else
            { 
            double npix;

            /* If not a PDF/DJVU/PS file, only read it once. */
            if (i>0 && src_type!=SRC_TYPE_PDF && src_type!=SRC_TYPE_DJVU
                    && src_type!=SRC_TYPE_PS)
                break;

            /* Pre-read at low dpi to check bitmap size */
            wsys_set_decimal_period(1);
            status=bmp_get_one_document_page(src,k2settings,src_type,mupdffilename,pageno,10.,8,
                                             stdout);
            wsys_set_decimal_period(1);
            if (status<0)
                {
                errcnt++;
                if (errcnt<=10)
                    {
                    k2printf(readerr,pageno,filename);
                    if (errcnt==10)
                        k2printf(readlimit,filename);
                    }
                /* Error reading PS probably means we've run out of pages. */
                if (src_type==SRC_TYPE_PS)
                    break;
                continue;
                }

            /* Sanity check the bitmap size */
            npix = (double)(dpi/10.)*(dpi/10.)*src->width*src->height;
            if (npix > 2.5e8 && !pixwarn)
                {
                int ww,hh;
                ww=(int)((double)(dpi/10.)*src->width+.5);
                hh=(int)((double)(dpi/10.)*src->height+.5);
                k2printf("\a\n" TTEXT_WARN "\n\a ** Source resolution is very high (%d x %d pixels)!\n"
                        "    You may want to reduce the -odpi or -idpi setting!\n"
                        "    k2pdfopt may crash when reading the source file..."
                        TTEXT_NORMAL "\n\n",ww,hh);
                pixwarn=1;
                }

            /* Read again at nominal source dpi */
            wsys_set_decimal_period(1);
            if (k2settings_need_color_initially(k2settings))
                status=bmp_get_one_document_page(src,k2settings,src_type,mupdffilename,pageno,
                                                 dpi,24,stdout);
            else
                status=bmp_get_one_document_page(src,k2settings,src_type,mupdffilename,pageno,
                                                 dpi,8,stdout);
            wsys_set_decimal_period(1);
            if (status<0)
                {
                errcnt++;
                if (errcnt<=10)
                    {
                    k2printf(readerr,pageno,filename);
                    if (errcnt==10)
                        aprintf(readlimit,filename);
                    }
                /* Error reading PS probably means we've run out of pages. */
                if (src_type==SRC_TYPE_PS)
                    break;
                continue;
                }
            }
        k2mark_page_count = i+1;

        {
        BMPREGION region;
        int mstatus;

        /* Got Good Page Render */
        bmpregion_init(&region);
        bmpregion_k2pagebreakmarks_allocate(&region);
        mstatus=masterinfo_new_source_page_init(masterinfo,k2settings,src,srcgrey,marked,
                                 &region,rot_deg,&bormean,rotstr,pageno,nextpage,stdout);
        if (mstatus==0)
            {
            /* v2.15 -- memory leak fix */
            bmpregion_free(&region);
            pages_done++;
            continue;
            }
        if (!preview)
            k2printf("\n" TTEXT_HEADER "SOURCE PAGE %d",pageno);
        if (pagecount>0)
            {
            if (!preview)
                {
                if (k2settings->pagelist[0]!='\0')
                    k2printf(" (%d of %d)",pages_done+1,pagecount);
                else
                    k2printf(" of %d",pagecount);
                }
            }
        if (!preview)
            {
            k2printf(TTEXT_NORMAL 
                " (%.1f x %.1f in) ... %s",(double)srcgrey->width/k2settings->src_dpi,
                  (double)srcgrey->height/k2settings->src_dpi,rotstr);
            fflush(stdout);
            }

        /* Parse the source bitmap for viewable regions */
        bmpregion_source_page_add(&region,k2settings,masterinfo,1,pages_done++);
        /* v2.15 memory leak fix */
        bmpregion_free(&region);
        } /* End declaration of BMPREGION region */
#ifdef HAVE_K2GUI
        if (k2gui_active())
            k2gui_cbox_set_pages_completed(pages_done,NULL);
#endif
        if (k2settings->verbose)
            {
            k2printf("    master->rows=%d\n",masterinfo->rows);
            k2printf("Publishing...\n");
            }
        /* Reset the display order for this source page */
        if (k2settings->show_marked_source)
            mark_source_page(k2settings,masterinfo,NULL,0,0xf);
        /*
        ** v2.10 Call masterinfo_publish() no matter what.  If we've just kicked out a
        **       page, it doesn't matter.  It will do nothing.
        */
        masterinfo_publish(masterinfo,k2settings,
                           masterinfo_should_flush(masterinfo,k2settings));
        if (preview && k2_handle_preview(k2settings,masterinfo,k2mark_page_count,
                                         k2settings->dst_color?marked:src,k2out))
            {
            bmp_free(marked);
            bmp_free(srcgrey);
            bmp_free(src);
            masterinfo_free(masterinfo,k2settings);
            if (folder)
                filelist_free(fl);
            k2out->status=0;
            return(0.);
            }
        if (k2settings->show_marked_source && !preview)
            publish_marked_page(mpdf,k2settings->dst_color ? marked : src,k2settings->src_dpi);
        if (!preview)
            {
            int np;
            np=masterinfo->published_pages-pw;
            k2printf("%d new page%s saved.\n",np,np==1?"":"s");
            }
        pw=masterinfo->published_pages;
        }
/*
willus_mem_debug_update("End");
*/
    /* Didn't find the preview page yet--push out final page. */
    if (preview)
        {
        masterinfo_flush(masterinfo,k2settings);
        if (!k2_handle_preview(k2settings,masterinfo,k2mark_page_count,
                               k2settings->dst_color?marked:src,k2out))
            {
            /* No preview bitmap--return zero-width bitmap */
            if (k2out->bmp==NULL)
                bmp_free(masterinfo->preview_bitmap);
            else
                k2out->bmp->width=0;
            }
        bmp_free(marked);
        bmp_free(srcgrey);
        bmp_free(src);
        masterinfo_free(masterinfo,k2settings);
        if (folder)
            filelist_free(fl);
        k2out->status=0;
        return(0.);
        }
    bmp_free(marked);
    bmp_free(srcgrey);
    bmp_free(src);
    /* Determine orientation of document */
    if (or_detect)
        {
        if (pages_done>0)
            {
            double thresh;
            /*
            ** bormean = 1.0 means neutral
            ** bormean >> 1.0 means document is likely portrait (no rotation necessary)
            ** bormean << 1.0 means document is likely landscape (need to rotate it)
            */
            bormean = pow(bormean,1./pages_done);
            thresh=10.-(double)pages_done/2.;
            if (thresh<5.)
                thresh=5.;
            if (bormean < 1./thresh)
                {
                k2printf("Rotating clockwise.\n");
                masterinfo_free(masterinfo,k2settings);
                if (folder)
                    filelist_free(fl);
                k2out->status=0;
                return(270.);
                }
            }
        k2printf("No rotation necessary.\n");
        masterinfo_free(masterinfo,k2settings);
        if (folder)
            filelist_free(fl);
        k2out->status=0;
        return(0.);
        }
    /*
    ** v2.10 -- Calling masterinfo_flush() without checking if a page has just been
    **          been flushed is fine at the end.  If there is nothing left
    **          in the master output bitmap, it won't do anything.
    */
    /*
    if (k2settings->dst_break_pages<=0 && !k2settings_gap_override(k2settings))
    */
        masterinfo_flush(masterinfo,k2settings);
    {
    char cdate[128],author[256],title[256];

#ifdef HAVE_MUPDF_LIB
    if (src_type==SRC_TYPE_PDF)
        {
        if (wmupdf_info_field(mupdffilename,"Author",author,255)<0)
            author[0]='\0';
        if (wmupdf_info_field(mupdffilename,"CreationDate",cdate,127)<0)
            cdate[0]='\0';
        if (wmupdf_info_field(mupdffilename,"Title",title,255)<0)
            title[0]='\0';
        }
    else
#endif
        author[0]=title[0]=cdate[0]='\0';
    if (k2settings->dst_author[0]!='\0')
        strcpy(author,k2settings->dst_author);
    if (k2settings->dst_title[0]!='\0')
        strcpy(title,k2settings->dst_title);
    if (!k2settings->use_crop_boxes)
        {
        if (masterinfo->outline!=NULL)
            {
            if (k2settings->debug)
                wpdfoutline_echo(masterinfo->outline,1,1,stdout);
            pdffile_add_outline(&masterinfo->outfile,masterinfo->outline);
            }
        pdffile_finish(&masterinfo->outfile,title,author,masterinfo->pageinfo.producer,cdate);
        pdffile_close(&masterinfo->outfile);
        }
    else
        {
        /* Re-write PDF file using crop boxes */
#if (WILLUSDEBUGX & 64)
wpdfboxes_echo(&masterinfo->pageinfo.boxes,stdout);
#endif
#ifdef HAVE_MUPDF_LIB
#if (WILLUSDEBUGX & 64)
printf("Calling wpdfpageinfo_scale_source_boxes()...\n");
#endif
        if (k2settings->dst_author[0]!='\0')
            strcpy(masterinfo->pageinfo.author,k2settings->dst_author);
        if (k2settings->dst_title[0]!='\0')
            strcpy(masterinfo->pageinfo.title,k2settings->dst_title);
        /* v2.20 bug fix -- need to compensate for document_scale_factor if its not 1.0 */
        wpdfpageinfo_scale_source_boxes(&masterinfo->pageinfo,1./k2settings->document_scale_factor);
#if (WILLUSDEBUGX & 64)
printf("Calling wmupdf_remake_pdf()...\n");
#endif
        wmupdf_remake_pdf(mupdffilename,dstfile,&masterinfo->pageinfo,1,masterinfo->outline,stdout);
#endif
        }
    if (k2settings->show_marked_source)
        {
        pdffile_finish(mpdf,title,author,masterinfo->pageinfo.producer,cdate);
        pdffile_close(mpdf);
        }
    } // cdate, author, title selection
    if (k2settings->debug || k2settings->verbose)
        k2printf("Cleaning up ...\n\n");
    /*
    if (folder)
        k2printf("Processing on " TTEXT_INPUT "folder %s" TTEXT_NORMAL " complete.  Total %d pages.\n\n",filename,masterinfo->published_pages);
    else
        k2printf("Processing on " TTEXT_BOLD2 "file %s" TTEXT_NORMAL " complete.  Total %d pages.\n\n",filename,masterinfo->published_pages);
    */
    size=wfile_size(dstfile);
    k2printf("\n" TTEXT_BOLD "%d pages" TTEXT_NORMAL,masterinfo->published_pages);
    if (masterinfo->wordcount>0)
        k2printf(" (%d words)",masterinfo->wordcount);
    k2printf(" written to " TTEXT_MAGENTA "%s" TTEXT_NORMAL " (%.1f MB).\n\n",
            dstfile,size/1024./1024.);
#ifdef HAVE_GHOSTSCRIPT
    if (k2settings->ppgs)
        gs_postprocess(dstfile);
#endif
    if (k2settings->show_marked_source)
        {
        size=wfile_size(markedfile);
        k2printf(TTEXT_BOLD "%d pages" TTEXT_NORMAL " written to " TTEXT_MAGENTA "%s" TTEXT_NORMAL " (%.1f MB).\n\n",pages_done,markedfile,size/1024./1024.);
        }
#ifdef HAVE_OCR_LIB
    if (k2settings->dst_ocr && masterinfo->ocrfilename[0]!='\0' && wfile_status(masterinfo->ocrfilename)==1)
        {
        size=wfile_size(masterinfo->ocrfilename);
        k2printf(TTEXT_BOLD "%d words" TTEXT_NORMAL " written to " TTEXT_MAGENTA "%s" TTEXT_NORMAL " (%.1f MB).\n\n",masterinfo->wordcount,masterinfo->ocrfilename,size/1024./1024.);
        }
#endif
    if (local_tocwrites>0)
        k2printf(TTEXT_BOLD "%d bytes" TTEXT_NORMAL " written to " TTEXT_MAGENTA "%s" TTEXT_NORMAL ".\n\n",(int)(wfile_size(k2settings->tocsavefile)+.5),k2settings->tocsavefile);
    masterinfo_free(masterinfo,k2settings);
    if (folder)
        filelist_free(fl);
    k2out->status=0;
    return(0.);
    }