C. jpg 를 BMP 첨부 파일 로 변환 합 니 다.

기본 사고방식:
      libjpeg 라 이브 러 리 를 이용 하여 jpg 파일 의 압축 을 풀 고 데 이 터 를 bmp (아래 에서 위로, 왼쪽 에서 오른쪽으로, BGR) 로 저장 합 니 다.
주의사항:
              1: bmp 데이터 저장 시 BGR 순서대로
           2: biHeight 가 정수 일 때 거꾸로 된 비트 맵 을 표시 합 니 다. 읽 는 순 서 는 (왼쪽 - > 오른쪽, 아래 - > 위) 입 니 다.
           3: 윈도 우즈 는 줄 스 캔 을 할 때 가장 작은 단 위 는 4 개의 바이트 이기 때문에 줄 마다 4 의 정수 배가 되 지 않 을 때 바이트 수 를 조정 해 야 합 니 다. 줄 마다 0 을 보충 하 는 것 이 부족 합 니 다.
           4: 이미지 비트 맵 헤더 정보의 높이 와 너 비 는 실제 이미지 의 픽 셀 점 의 개수 로 저장 할 때 그 데 이 터 를 4 바이트 의 배 수 를 만족 시 키 도록 조정 해 야 하 는 것 과 무관 합 니 다.
실험 내용:
단계 1: libjpeg 라 이브 러 리 를 이용 하여 jpg 파일 의 압축 을 풀 고 데 이 터 를 추출 합 니 다.
	FILE *fJpeg;//  jpeg     
	struct jpeg_decompress_struct cinfo;
	struct jpeg_error_mgr jerr;
	cinfo.err = jpeg_std_error(&jerr);
	jpeg_create_decompress(&cinfo);//           
 	row_stride = cinfo.output_width * cinfo.output_components; //         ,     
         while (cinfo.output_scanline < cinfo.output_height)
        {
            int line=cinfo.output_scanline;//    
 
              (void) jpeg_read_scanlines(&cinfo, &jpgbuf, 1);//        line   ,cinfo.output_scanline   ,          
 
              for(int i=0;i< cinfo.output_width;i++)//      jpgbuf        data 
                      {      
                              data[line*row_stride+i*cinfo.output_components+0]=jpgbuf[i*3];
                              data[line*row_stride+i*cinfo.output_components+1]=jpgbuf[i*3+1];
                              data[line*row_stride+i*cinfo.output_components+2]=jpgbuf[i*3+2];
                       }
 
        }
         fJpeg=fopen(JpegName,"rb");//       jpeg  
          jpeg_stdio_src(&cinfo, fJpeg);//          
          jpeg_read_header(&cinfo, TRUE);//      ,            cinfo         
          jpeg_start_decompress(&cinfo);//     
          data=(unsigned char *)malloc(cinfo.output_width* cinfo.output_components*cinfo.output_width);//          
          memset(data,0,cinfo.output_width*cinfo.output_width*cinfo.output_components);//         0
        jpgbuf = (unsigned char *) malloc(cinfo.output_width *cinfo.output_components);//        
          memset(jpgbuf,0,cinfo.output_width*cinfo.output_components);//         

단계 2: 데 이 터 를 bmp 파일 에 기록 합 니 다 (bmp 파일 에 데 이 터 를 저장 하 는 순서 로 저장 해 야 합 니 다)
 //       
        	infoheader.biSize=sizeof(infoheader);//          , 40
       	infoheader.biBitCount=24;//      
        	infoheader.biHeight=cinfo.image_height;
        	infoheader.biWidth=cinfo.image_width;
        	infoheader.biCompression=BI_RGB;//     BI_RGB       
        	infoheader.biPlanes=1;//      
        	infoheader.biXPelsPerMeter=0;
        	infoheader.biYPelsPerMeter=0;
        	infoheader.biClrUsed=0;// MSDN  RGB          0
 
//                
        	fNewBmp=fopen(NewBmpName,"wb");//           
        	if(!fNewBmp)
        	{
              cout<>2)<<2;//        ,   4   
#ifdef DEBUG__
        	cout<>2)<<2 IS "<>2)<<2)<
 

 원본 파일:
void JpegToBmp( char *JpegName,char * NewBmpName)//   jpeg        BMP     ,     24    ,      
{
       
          FILE *fNewBmp;//     Bmp     
        	FILE *fJpeg;//  jpeg     
        	unsigned char *data;   //        
        	unsigned char *jpgbuf;      //           
        	BITMAPFILEHEADER header;//     
        	memset(&header, 0, sizeof(header));//       
        	BITMAPINFOHEADER infoheader;//     
        	memset(&infoheader, 0, sizeof(infoheader));//    
 
        	int row_stride;        //        
        	struct jpeg_decompress_struct cinfo;
	struct jpeg_error_mgr jerr;
        cinfo.err = jpeg_std_error(&jerr);
        jpeg_create_decompress(&cinfo);//           
 
        	fJpeg=fopen(JpegName,"rb");//       jpeg  
       
        if(fJpeg==NULL) //       
        {
              printf("error: cannot open  the file
"); return ; }// jpeg jpeg_stdio_src(&cinfo, fJpeg);// jpeg_read_header(&cinfo, TRUE);// , cinfo jpeg_start_decompress(&cinfo);// data=(unsigned char *)malloc(cinfo.output_width* cinfo.output_components*cinfo.output_width);// memset(data,0,cinfo.output_width*cinfo.output_width*cinfo.output_components);// 0 jpgbuf = (unsigned char *) malloc(cinfo.output_width *cinfo.output_components);// memset(jpgbuf,0,cinfo.output_width*cinfo.output_components);// row_stride = cinfo.output_width * cinfo.output_components; // , while (cinfo.output_scanline < cinfo.output_height) { int line=cinfo.output_scanline;// (void) jpeg_read_scanlines(&cinfo, &jpgbuf, 1);// line ,cinfo.output_scanline , for(int i=0;i< cinfo.output_width;i++)// jpgbuf data { data[line*row_stride+i*cinfo.output_components+0]=jpgbuf[i*3]; data[line*row_stride+i*cinfo.output_components+1]=jpgbuf[i*3+1]; data[line*row_stride+i*cinfo.output_components+2]=jpgbuf[i*3+2]; #ifdef SHOWDATA__ printf("(%d,%d,%d),(%d,%d)",jpgbuf[i*3],jpgbuf[i*3+1],jpgbuf[i*3+2],line,i);// #endif } } free(jpgbuf); // header.bfType= 0x4D42;// “BM” header.bfSize=sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)+cinfo.output_width* cinfo.output_components*cinfo.output_width ; header.bfOffBits=sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER); #ifdef DEBUG__ cout<>2)<<2;// , 4 #ifdef DEBUG__ cout<>2)<<2 IS "<>2)<<2)<

참고:
[꼭대기] libjpeg 를 사용 하여 jpeg 품질 인 자 를 추출 합 니 다.          
C. BMP 를 JPG 소스 코드 로 변환 합 니 다.

좋은 웹페이지 즐겨찾기