www.pudn.com > imgport.rar > p2jpg.cpp, change:2009-03-26,size:7939b


/* 
     p2jpg.cpp 
      
     Project Full Name: imageprinter 
     http://sourceforge.net/projects/imageprinter/ 
     http://code-industry.net 
     
     
     Copyright (C) 2007-2008    Ibadov Tariel   <tariel@code-industry.net> 
     
    * This program is free software; you can redistribute it and/or modify 
    * it under the terms of the GNU General Public License as published by 
    * the Free Software Foundation; either version 2 of the License, or 
    * (at your option) any later version. 
    * 
    * This program is distributed in the hope that it will be useful, 
    * but WITHOUT ANY WARRANTY; without even the implied warranty of 
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
    * GNU General Public License for more details.      
      
      
*/ 
 
 
#include "p2jpg.h" 
#define BUFSIZE 4096 
 
 
long FileLength; 
int  j_Width; 
int  j_Height; 
 
long PageNo = 1; 
extern int qjpg; 
 
 
#define JMESSAGE(code,string)	string , 
 
static const char * const cdjpeg_message_table[] = { 
#include "cderror.h" 
  NULL 
}; 
 
long GetFileSize(FILE *fp) 
{ 
      int Pos; 
      long Size; 
      Pos =ftell(fp); 
      fseek(fp, 0, SEEK_END );     
      Size = ftell(fp);     
      fseek(fp, Pos, SEEK_SET ); 
      return Size; 
} 
 
bool CopyStream(FILE *Src,FILE *Dest) 
{ 
     unsigned char  *buffer; 
     int   Pos; 
     unsigned long FileSize; 
     Pos =ftell(Src); 
     FileSize=FileLength; 
     buffer=(unsigned char *)malloc(FileSize); 
     if (buffer==NULL)   return false; 
     fseek(Src,0,SEEK_SET); 
     fread(buffer,1,FileSize,Src);        
     fwrite(buffer,1,FileSize,Dest); 
     free(buffer); 
     fseek(Src,Pos,SEEK_SET); 
     return true; 
} 
 
void WriteCrossReferenceTable(FILE *AStream,unsigned long ObjectPosArray[],int i_count) 
{ 
   int i; 
   fprintf(AStream,"xref\n"); 
   fprintf(AStream,"0 %d\n",i_count+1); 
   fprintf(AStream,"0000000000 65535 f \n"); 
   for (i= 0; i<=i_count-1;i++) 
       fprintf(AStream,"%0.10d 00000 n \n",ObjectPosArray[i]); 
} 
 
void WriteContentsObject(FILE *AStream,unsigned long ObjectPosArray[],int *ObjectIndex,int w,int h) 
{ 
   int Length; 
   ObjectPosArray[*ObjectIndex]  =(unsigned long)ftell(AStream); 
   fprintf(AStream,"%d 0 obj\n",*ObjectIndex+1); 
   fprintf(AStream,"<< /Length %d 0 R >>\n",*ObjectIndex+2); 
   fprintf(AStream,"stream\n"); 
   Length=ftell(AStream); 
   fprintf(AStream,"q\n"); 
   fprintf(AStream,"%d 0 0 %d 0 0 cm\n",w,h); 
   fprintf(AStream,"/Im0 Do\n"); 
   fprintf(AStream,"Q\n"); 
   Length=ftell(AStream)-Length; 
   fprintf(AStream,"endstream\n"); 
   fprintf(AStream,"endobj\n"); 
   *ObjectIndex=*ObjectIndex+1; 
   ObjectPosArray[*ObjectIndex] =(unsigned long)ftell(AStream); 
   fprintf(AStream,"%d 0 obj\n",*ObjectIndex+1); 
   fprintf(AStream,"%d\n",Length); 
   fprintf(AStream,"endobj\n"); 
   *ObjectIndex=*ObjectIndex+1; 
} 
 
