實驗3用于實現(xiàn)JPEG編解碼的CJpeg類的定義_第1頁
實驗3用于實現(xiàn)JPEG編解碼的CJpeg類的定義_第2頁
實驗3用于實現(xiàn)JPEG編解碼的CJpeg類的定義_第3頁
實驗3用于實現(xiàn)JPEG編解碼的CJpeg類的定義_第4頁
實驗3用于實現(xiàn)JPEG編解碼的CJpeg類的定義_第5頁
已閱讀5頁,還剩3頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)

文檔簡介

1、實驗三 用于實現(xiàn)JPEG編解碼的CJpeg類的定義實驗?zāi)康模菏箤W(xué)生定義類CJpeg用于實現(xiàn)JPEG編解碼中關(guān)鍵技術(shù)的原理和算法,使用VC+6.0對這些算法加以封裝。實現(xiàn)實現(xiàn)與設(shè)備無關(guān)的位圖壓縮成JPEG圖像和將JPEG圖像解壓縮成與設(shè)備無關(guān)的位圖。實驗環(huán)境:具有多媒體處理功能的計算安裝有Windows操作系統(tǒng),安裝有Visual c+6.0程序設(shè)計軟件。實驗要求:學(xué)習(xí)相關(guān)理論指導(dǎo),掌握相關(guān)程序設(shè)計知識;按照實驗步驟要求完成程序設(shè)計任務(wù),書寫實驗報告,試驗報告中要求包含程序?qū)崿F(xiàn)的主要程序代碼。實驗內(nèi)容和實驗步驟:1、聲明CJpeg類 CJpeg類的聲明如下:#include "Dib.

2、h"class CJpeg public:CJpeg();CJpeg(CDib *pDib);virtual CJpeg();public: /獲取CDibCDib * GetDib()return m_pDib; /設(shè)置CDibBOOL SetDib(CDib *pDib)if (pDib = NULL)return FALSE;if (m_pDib != NULL)delete m_pDib;m_pDib = pDib->Clone();return (m_pDib != NULL);/ 載入JPEG文件BOOL Load(LPCSTR lpstrFileName);/ 存

3、儲JPEG文件BOOL Save(LPCSTR lpstrFileName, CDib* pDib = NULL, BOOL bColor = TRUE, int nQuality = 75);/ 獲取錯誤信息CString GetErrorString();private:/讀取JPEG文件到緩沖區(qū)BYTE* ReadJPEGFile(LPCSTR lpstrFileName, UINT *uWidth, UINT *uHeight);/將位圖文件存儲為JPEG文件BOOL WriteJPEGFile(LPCTSTR lpstrFileName,/ path BYTE *dataBuf,/

4、RGB buffer UINT width,/ pixels UINT height,/ rows BOOL color,/ TRUE = RGB/ FALSE = Grayscale int quality);/ 0 - 100/ allocates a DWORD-aligned buffer, copies data buffer/ caller is responsible for delete 'ing the bufferBYTE* MakeDwordAlign(BYTE *dataBuf,/ input buf UINT widthPix,/ input pixels U

5、INT height,/ lines UINT *uiOutWidthBytes);/ new width bytesvoid FreeBuffer(BYTE *Buffer);/ if you have a DWORD aligned buffer, this will copy the/ RGBs out of it into a new buffer. new width is widthPix * 3 bytes/ caller is responsible for delete 'ing the bufferBYTE *ClearDwordAlign(BYTE *inBuf,

6、/ input bufUINT widthPix,/ input sizeUINT widthBytes,/ input sizeUINT height);BOOL VertFlipBuf(BYTE * inbuf,/ input buf UINT widthBytes,/ input width bytes UINT height);/ heightBOOL BGRFromRGB(BYTE *buf,/ input bufUINT widthPix,/ width in pixelsUINT height);/ lines/ dataCString m_strJPEGError;/datap

