• 大小: 6.55MB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2023-09-24
  • 语言: C/C++
  • 标签: BMP  JPEG  C++  

资源简介

C++代码,支持图pain从BMP格式转换为JPEG格式,已经封装好的C++ CBmp2Jpeg类,可直接调用: CBmp2Jpeg bmp; bmp.Bmp2Jpeg("111_24.bmp", "lena.jpg"); cout<<"good job."<<endl; cin.get(); return 0;

资源截图

代码片段和文件信息

#include 
#include
#include 
//#include
#include “Bmp2Jpeg.h“
#include 

extern “C“
{
#include “jpeglib.h“
};

#pragma comment(lib“libjpeg.lib“)

using namespace std;

#pragma pack(2)  

struct bmp_fileheader   //文件头,长度为14Byte固定  
{  
unsigned short bfType;  
unsigned long bfSize;  
unsigned short bfReserved1;  
unsigned short bfReserved2;  
unsigned long bfOffBits;  
};  

struct bmp_infoheader  //文件信息头,长度为40Byte固定  
{  
unsigned long biSize;  
unsigned long biWidth;  
unsigned long biHeight;  
unsigned short biPlanes;  
unsigned short biBitCount;  
unsigned long biCompression;  
unsigned long biSizeImage;  
unsigned long biXPelsPerMeter;  
unsigned long biYPelsPerMeter;  
unsigned long biClrUsed;  
unsigned long biClrImportant;  
}; 

struct RGBPallete
{
unsigned char b;
unsigned char g;
unsigned char r;
unsigned char alpha;
};

CBmp2Jpeg::CBmp2Jpeg() :
m_quality(JPEG_QUALITY)
{

}

CBmp2Jpeg::~CBmp2Jpeg()
{

}
/*===================================================================================
function:       jpeg压缩
input:          1:生成的文件名2:bmp的指针3:位图宽度4:位图高度5:颜色深度
return:         int
description:    bmp的像素格式为(RGB)
===================================================================================*/
int CBmp2Jpeg::SaveJpeg(const char *filename unsigned char *bits int width int height int depth)
{
FILE * outfile;                 /* target file */
fopen_s(&outfile filename “wb“);
if (outfile == NULL) {
return -1;
}

struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;

cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);

jpeg_stdio_dest(&cinfo outfile);

cinfo.image_width = width;      /* image width and height in pixels */
cinfo.image_height = height;
cinfo.input_components = 3;         /* # of color components per pixel */
cinfo.in_color_space = JCS_RGB;         /* colorspace of input image */

jpeg_set_defaults(&cinfo);
jpeg_set_quality(&cinfo m_quality TRUE /* limit to baseline-JPEG values */);

jpeg_start_compress(&cinfo TRUE);

JSAMPROW row_pointer[1];        /* pointer to JSAMPLE row[s] */
int     row_stride;             /* physical row width in image buffer */
row_stride = width * depth; /* JSAMPLEs per row in image_buffer */

while (cinfo.next_scanline < cinfo.image_height) {
//这里我做过修改,由于jpg文件的图像是倒的,所以改了一下读的顺序
//row_pointer[0] = & bits[cinfo.next_scanline * row_stride];
row_pointer[0] = & bits[(cinfo.image_height - cinfo.next_scanline - 1) * row_stride];
(void) jpeg_write_scanlines(&cinfo row_pointer 1);
}

jpeg_finish_compress(&cinfo);
fclose(outfile);

jpeg_destroy_compress(&cinfo);
return 0;
}

void CBmp2Jpeg::InitFileHeader(void *pFile void *fileHeader)
{
bmp_fileheader *pfileHeader = (bmp_fileheader *)fileHeader;
fstream *filein = (fstream *)pFile;
//初始化文件头
pfileHeader-

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

    ..A..H.     33280  2018-05-22 10:06  Bmp2jpeg\.vs\Bmp2jpeg\v14\.suo

     文件    6220854  2017-06-22 11:19  Bmp2jpeg\Bmp2jpeg\111_24.bmp

     文件     276598  2017-06-22 10:59  Bmp2jpeg\Bmp2jpeg\16.bmp

     文件    2211894  2017-06-22 10:57  Bmp2jpeg\Bmp2jpeg\2017622105730.bmp

     文件    2211894  2017-06-22 11:00  Bmp2jpeg\Bmp2jpeg\24.bmp

     文件     554038  2017-06-22 11:00  Bmp2jpeg\Bmp2jpeg\256.bmp

     文件       7690  2017-06-22 11:31  Bmp2jpeg\Bmp2jpeg\Bmp2Jpeg.cpp

     文件        723  2017-06-22 11:31  Bmp2jpeg\Bmp2jpeg\Bmp2Jpeg.h

     文件       4224  2017-06-22 13:50  Bmp2jpeg\Bmp2jpeg\Bmp2jpeg.vcxproj

     文件       1155  2017-06-22 13:50  Bmp2jpeg\Bmp2jpeg\Bmp2jpeg.vcxproj.filters

     文件        143  2017-06-22 10:48  Bmp2jpeg\Bmp2jpeg\Bmp2jpeg.vcxproj.user

     文件          3  2018-05-22 10:05  Bmp2jpeg\Bmp2jpeg\Debug\Bmp2jpeg.log

     文件    1335146  2017-06-22 13:54  Bmp2jpeg\Bmp2jpeg\lena.jpg

     文件       3704  2017-06-22 13:54  Bmp2jpeg\Bmp2jpeg\main.cpp

     文件        891  2017-06-22 10:48  Bmp2jpeg\Bmp2jpeg.sln

    ..A..H.     13824  2017-06-22 14:33  Bmp2jpeg\Bmp2jpeg.suo

     文件   15269888  2017-06-22 10:50  Bmp2jpeg\ipch\bmp2jpeg-ab62d05f\bmp2jpeg-17e0ff43.ipch

     文件       5992  2017-04-05 16:18  Bmp2jpeg\jpeglib\jconfig.h

     文件      15371  2017-04-05 16:18  Bmp2jpeg\jpeglib\jmorecfg.h

     文件      50461  2017-04-05 16:18  Bmp2jpeg\jpeglib\jpeglib.h

     文件     835192  2017-04-05 16:18  Bmp2jpeg\jpeglib\libjpeg.lib

     文件       1063  2017-04-05 16:18  Bmp2jpeg\jpeglib\readme.txt

     目录          0  2018-05-22 10:03  Bmp2jpeg\.vs\Bmp2jpeg\v14

     目录          0  2018-05-22 10:03  Bmp2jpeg\.vs\Bmp2jpeg

     目录          0  2018-05-22 10:05  Bmp2jpeg\Bmp2jpeg\Debug

     目录          0  2017-08-08 14:47  Bmp2jpeg\ipch\bmp2jpeg-ab62d05f

    ...D.H.         0  2018-05-22 10:03  Bmp2jpeg\.vs

     目录          0  2017-08-08 14:47  Bmp2jpeg\Bmp2jpeg

     目录          0  2018-05-22 10:05  Bmp2jpeg\Debug

     目录          0  2017-08-08 14:47  Bmp2jpeg\ipch

............此处省略5个文件信息

评论

共有 条评论