int JPGtoPDF(const char *OpenName,const char *SaveName) 
{ 
    int   ObjectIndex; 
    unsigned long ObjectPosArray[10]; 
    FILE  *JPGStream,*AStream;  
     
    ObjectIndex=0; 
    JPGStream=fopen(OpenName,"rb"); 
    if(JPGStream==NULL) { 
         return -1;   
    } 
     
    FileLength=GetFileSize(JPGStream); 
    AStream=fopen(SaveName,"wb+"); 
    if(AStream==NULL){ 
        fclose(JPGStream); 
        return -2;  
    } 
    fprintf(AStream,"%%PDF-1.2\n"); 
    ObjectPosArray[ObjectIndex] =ftell(AStream); 
    fprintf(AStream,"%d 0 obj\n",ObjectIndex+1); 
    fprintf(AStream,"<<\n"); 
    fprintf(AStream,"/Type /Catalog\n"); 
    fprintf(AStream,"/Pages 2 0 R\n"); 
    fprintf(AStream,">>\n"); 
    fprintf(AStream,"endobj\n"); 
    ObjectIndex++; 
    ObjectPosArray[ObjectIndex] =ftell(AStream); 
    fprintf(AStream,"%d 0 obj\n",ObjectIndex+1); 
    fprintf(AStream,"<<\n"); 
    fprintf(AStream,"/Type /Pages\n"); 
    fprintf(AStream,"/Kids [ 3 0 R ]\n"); 
    fprintf(AStream,"/Count 1\n"); 
    fprintf(AStream,">>\n"); 
    fprintf(AStream,"endobj\n"); 
    ObjectIndex++; 
    ObjectPosArray[ObjectIndex] =ftell(AStream); 
    fprintf(AStream,"%d 0 obj\n",ObjectIndex+1); 
    fprintf(AStream,"<<\n"); 
    fprintf(AStream,"/Type /Page\n"); 
    fprintf(AStream,"/Parent 2 0 R\n"); 
    fprintf(AStream,"/Resources\n"); 
    fprintf(AStream,"<<\n"); 
    fprintf(AStream,"/XObject << /Im0 4 0 R >>\n"); 
    fprintf(AStream,"/ProcSet [ /PDF /ImageC ]\n"); 
    fprintf(AStream,">>\n"); 
    fprintf(AStream,"/MediaBox [ 0 0 %d %d ]\n",j_Width,j_Height); 
    fprintf(AStream,"/Contents 5 0 R\n"); 
    fprintf(AStream,">>\n"); 
    fprintf(AStream,"endobj\n"); 
    ObjectIndex++; 
    ObjectPosArray[ObjectIndex] =ftell(AStream);  
    fprintf(AStream,"%d 0 obj\n",ObjectIndex+1); 
    fprintf(AStream,"<<\n"); 
    fprintf(AStream,"/Type /XObject\n"); 
    fprintf(AStream,"/Subtype /Image\n"); 
    fprintf(AStream,"/Name /Im0\n"); 
    fprintf(AStream,"/Width %d\n",j_Width); 
    fprintf(AStream,"/Height %d\n",j_Height); 
    fprintf(AStream,"/BitsPerComponent 8\n"); 
    fprintf(AStream,"/Filter [/DCTDecode]\n"); 
    fprintf(AStream,"/ColorSpace /DeviceRGB\n"); 
    fprintf(AStream,"/Length %d >>\n",FileLength); 
    fprintf(AStream,"stream\n");        
    if (CopyStream(JPGStream,AStream)==false)  return -1; 
    fprintf(AStream,"endstream\n"); 
    fprintf(AStream,"endobj\n"); 
    ObjectIndex++; 
    WriteContentsObject(AStream,ObjectPosArray,&ObjectIndex,j_Width,j_Height); 
    ObjectPosArray[ObjectIndex] =ftell(AStream); 
    WriteCrossReferenceTable(AStream,ObjectPosArray,(int)ObjectIndex); 
    fprintf(AStream,"trailer\n"); 
    fprintf(AStream,"<<\n"); 
    fprintf(AStream,"/Size %d\n",ObjectIndex+1); 
    fprintf(AStream,"/Root 1 0 R\n"); 
    fprintf(AStream,">>\n"); 
    fprintf(AStream,"startxref\n"); 
    fprintf(AStream,"%d\n",ObjectPosArray[ObjectIndex]); 
    fprintf(AStream,"%%%%EOF\n"); 
    fclose(JPGStream); fclose(AStream); 
    return 0; 
 
} 
 
 
int start_toJPG(char *NameBmp,bool b_pdf) 
{ 
    string  namejpg; 
    string  namepdf;                
	namejpg=NameBmp; 
  	namepdf=namejpg;         
    namepdf +=".pdf";  	 
    if (b_pdf) namejpg +="_"; else namejpg +=".jpg";;  
    if ((Bmp2Jpg(NameBmp,(char *)namejpg.c_str() )) == 0)  
       if (b_pdf){ 
          JPGtoPDF(namejpg.c_str(),namepdf.c_str()); 
          remove(namejpg.c_str());  
       } 
} 
 
 
int Bmp2Jpg( char * pszBmpFile, char * pszJpgFile ) 
{ 
  struct jpeg_compress_struct cinfo; 
  struct jpeg_error_mgr jerr; 
  cjpeg_source_ptr src_mgr; 
  FILE * input_file; 
  FILE * output_file; 
  JDIMENSION num_scanlines; 
 
  cinfo.err = jpeg_std_error(&jerr); 
  jpeg_create_compress(&cinfo); 
 
  jerr.addon_message_table = cdjpeg_message_table; 
  jerr.first_addon_message = JMSG_FIRSTADDONCODE; 
  jerr.last_addon_message = JMSG_LASTADDONCODE; 
 
 
  cinfo.in_color_space = JCS_RGB; 
  cinfo.smoothing_factor=qjpg; 
  cinfo.data_precision=qjpg; 
  jpeg_set_defaults(&cinfo); 
  jpeg_set_quality(&cinfo, qjpg, TRUE); 
 
  if ((input_file = fopen(pszBmpFile, READ_BINARY)) == NULL) { 
    return -1; 
  } 
 
  if ((output_file = fopen(pszJpgFile, WRITE_BINARY)) == NULL) { 
    return -1; 
  } 
   
  src_mgr = (cjpeg_source_ptr)jinit_read_bmp(&cinfo); 
  src_mgr->input_file = input_file; 
 
  (*src_mgr->start_input) (&cinfo, src_mgr); 
 
  jpeg_default_colorspace(&cinfo); 
 
  jpeg_stdio_dest(&cinfo, output_file); 
 
  jpeg_start_compress(&cinfo, TRUE); 
 
  j_Width=cinfo.image_width; 
  j_Height=cinfo.image_height; 
 
 
  while (cinfo.next_scanline < cinfo.image_height) { 
    num_scanlines = (*src_mgr->get_pixel_rows) (&cinfo, src_mgr); 
    (void) jpeg_write_scanlines(&cinfo, src_mgr->buffer, num_scanlines); 
  } 
 
  (*src_mgr->finish_input) (&cinfo, src_mgr); 
  jpeg_finish_compress(&cinfo); 
  jpeg_destroy_compress(&cinfo); 
 
  if (input_file != stdin) 
    fclose(input_file); 
  if (output_file != stdout) 
    fclose(output_file); 
 
  return 0; 
}