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 소스 코드 로 변환 합 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Python 버 전의 cairo 모듈 설치: PycairoWindows 에 cairo 를 어떻게 설치 하 는 지, 특히 for python 을 어떻게 설치 하 는 지. 대응 하 는 Pycairo 를 찾 았 는데 보 니 아까 홈 페이지 에 있 던 것 같 습 니 다. 그리고 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.