예제 #1
0
파일: k2bmp.c 프로젝트: hwhw/libk2pdfopt
int bmp_get_one_document_page(WILLUSBITMAP *src,K2PDFOPT_SETTINGS *k2settings,
                              int src_type,char *filename,
                              int pageno,double dpi,int bpp,FILE *out)

    {
    if (src_type==SRC_TYPE_PDF)
        {
        int status;
#ifdef HAVE_MUPDF_LIB
        static char *mupdferr_trygs=TTEXT_WARN "\a\n ** ERROR reading from " TTEXT_BOLD2 "%s" TTEXT_WARN "using MuPDF.  Trying Ghostscript...\n\n" TTEXT_NORMAL;

        status=0;
        if (k2settings->usegs<=0)
            {
#if (WILLUSDEBUGX & 0x80000000)
printf("\a\a\n\n\n\n\n\n\n\n\n   ****** FAKING MUPDF--IGNORING SOURCE DOCUMENT!!  *****\n\n\n\n\n\n\n");
status=bmp_read(src,"out.png",NULL);
if (status==0 && bpp!=24)
bmp_convert_to_grayscale(src);
return(status);
#else
            status=bmpmupdf_pdffile_to_bmp(src,filename,pageno,dpi*k2settings->document_scale_factor,bpp);
            if (!status || k2settings->usegs<0)
                return(status);
#endif
            }
        /* Switch to Postscript since MuPDF failed */
        if (k2settings->usegs==0)
            {
            k2printf(mupdferr_trygs,filename);
            k2settings->usegs=1;
            }
#endif
#ifdef HAVE_GHOSTSCRIPT_LIB
        if (willusgs_init(stdout) < 0)
            {
            k2sys_enter_to_exit(k2settings);
            exit(20);
            }
#endif
        bmp_set_pdf_pageno(pageno);
        bmp_set_pdf_dpi(dpi);
        /*
        k2printf("Converting " TTEXT_BOLD2 "%s" TTEXT_NORMAL 
            " page %2d to %d dpi bitmap ... ",filename,i,dpi);
        fflush(stdout);
        */
        status=bmp_read(src,filename,NULL);
        if (!status && bpp==8)
            bmp_convert_to_greyscale(src);
        return(status);
        }
    else
#ifdef HAVE_DJVU_LIB
    if (src_type==SRC_TYPE_DJVU)
        return(bmpdjvu_djvufile_to_bmp(src,filename,pageno,dpi*k2settings->document_scale_factor,bpp,out));
    else
#endif
    return(-1);
    }