7、rivate:/CDib對象CDib*m_pDib;2、編寫CJpeg類的實現(xiàn)文件,實現(xiàn)ReadJPEGFile用于讀取JPEG文件到RGB緩沖區(qū),實現(xiàn)了對JPEG文件的解碼過程。函數(shù)原型為:BYTE *ReadJPEGFile(LPCSTR lpstrFileName,UINT *uWidth,UINT *uHeight)參數(shù)為:lpstrFileName 包含JPEG文件的全部路徑uWidth 圖像的寬度,即每行像素的個數(shù)uHeight 圖像的高度,即像素的行數(shù)返回值:數(shù)據(jù)緩沖區(qū)的地址 參考程序:BYTE* CJpeg:ReadJPEGFile(LPCSTR lpstrFileName,

8、UINT *uWidth, UINT *uHeight)*uWidth=0;*uHeight=0;/定義JPEG文件的解壓信息struct jpeg_decompress_struct cinfo;/定義JPEG文件的錯誤信息struct my_error_mgr jerr;/定義緩沖區(qū)FILE * infile;JSAMPARRAY buffer;int row_stride;char buf250; /打開JPEG文件if (infile = fopen(lpstrFileName, "rb") = NULL) sprintf(buf, "JPEG :nCan

9、't open %sn", lpstrFileName);m_strJPEGError = buf;return NULL; /為JPEG文件解壓對象分配內(nèi)存并對其初始化cinfo.err = jpeg_std_error(&jerr.pub);jerr.pub.error_exit = my_error_exit;if (setjmp(jerr.setjmp_buffer) jpeg_destroy_decompress(&cinfo);fclose(infile);return NULL;jpeg_create_decompress(&cinfo)

10、; /設(shè)定數(shù)據(jù)源 jpeg_stdio_src(&cinfo, infile); /讀取JPEG文件參數(shù)(void) jpeg_read_header(&cinfo, TRUE); /開始解壓(void) jpeg_start_decompress(&cinfo);BYTE *dataBuf;dataBuf=(BYTE *)new BYTEcinfo.output_width * 3 * cinfo.output_height;if (dataBuf=NULL) m_strJPEGError = "JpegFile :nOut of memory"j

11、peg_destroy_decompress(&cinfo);fclose(infile);return NULL;*uWidth = cinfo.output_width;*uHeight = cinfo.output_height;row_stride = cinfo.output_width * cinfo.output_components;buffer = (*cinfo.mem->alloc_sarray)(j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);/讀取掃描線while (cinfo.output_s

12、canline < cinfo.output_height) (void) jpeg_read_scanlines(&cinfo, buffer, 1);if (cinfo.out_color_components=3) j_putRGBScanline(buffer0, *uWidth,dataBuf,cinfo.output_scanline-1); else if (cinfo.out_color_components=1) j_putGrayScanlineToRGB(buffer0, *uWidth,dataBuf,cinfo.output_scanline-1); /

13、完成解壓(void) jpeg_finish_decompress(&cinfo); /釋放JPEG解壓對象jpeg_destroy_decompress(&cinfo);fclose(infile);return dataBuf;3、編寫CJpeg類的實現(xiàn)文件,實現(xiàn)WriteJPEGFile用于將位圖文件寫入JPEG文件格式,實現(xiàn)了JPEG文件的壓縮過程。 函數(shù)原型:BOOL WriteJPEGFile(LPCSTR lpstrFileName,BYTE *dataBuf, UINT widthPix,UINT height,BOOL color,int quality) 參

14、數(shù):lpstrFileName 包含要寫入的JPEG文件的全部路徑dataBuf RGB文件指針widthPix 圖像的寬度,即每行像素的個數(shù)height 圖像的高度,即像素的行數(shù)color 顏色標(biāo)志,true為彩色,false為黑白quality 壓縮圖像質(zhì)量,范圍為0100 返回值:操作成功返回為TRUE參考代碼為: BOOL CJpeg:WriteJPEGFile(LPCSTR lpstrFileName, BYTE *dataBuf,UINT widthPix,UINT height,BOOL color, int quality)if (dataBuf=NULL)return FAL

15、SE;if (widthPix=0)return FALSE;if (height=0)return FALSE;LPBYTE tmp;if (!color) tmp = (BYTE*)new BYTEwidthPix*height;if (tmp=NULL) m_strJPEGError = "Memory error"return FALSE;UINT row,col;for (row=0;row<height;row+) for (col=0;col<widthPix;col+) LPBYTE pRed, pGrn, pBlu;pRed = dataBuf

16、 + row * widthPix * 3 + col * 3;pGrn = dataBuf + row * widthPix * 3 + col * 3 + 1;pBlu = dataBuf + row * widthPix * 3 + col * 3 + 2;/ 計算圖像亮度值int lum = (int)(.299 * (double)(*pRed) + .587 * (double)(*pGrn) + .114 * (double)(*pBlu);LPBYTE pGray;pGray = tmp + row * widthPix + col;*pGray = (BYTE)lum; /定

17、義壓縮信息struct jpeg_compress_struct cinfo; /定義緩沖區(qū)FILE * outfile;int row_stride; /定義錯誤信息struct my_error_mgr jerr; /為JPEG文件壓縮對象分配內(nèi)存并對其初始化cinfo.err = jpeg_std_error(&jerr.pub);jerr.pub.error_exit = my_error_exit;if (setjmp(jerr.setjmp_buffer) jpeg_destroy_compress(&cinfo);fclose(outfile);if (!colo

18、r) delete tmp;return FALSE;jpeg_create_compress(&cinfo); /打開文件并設(shè)定數(shù)據(jù)目標(biāo) if (outfile = fopen(lpstrFileName, "wb") = NULL) char buf250;sprintf(buf, "JpegFile :nCan't open %sn", lpstrFileName);m_strJPEGError = buf;return PE;jpeg_stdio_dest(&cinfo, outfile);/設(shè)置壓縮參數(shù)cinfo.ima

19、ge_width = widthPix; cinfo.image_height = height;if (color) cinfo.input_components = 3;cinfo.in_color_space = JCS_RGB; else cinfo.input_components = 1;cinfo.in_color_space = JCS_GRAYSCALE; jpeg_set_defaults(&cinfo); jpeg_set_quality(&cinfo, quality, TRUE ); /開始壓縮 jpeg_start_compress(&cinfo, TRUE); /寫入掃描線 row_stride = widthPix * 3; while (cinfo.next_scanline < cinfo.image_height) LPBYTE outRow;if (color) outRow = dataBuf + (cinfo.next_scanline * widthPix * 3); else outRow = tmp + (cinfo.next_scanline * widthPix);

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論