예제 #2
0
int main(int argc, char *argv[]) {
  bitmap_t* bmp1 = bmp_read("test.bmp");
  printf("\n");
  bitmap_t* bmp2 = bmp_read("lena512.bmp");
  bmp_write(bmp2, "lena512-2.bmp");
  bmp_destroy(bmp1);
  bmp_destroy(bmp2);
  return 0;
}
예제 #3
0
파일: run.c 프로젝트: SexyPantz/Orga2.rtp2
int run_blur(int c, char* src, char* dst){
  BMP* bmp = bmp_read(src);
  if(bmp==0) { return -1;}  // open error
  
  uint8_t* data = bmp_get_data(bmp);
  uint32_t h = *(bmp_get_h(bmp));
  uint32_t w = *(bmp_get_w(bmp));
  if(w%4!=0) { return -1;}  // do not support padding
  
  uint8_t* dataC = 0;
  if(*(bmp_get_bitcount(bmp)) == 24) {
    dataC = malloc(sizeof(uint8_t)*4*h*w);
    to32(w,h,data,dataC);
  } else {
    dataC = data;
  }
  
  if(c==0)         C_blur(w,h,dataC);
  else if(c==1) ASM_blur1(w,h,dataC);
  else if(c==2) ASM_blur2(w,h,dataC);
  else {return -1;}
  
  if(*(bmp_get_bitcount(bmp)) == 24) {
    to24(w,h,dataC,data);
    free(dataC);
  }
  bmp_save(dst,bmp);
  bmp_delete(bmp);
  return 0;
}
예제 #4
0
파일: run.c 프로젝트: SexyPantz/Orga2.rtp2
int run_hsl(int c, char* src, char* dst, float hh, float ss, float ll) {
  BMP* bmp = bmp_read(src);
  if(bmp==0) { return -1;}  // open error
  if(ss>1) ss=1; else if(ss<-1) ss=-1;
  if(ll>1) ll=1; else if(ll<-1) ll=-1;
  uint8_t* data = bmp_get_data(bmp);
  uint32_t h = *(bmp_get_h(bmp));
  uint32_t w = *(bmp_get_w(bmp));
  if(w%4!=0) { return -1;}  // do not support padding
  
  uint8_t* dataC = 0;
  if(*(bmp_get_bitcount(bmp)) == 24) {
    dataC = malloc(sizeof(uint8_t)*4*h*w);
    to32(w,h,data,dataC);
  } else {
    dataC = data;
  }
  
  if(c==0)         C_hsl(w,h,dataC,hh,ss,ll);
  else if(c==1) ASM_hsl1(w,h,dataC,hh,ss,ll);
  else if(c==2) ASM_hsl2(w,h,dataC,hh,ss,ll);
  else {return -1;}
  
  if(*(bmp_get_bitcount(bmp)) == 24) {
    to24(w,h,dataC,data);
    free(dataC);
  }
  bmp_save(dst,bmp);
  bmp_delete(bmp);
  return 0;
}
예제 #5
0
/**
* @brief makes texture from given file
*
* @param filename
* File name
* @param tex_name
* Texture name, which is a number
*/
void makeTextureImage(char filename[], GLuint tex_name) {
   unsigned long width=0;
   long height=0;   /* width/height of textures */
   GLubyte *image=NULL; /* storage for texture */

   if (bmp_read (filename, &width, &height, image))
      assert(false);

   /* create texture object */
   glBindTexture(GL_TEXTURE_2D, texName[tex_name]);

   /* set up wrap & filter params */
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

   /*  printf("Texture %s, height %d, width %d\n",
       filename, (int)height, (int)width); */

   /* load the image */
   glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height,
                0, GL_RGB, GL_UNSIGNED_BYTE, image);

   delete []image;
}
예제 #6
0
파일: run.c 프로젝트: SexyPantz/Orga2.rtp2
int run_merge(int c, char* src1, char* src2, char* dst, float value){
  if(dst==0) { return -1;}  // non destine
  if(value>1) value=1; else if(value<0) value=0;
  BMP* bmp1 = bmp_read(src1);
  BMP* bmp2 = bmp_read(src2);
  if(bmp1==0 || bmp2==0) { return -1;}  // open error
  
  uint8_t* data1 = bmp_get_data(bmp1);
  uint8_t* data2 = bmp_get_data(bmp2);
  uint32_t h1 = *(bmp_get_h(bmp1));
  uint32_t w1 = *(bmp_get_w(bmp1));
  uint32_t h2 = *(bmp_get_h(bmp2));
  uint32_t w2 = *(bmp_get_w(bmp2));
  if(w1%4!=0 || w2%4!=0) { return -1;}  // do not support padding
  if( w1!=w2 || h1!=h2 ) { return -1;}  // different image size
  
  uint8_t* data1C = 0;
  uint8_t* data2C = 0;
  if(*(bmp_get_bitcount(bmp1)) == 24) {
    data1C = malloc(sizeof(uint8_t)*4*h1*w1);
    data2C = malloc(sizeof(uint8_t)*4*h2*w2);
    to32(w1,h1,data1,data1C);
    to32(w2,h2,data2,data2C);
  } else {
    data1C = data1;
    data2C = data2;
  }
  
  if(c==0)         C_merge(w1,h1,data1C,data2C,value);
  else if(c==1) ASM_merge1(w1,h1,data1C,data2C,value);
  else if(c==2) ASM_merge2(w1,h1,data1C,data2C,value);
  else {return -1;}
  
  if(*(bmp_get_bitcount(bmp1)) == 24) {
    to24(w1,h1,data1C,data1);
    free(data1C);
    free(data2C);
  }
  bmp_save(dst,bmp1);
  bmp_delete(bmp1);
  bmp_delete(bmp2);
  return 0;
}
예제 #7
0
void imagenes_abrir(configuracion_t *config)
{
	// Cargo la imagen
	if ( (src_img = bmp_read (config->archivo_entrada)) == 0 ) {
    	fprintf(stderr, "Error abriendo la imagen fuente\n");
		exit(EXIT_FAILURE);
	}
	if (bmp_compression(src_img) != BI_RGB) {
		fprintf(stderr, "Error: La imagen fuente esta comprimida\n");
		exit(EXIT_FAILURE);
	}

	if (bmp_bit_count(src_img) == 24) {
    	bmp_convert_24_to_32_bpp(src_img);
	}

	dst_img = bmp_copy( src_img, 1 );

	if (config->archivo_entrada_2 != NULL) {
		if ( (src_img2 = bmp_read (config->archivo_entrada_2)) == 0 ) {
	    	fprintf(stderr, "Error abriendo la imagen fuente 2\n");
			exit(EXIT_FAILURE);
		}
		if (bmp_compression(src_img2) != BI_RGB) {
			fprintf(stderr, "Error: La imagen fuente 2 esta comprimida\n");
			exit(EXIT_FAILURE);
		}

		if (bmp_bit_count(src_img2) == 24) {
	    	bmp_convert_24_to_32_bpp(src_img2);
		}
		setear_buffer(&config->src_2, src_img2);
	} else {
		src_img2 = NULL;
	}

	setear_buffer(&config->src, src_img);
	setear_buffer(&config->dst, dst_img);
}
예제 #8
0
int run_hsl(int c, char* src, char* dst, float hh, float ss, float ll, int times) {
  BMP* bmp = bmp_read(src);
  if(bmp==0) { return -1;}  // open error
  if(ss>1) ss=1; else if(ss<-1) ss=-1;
  if(ll>1) ll=1; else if(ll<-1) ll=-1;
  uint8_t* data = bmp_get_data(bmp);
  uint32_t h = *(bmp_get_h(bmp));
  uint32_t w = *(bmp_get_w(bmp));
  if(w%4!=0) { return -1;}  // do not support padding
  
  uint8_t* dataC = 0;
  if(*(bmp_get_bitcount(bmp)) == 24) {
    dataC = malloc(sizeof(uint8_t)*4*h*w);
    to32(w,h,data,dataC);
  } else {
    dataC = data;
  }
  
  unsigned long start, end;
  switch(c){
    case 0:
      RDTSC_START(start);
      C_hsl(w,h,dataC,hh,ss,ll);
      RDTSC_STOP(end); 
      break;
    case 1:
      RDTSC_START(start);
      ASM_hsl1(w,h,dataC,hh,ss,ll);
      RDTSC_STOP(end);
      break;
    case 2:
      RDTSC_START(start);
      ASM_hsl2(w,h,dataC,hh,ss,ll);
      RDTSC_STOP(end);
      break;
    default:
      return -1;
      break;
  }
  unsigned long delta = end - start;
  
  printf("%lu", delta);

  if(*(bmp_get_bitcount(bmp)) == 24) {
    to24(w,h,dataC,data);
    free(dataC);
  }
  bmp_delete(bmp);
  return 0;
}
예제 #9
0
int main(int argc, char* argv[]){

	if (argc != 2) {
		print_help(argv[0]);
		return 0;
	}

	char* fileName = argv[1];

	BMP* bmp = bmp_read(fileName);

	if (bmp == 0) {
		printf("Invalid filename.\n");
		return 0;
	}

	uint8_t *data = bmp_get_data(bmp);
	int h  = (int) *bmp_get_h(bmp);
	int w  = (int) *bmp_get_w(bmp);
	int c1 = ((BMPIH*)(bmp->ih))->biBitCount;

	if(c1 == 32) {
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				int pos   = (i*w+j)*4;
				uint8_t r = data[pos+3];
				uint8_t g = data[pos+2];
				uint8_t b = data[pos+1];
				uint8_t a = data[pos+0];
				printf("[%u,%u,%u,%u]\n", (int) r, (int) g, (int) b, (int) a);
			}
		}
	}

	if(c1 == 24) {
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				int pos   = (i*w+j)*3;
				uint8_t r = data[pos+2];
				uint8_t g = data[pos+1];
				uint8_t b = data[pos+0];
				printf("[%u,%u,%u]\n", (int) r, (int) g, (int) b);
			}
		}
	}

	bmp_delete(bmp);

}
예제 #10
0
int run_blur(int c, char* src, char* dst, int times){
  BMP* bmp = bmp_read(src);
  if(bmp==0) { return -1;}  // open error
  
  uint8_t* data = bmp_get_data(bmp);
  uint32_t h = *(bmp_get_h(bmp));
  uint32_t w = *(bmp_get_w(bmp));
  if(w%4!=0) { return -1;}  // do not support padding
  
  uint8_t* dataC = 0;
  if(*(bmp_get_bitcount(bmp)) == 24) {
    dataC = malloc(sizeof(uint8_t)*4*h*w);
    to32(w,h,data,dataC);
  } else {
    dataC = data;
  }
  
  unsigned long start, end;
  switch(c){
    case 0:
      RDTSC_START(start);
      C_blur(w,h,dataC);
      RDTSC_STOP(end); 
      break;
    case 1:
      RDTSC_START(start);
      ASM_blur1(w,h,dataC);
      RDTSC_STOP(end);
      break;
    case 2:
      RDTSC_START(start);
      ASM_blur2(w,h,dataC);
      RDTSC_STOP(end);
      break;
    default:
      // return -1;
      break;
  }
  unsigned long delta = end - start;
  printf("%lu", delta);

  if(*(bmp_get_bitcount(bmp)) == 24) {
    to24(w,h,dataC,data);
    free(dataC);
  }
  bmp_delete(bmp);
  return 0;
}
예제 #11
0
void bmp_io_test()
{
    // this test copies the original image with swapped colours
    unsigned char *barray, *garray, *rarray;
    bool error;
    long int height;
    unsigned long int width;

    //  read the original BMP file
  	// bmp_read(filename, *width, *height, *red, *green, *blue);
  	// colour arrays are automatically allocated inside the function
  	// size of the image is also extracted from the BMP header
	error = bmp_read("icl1.bmp", &width, &height, &rarray,&garray,&barray);
    if ( error )
    {
        cout << "\n";
        cout << "bmp_read: ERROR" << endl;
        return ;
    }
    else {
        cout << "bmp_read: OK" << endl;
        cout << "bmp_read: " << " width = " <<  width << ", height = " << height << endl;
    }

    // write the new BMP file:  swap blue and green
	// bmp_24_write(filename, width, height, red, green, blue);
	error = bmp_24_write("icl2.bmp", width, height, rarray, barray, garray );
    if ( error ) {
        cout << "bmp_24_write: ERROR" << endl;
        return ;
    }
    else {
        cout << "bmp_24_write: OK" << endl;
    }

    //  Free the memory
    delete [] rarray;
    delete [] garray;
    delete [] barray;

    return;
}
예제 #12
0
int main(){

  /* ======================================================================= */
  /* === EJ 1 : crear un bmp de 24 bits de 100x100 y dibujar un patron bayer */

  // creo el header de una imagen de 100x100 de 24 bits
  BMPIH* imgh1 = get_BMPIH(100,100);

  // crea una imagen bmp inicializada
  BMP* bmp1 = bmp_create(imgh1,1);

  // obtengo la data y dibujo el patron bayer
  uint8_t* data1 = bmp_get_data(bmp1);
  int i,j;
  for(j=0;j<100;j=j+2) {
    for(i=0;i<100;i=i+2) {
      data1[j*300+3*i+1] = 0xff;
      data1[j*300+3*i+3] = 0xff;
    }
    for(i=0;i<100;i=i+2) {
      data1[j*300+300+3*i+2] = 0xff;
      data1[j*300+300+3*i+4] = 0xff;
    }
  }

  // guardo la imagen
  bmp_save(FILE1, bmp1);

  // borrar bmp
  bmp_delete(bmp1);

  /* ======================================================================= */
  /* === EJ 2 : crear un bmp de 32 bits de 100x100 y fondo blanco en degrade */

  // creo el encavezado de una imagen de 640x480 de 32 bits
  BMPV5H* imgh2 = get_BMPV5H(100,100);

  // crea una imagen bmp no inicializada
  BMP* bmp2 = bmp_create(imgh2,0);

  // obtengo la data y dibujo el degrade
  uint8_t* data2 = bmp_get_data(bmp2);
  for(j=0;j<100;j++) {
    for(i=0;i<100;i++) {
      data2[j*400+i*4+0] = (uint8_t)(((float)(i+j))*(256.0/200.0));
      data2[j*400+i*4+1] = 0xff;
      data2[j*400+i*4+2] = 0xff;
      data2[j*400+i*4+3] = 0xff;
    }
  }

  // guardo la imagen
  bmp_save(FILE2, bmp2);

  // borrar bmp
  bmp_delete(bmp2);

  /* ======================================================================= */
  /* === EJ 3 : crear un bmp con el mapa de bits de bmp1 y el alpha de bmp2  */

  // Abro los dos archivos
  BMP* bmp1n = bmp_read(FILE1);
  BMP* bmp2n = bmp_read(FILE2);

  // copio la imagen con transparecia sin datos
  BMP* bmpNEW = bmp_copy(bmp2n, 0);

  // obtengo datos de new y las combino
  uint8_t* data1n = bmp_get_data(bmp1n);
  uint8_t* data2n = bmp_get_data(bmp2n);
  uint8_t* dataNEW = bmp_get_data(bmpNEW);
  for(j=0;j<100;j++) {
    for(i=0;i<100;i++) {
      dataNEW[j*400+i*4+0] = data2n[j*400+i*4+0];
      dataNEW[j*400+i*4+1] = data1n[j*300+i*3+0];
      dataNEW[j*400+i*4+2] = data1n[j*300+i*3+1];
      dataNEW[j*400+i*4+3] = data1n[j*300+i*3+2];
    }
  }

  // guardo la imagen
  bmp_save(FILE3, bmpNEW);

  // borrar bmp
  bmp_delete(bmp1n);
  bmp_delete(bmp2n);
  bmp_delete(bmpNEW);

  return 0;
}
예제 #13
0
// this function tests your image processing algorithm implmented 
// in hardware using the RGB streams from BMP file
void testbench()
{
 
    unsigned char *red_out, *green_out, *blue_out;
    bool error;
    int i, j;


	// these signals have to match the ones in the block diagram 
	// where they are connected
    ac_int<PIXEL_WL * KERNEL_WIDTH, false> *input_stream;
    ac_int<PIXEL_WL, false> *output_stream;



	/************************************************************************
    * reads the original/source BMP file, to emulate video frame
  	* colour arrays are automatically allocated inside the function
  	* size of the image is extracted from the BMP header
  	* bmp_read(filename, *width, *height, *red, *green, *blue);
	************************************************************************/
	error = bmp_read(source_bmp_file, &width, &height, &red_in, &green_in, &blue_in);
    if (error)
    {
        cout << "\n";
        cout << "bmp_read: ERROR" << endl;
        return ;
    }
    else {
        cout << "bmp_read: OK" << endl;
        cout << "bmp_read: " << width << "x" << height << endl;
    }


    num_pixels = width * abs (height) * sizeof ( unsigned char );     

    if(num_pixels != NUM_PIXELS) {
        cout << "ERROR: Expecting a 640x480 BMP image!" << endl;
        delete [] red_in;
        delete [] green_in;
        delete [] blue_in;
        return;
    }
    
    

    // need to reserve memory to store results from the filter
    // allocate memory to input & output streams from/to your hardware block
    input_stream = new ac_int<PIXEL_WL * KERNEL_WIDTH, false>[num_pixels];
    output_stream = new ac_int<PIXEL_WL, false>[num_pixels];


    // RGB colour components to be written in file
    // the output must have the same number of bytes/pixels as the input
    red_out = new unsigned char[num_pixels];
    green_out = new unsigned char[num_pixels];
    blue_out = new unsigned char[num_pixels];


    // filter buffer = shift register from input column (KERNEL_WIDTH columns)
    ac_int<PIXEL_WL, false>col_pixel_buf[KERNEL_WIDTH];

    // group the 3 colour components into 1 single steam
    // generate the input stream emulating the camera
    for(i = 0; i < num_pixels; i++) {
        for(j = 0; j < KERNEL_WIDTH; j++) {
			// bits 29..20 = RED,  19..10 = GREEN, 9..0 = BLUE
            col_pixel_buf[j] = ((((ac_int<PIXEL_WL, false>)red_in[i + j * width]) << (2*COLOUR_WL)) | 
                                (((ac_int<PIXEL_WL, false>)green_in[i + j * width]) << COLOUR_WL) 
                                | (ac_int<PIXEL_WL, false>)blue_in[i + j * width]);
        }
        input_stream[i] = 0;
        for(j = 0; j < KERNEL_WIDTH; j++) {
            input_stream[i] |= ((ac_int<PIXEL_WL * KERNEL_WIDTH, false>)col_pixel_buf[j]) << (j * PIXEL_WL);
        }
    }
    
    
    
    
    
    /******************************************************************/
    /* test your design                                               */
    /******************************************************************/

    CCS_DESIGN(mean_vga)(input_stream, output_stream); 

/*  by-pass your block - check I/Os  
	for(int i = 0; i < num_pixels; i++) {
        output_stream[i] = input_stream[i].slc<PIXEL_WL>(0); // copy current pixel (0,30,60,90,120)
    }    */


    
    
    // recover your RGB colour signals from the output stream
    for(int i = 0; i < num_pixels; i++) {
        red_out[i] = (output_stream[i].slc<COLOUR_WL>(2*COLOUR_WL));
        green_out[i] = (output_stream[i].slc<COLOUR_WL>(COLOUR_WL));
        blue_out[i] = (output_stream[i].slc<COLOUR_WL>(0));
    }    

 


    // write the new BMP file:  swap blue and green
	// bmp_24_write(filename, width, height, red, green, blue);
	error = bmp_24_write(hw_bmp_file, width, height, red_out, green_out, blue_out);
    if ( error ) {
        cout << "bmp_24_write: ERROR" << endl;
        return ;
    }
    else {
        cout << "bmp_24_write: OK" << endl;
    }




   // release memory
    delete [] input_stream;
    delete [] output_stream;
    

    delete [] red_out;
    delete [] green_out;
    delete [] blue_out;

    return;        

}
예제 #14
0
int main(int argc,char *argv[])

{
    static K2PDFOPT_SETTINGS _k2settings, *k2settings;
    static MASTERINFO _masterinfo, *masterinfo;
    WILLUSBITMAP _srcgrey, *srcgrey;
    WILLUSBITMAP _src, *src;
    BMPREGION region;
    int status;

    if (argc<2)
    {
        printf("usage:  kview <infile.bmp>\n");
        return(0);
    }
    src=&_src;
    bmp_init(src);
    srcgrey=&_srcgrey;
    bmp_init(srcgrey);
    if ((status=bmp_read(src,argv[1],NULL))<0)
    {
        printf("Error %d reading bitmap file %s.\n",status,argv[1]);
        return(10);
    }
    printf("Bitmap %s is %d x %d x %d\n",argv[1],src->width,src->height,src->bpp);

    /* Initialize settings */
    k2settings=&_k2settings;
    k2pdfopt_settings_init(k2settings);
    k2settings->use_crop_boxes=0;
    k2settings->src_rot=0;
    k2settings->erase_vertical_lines=0;
    k2settings->src_autostraighten=0;
    k2pdfopt_settings_sanity_check(k2settings);

    /* Init master output structure */
    masterinfo=&_masterinfo;
    masterinfo_init(masterinfo,k2settings);

    /* Init for new source doc */
    k2pdfopt_settings_new_source_document_init(k2settings);

    /* Init new source bitmap */
    masterinfo_new_source_page_init(masterinfo,k2settings,src,srcgrey,NULL,&region,0.,NULL,NULL,1,NULL);

    /* Process single source page */
    bmpregion_source_page_add(&region,k2settings,masterinfo,1,
                              (int)(0.25 * k2settings->src_dpi + .5));
    bmp_free(srcgrey);
    bmp_free(src);

    /*
    ** Get output pages
    */
    {
        WILLUSBITMAP *bmp,_bmp;
        int pn,rows,size_reduction;
        double bmpdpi;

        bmp=&_bmp;
        bmp_init(bmp);
        pn=0;
        while ((rows=masterinfo_get_next_output_page(masterinfo,k2settings,1,bmp,
                     &bmpdpi,&size_reduction,NULL))>0)
        {
            char filename[256];
            pn++;
            sprintf(filename,"outpage%02d.bmp",pn);
            bmp_write(bmp,filename,stdout,0);
        }
        bmp_free(bmp);
    }
    masterinfo_free(masterinfo,k2settings);
    return(0);
}
예제 #15
0
int run_merge(int c, char* src1, char* src2, char* dst, float value, int times){
  if(value>1) value=1; else if(value<0) value=0;
  BMP* bmp1 = bmp_read(src1);
  BMP* bmp2 = bmp_read(src2);
  if(bmp1==0 || bmp2==0) { return -1;}  // open error
  
  uint8_t* data1 = bmp_get_data(bmp1);
  uint8_t* data2 = bmp_get_data(bmp2);
  uint32_t h1 = *(bmp_get_h(bmp1));
  uint32_t w1 = *(bmp_get_w(bmp1));
  uint32_t h2 = *(bmp_get_h(bmp2));
  uint32_t w2 = *(bmp_get_w(bmp2));
  if(w1%4!=0 || w2%4!=0) { return -1;}  // do not support padding
  if( w1!=w2 || h1!=h2 ) { return -1;}  // different image size
  
  uint8_t* data1C = 0;
  uint8_t* data2C = 0;
  if(*(bmp_get_bitcount(bmp1)) == 24) {
    data1C = malloc(sizeof(uint8_t)*4*h1*w1);
    data2C = malloc(sizeof(uint8_t)*4*h2*w2);
    to32(w1,h1,data1,data1C);
    to32(w2,h2,data2,data2C);
  } else {
    data1C = data1;
    data2C = data2;
  }

  unsigned long start, end;
  switch(c){
    case 0:
      RDTSC_START(start);
      C_merge(w1,h1,data1C,data2C,value);
      RDTSC_STOP(end); 
      break;
    case 1:
      RDTSC_START(start);
      ASM_merge1(w1,h1,data1C,data2C,value);
      RDTSC_STOP(end);
      break;
    case 2:
      RDTSC_START(start);
      ASM_merge2(w1,h1,data1C,data2C,value);
      RDTSC_STOP(end);
      break;
    default:
      return -1;
      break;
  }
  unsigned long delta = end - start;
  
  printf("%lu", delta);

  if(*(bmp_get_bitcount(bmp1)) == 24) {
    to24(w1,h1,data1C,data1);
    free(data1C);
    free(data2C);
  }
  bmp_delete(bmp1);
  bmp_delete(bmp2);
  return 0;
}
예제 #16
0
파일: util.cpp 프로젝트: XBOOS/raytracing
Texture::Texture(std::string filename) : textW(0), textH(0), r(NULL), g(NULL), b(NULL) {
    bool readtext = bmp_read((char*)filename.c_str(), &textW, &textH, &r, &g, &b);
    if(readtext){
        std::cerr<<"fail to read texture"<<std::endl;
    }
}
예제 #17
0
int main(int argc, char* argv[]){
  int i,j;
  
  // (0) leer parametros
  options opt;
  if (argc == 1) {print_help(argv[0]); return 0;}
  if(read_options(argc, argv, &opt))
  {printf("ERROR reading parameters\n"); return 1;}
  int len1 = strlen(opt.file1);
  int len2 = strlen(opt.file2);
  if( strcmp(&(opt.file1[len1-4]),".bmp") || strcmp(&(opt.file2[len2-4]),".bmp") )
  { printf("ERROR: nombre del archivo\n"); return -1;}
  
  // (0.1) siempre armo el summary
  opt.summary = (int*)malloc(sizeof(int)*256);
  for(i=0;i<256;i++) opt.summary[i]=0;
  
  // (1) leer imagenes
  BMP* bmp1 = bmp_read(opt.file1);
  BMP* bmp2 = bmp_read(opt.file2);
  if( bmp1==0 || bmp1==0 )
  { printf("ERROR: no se puede abrir el archivo\n"); return -1;}
  
  // (2) check tipo de archivo
  if( ((BMPIH*)(bmp1->ih))->biSize != ((BMPIH*)(bmp1->ih))->biSize )
  { printf("ERROR: tipo de archivo diferente\n"); return -1;}
  
  // (3) check tamaño del archivo
  int w1 = ((BMPIH*)(bmp1->ih))->biWidth;
  int h1 = ((BMPIH*)(bmp1->ih))->biHeight;
  int c1 = ((BMPIH*)(bmp1->ih))->biBitCount;
  int w2 = ((BMPIH*)(bmp2->ih))->biWidth;
  int h2 = ((BMPIH*)(bmp2->ih))->biHeight;
  int c2 = ((BMPIH*)(bmp2->ih))->biBitCount;
  if( w1!=w2 || h1!=h2 || c1!=c2 )
  { printf("ERROR: tamaño de archivo diferente\n"); return -1;}
  //printf("%i=%i %i=%i %i=%i\n",w1,w2,h1,h2,c1,c2);
  if(w1%4!=0) { printf("ERROR: padding no soportado\n"); return -1;} // TODO: soportar padding!
  
  // (3) check el bit count TODO: only 24 o 32
  if( c1!=24 && c1!=32 )
    { printf("ERROR: (%i) bitcount distinto de 24 o 32\n", c1); return -1;}
  
  // (4) crear imagenes de diferencias
  BMP *bmpDiffR, *bmpDiffG, *bmpDiffB, *bmpDiffA;
  bmpDiffR = bmp_copy(bmp1,0);
  bmpDiffG = bmp_copy(bmp1,0);
  bmpDiffB = bmp_copy(bmp1,0);
  if(c1 == 32)
    bmpDiffA = bmp_copy(bmp1,0);
  
  // (5) extraer data
  uint8_t *data1,*data2,*dataR,*dataG,*dataB,*dataA;
  data1 = bmp_get_data(bmp1);
  data2 = bmp_get_data(bmp2);
  dataR = bmp_get_data(bmpDiffR);
  dataG = bmp_get_data(bmpDiffG);
  dataB = bmp_get_data(bmpDiffB);
  if(c1 == 32)
    dataA = bmp_get_data(bmpDiffA);
  
  // (6) calcular diferencias
  if(c1 == 32)
  for(j=0;j<h1;j++) {
    for(i=0;i<w1;i++) {
      int pos = (j*w1+i)*4;
      uint8_t R1 = data1[pos+3];
      uint8_t G1 = data1[pos+2];
      uint8_t B1 = data1[pos+1];
      uint8_t A1 = data1[pos+0];
      uint8_t R2 = data2[pos+3];
      uint8_t G2 = data2[pos+2];
      uint8_t B2 = data2[pos+1];
      uint8_t A2 = data2[pos+0];
      
      dataR[pos+3] = cmp(R1,R2,i,j,"R",&opt);
      dataR[pos+2] = dataR[pos+3];
      dataR[pos+1] = dataR[pos+3];
      dataR[pos+0] = 255;
      
      dataG[pos+3] = cmp(G1,G2,i,j,"G",&opt);
      dataG[pos+2] = dataG[pos+3];
      dataG[pos+1] = dataG[pos+3];
      dataG[pos+0] = 255;
      
      dataB[pos+3] = cmp(B1,B2,i,j,"B",&opt);
      dataB[pos+2] = dataB[pos+3];
      dataB[pos+1] = dataB[pos+3];
      dataB[pos+0] = 255;

      dataA[pos+3] = cmp(A1,A2,i,j,"A",&opt);
      dataA[pos+2] = dataA[pos+3];
      dataA[pos+1] = dataA[pos+3];
      dataA[pos+0] = 255;
    }
  }
  if(c1 == 24)
  for(j=0;j<h1;j++) {
    for(i=0;i<w1;i++) {
      int pos = (j*w1+i)*3;
      uint8_t R1 = data1[pos+2];
      uint8_t G1 = data1[pos+1];
      uint8_t B1 = data1[pos+0];
      uint8_t R2 = data2[pos+2];
      uint8_t G2 = data2[pos+1];
      uint8_t B2 = data2[pos+0];
      
      dataR[pos+2] = cmp(R1,R2,i,j,"R",&opt);
      dataR[pos+1] = dataR[pos+2];
      dataR[pos+0] = dataR[pos+2];
      
      dataG[pos+2] = cmp(G1,G2,i,j,"G",&opt);
      dataG[pos+1] = dataG[pos+2];
      dataG[pos+0] = dataG[pos+2];
      
      dataB[pos+2] = cmp(B1,B2,i,j,"B",&opt);
      dataB[pos+1] = dataB[pos+2];
      dataB[pos+0] = dataB[pos+2];
    }
  }
  
  // (7) mostrar summary
  if(opt.summaryop) {
    for(i=1;i<256;i++)
      if(opt.summary[i]!=0)
      printf("%i\t%i\n",i,opt.summary[i]);
  }
  
  // (8) guardar resultados
  if(opt.image) {
    char* strX = "diffX.bmp";
    char* fileSto = malloc(strlen(opt.file1)+5+1);
    strcpy(fileSto,opt.file1);
    strcpy(fileSto+len1-4,strX);
    fileSto[len1]='R';
    bmp_save(fileSto,bmpDiffR);
    fileSto[len1]='G';
    bmp_save(fileSto,bmpDiffG);
    fileSto[len1]='B';
    bmp_save(fileSto,bmpDiffB);
    fileSto[len1]='A';
    if(c1 == 32)
      bmp_save(fileSto,bmpDiffA);
  
    // (8.1) borrar las imagenes
    bmp_delete(bmp1);
    bmp_delete(bmp2);
    bmp_delete(bmpDiffR);
    bmp_delete(bmpDiffG);
    bmp_delete(bmpDiffB);
    if(c1 == 32)
      bmp_delete(bmpDiffA);
  }
  
  // (9) retorno error si encontre una diferencia
  for(i=opt.epsilon;i<256;i++)
    if(opt.summary[i]>0)
      return -1;
  return 0;
}
예제 #18
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.);
    }