bmp轉jpg(使用libjpeg)

  jpg壓縮原理可以參考這篇文章http://hi.baidu.com/tiandsp/item/f5a2dcde6ef1405bd73aae41,我很早以前轉的一篇文章。

  沒有使用libjpeg的壓縮代碼可以看看這篇文章http://hi.baidu.com/tiandsp/item/9b5843c58a3b4474cfd4f841,也是我很早以前轉的。

  這次使用libjpeg庫壓縮和上一篇的解壓正好對應起來,有好多函數名稱我都是對稱的起的,所以結合起來看效果更好。

  和上一篇一樣,只能處理24位和8位的圖像。

  代碼如下:

#include <iostream>
#include <stdio.h>
extern "C"{
#include "jpeglib.h"
};
#pragma comment(lib,"jpeg.lib")
using namespace std;#pragma pack(2)        //兩字節對齊,否則bmp_fileheader會占16Byte
struct bmp_fileheader
{unsigned short    bfType;        //若不對齊,這個會占4Byteunsigned long    bfSize;unsigned short    bfReverved1;unsigned short    bfReverved2;unsigned long    bfOffBits;
};struct bmp_infoheader
{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;
};FILE *input_file;
FILE *output_file;struct bmp_fileheader bfh;
struct bmp_infoheader bih;unsigned char *src_buffer;
unsigned char *dst_buffer;void read_bmp_header()
{    fread(&bfh,sizeof(struct bmp_fileheader),1,input_file);fread(&bih,sizeof(struct bmp_infoheader),1,input_file);
}void read_bmp_data()
{fseek(input_file,bfh.bfOffBits,SEEK_SET);src_buffer=new unsigned char[bih.biWidth*bih.biHeight*bih.biBitCount/8];fread(src_buffer,sizeof(unsigned char)*bih.biWidth*bih.biHeight*bih.biBitCount/8,1,input_file);unsigned long width=bih.biWidth;unsigned long height=bih.biHeight;unsigned short depth=unsigned short(bih.biBitCount/8);unsigned char *src_point;unsigned char *dst_point;dst_buffer=new unsigned char[width*height*depth];    src_point=src_buffer+width*depth*(height-1);dst_point=dst_buffer+width*depth*(height-1);for (unsigned long i=0;i<height;i++){for (unsigned long j=0;j<width*depth;j+=depth){if (depth==1)        //處理灰度圖
            {dst_point[j]=src_point[j];}if (depth==3)        //處理彩色圖
            {dst_point[j+2]=src_point[j+0];dst_point[j+1]=src_point[j+1];dst_point[j+0]=src_point[j+2];}}dst_point-=width*depth;src_point-=width*depth;}
}void synthese_jpeg()
{struct jpeg_compress_struct cinfo;struct jpeg_error_mgr jerr;JSAMPARRAY buffer;unsigned long width=bih.biWidth;unsigned long height=bih.biHeight;unsigned short depth=unsigned short(bih.biBitCount/8);unsigned char *point;cinfo.err=jpeg_std_error(&jerr);        //libjpeg各種配置jpeg_create_compress(&cinfo);jpeg_stdio_dest(&cinfo,output_file);cinfo.image_width=width;cinfo.image_height=height;cinfo.input_components=depth;if (depth==1)cinfo.in_color_space=JCS_GRAYSCALE;elsecinfo.in_color_space=JCS_RGB;jpeg_set_defaults(&cinfo);jpeg_set_quality(&cinfo,20,TRUE);    //中間的值為壓縮質量,越大質量越好jpeg_start_compress(&cinfo,TRUE);buffer=(*cinfo.mem->alloc_sarray)((j_common_ptr)&cinfo,JPOOL_IMAGE,width*depth,1);point=dst_buffer+width*depth*(height-1);while (cinfo.next_scanline<height){memcpy(*buffer,point,width*depth);jpeg_write_scanlines(&cinfo,buffer,1);point-=width*depth;}jpeg_finish_compress(&cinfo);jpeg_destroy_compress(&cinfo);
}int main()
{input_file=fopen("lena_gray.bmp","rb");output_file=fopen("lena.jpg","wb");read_bmp_header();read_bmp_data();synthese_jpeg();fclose(input_file);fclose(output_file);delete[] src_buffer;delete[] dst_buffer;cout<<"good job."<<endl;cin.get();return 0;
}

轉載于:https://www.cnblogs.com/tiandsp/archive/2012/12/03/2799561.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/274795.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/274795.shtml
英文地址,請注明出處:http://en.pswp.cn/news/274795.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

figma設計_Figma與Adobe XD:我們如何選擇下一個設計工具

figma設計The time came for changes and our design team started raising the topic again about how we should consider moving away from Sketch. This is not the first time this question came to mind, but this time seems like it was serious. Last summer we cons…

一個小廠前端 Leader 如何篩選候選人?

大家好&#xff0c;我是若川。持續組織了8個月源碼共讀活動&#xff0c;感興趣的可以點此加我微信 ruochuan12 參與&#xff0c;每周大家一起學習200行左右的源碼&#xff0c;共同進步。同時極力推薦訂閱我寫的《學習源碼整體架構系列》 包含20余篇源碼文章。歷史面試系列本文作…

figma設計_如何在Figma中構建設計入門套件(第1部分)

figma設計Figma教程 (Figma Tutorial) Do you like staring at a blank canvas every time you start a new project in Figma?每次在Figma中啟動新項目時&#xff0c;您是否喜歡盯著一塊空白的畫布&#xff1f; I’m guessing you’re not a big fan right, but it’s a pra…

純靠技術,很難進入大廠了。。。

日前&#xff0c;國務院印發《“十四五”時期就業促進規劃的通知》&#xff0c;其中明確指出&#xff0c;要完善終身學習體系&#xff0c;推進高水平大學開放教育資源&#xff0c;暢通在職人員繼續教育與終身學習通道。為響應國家政策&#xff0c;現臨時擴大招生規模&#xff0…

十天學會ASP.Net——(8)

1. ajax入門AJAX Extensions工具箱 &#xff08;1&#xff09;實現又刷新改變字體大小和無刷新改變字體大小 頁面設計&#xff1a; 前臺頁面設計&#xff1a; 首先需要在用到ajaxExtensions控件的位置之前放置一個ScriptManager控件&#xff0c;它將在瀏覽頁面時不可見&#xf…

聊聊 computed 影響性能的場景

大家好&#xff0c;我是若川。持續組織了8個月源碼共讀活動&#xff0c;感興趣的可以點此加我微信 ruochuan12 參與&#xff0c;每周大家一起學習200行左右的源碼&#xff0c;共同進步。同時極力推薦訂閱我寫的《學習源碼整體架構系列》 包含20余篇源碼文章。歷史面試系列前言在…

saej1929_(1929年-2020年)

saej1929Milton Glaser, the legendary graphic designer who co-founded New York Magazine, created the iconic ‘I ? NY’ logo, the psychedelic Bob Dylan poster, and the Brooklyn Brewery logo, passed away yesterday at the age of 91 on his birthday, June 26, 2…

Chap2-構造函數語意學

如果一個類沒有任何constructor&#xff0c;那么會有一個default constructor被隱式的聲明出來&#xff0c;一個implicit default constructor將是一個trivial&#xff08;無用的&#xff09;constructor。但是在某些情況下&#xff0c;implicit default constructor將是一個no…

【熱點】React18正式版發布,未來發展趨勢是?

大家好&#xff0c;我是若川。持續組織了8個月源碼共讀活動&#xff0c;感興趣的可以點此加我微信 ruochuan12 參與&#xff0c;每周大家一起學習200行左右的源碼&#xff0c;共同進步。同時極力推薦訂閱我寫的《學習源碼整體架構系列》 包含20余篇源碼文章。歷史面試系列2022年…

不要重新發明輪子_是否重新發明輪子

不要重新發明輪子Design is a profession that thrives on creativity. Us designers are constantly trying to innovate by thinking outside the box. We’ve seen design evolve across all sectors — print, digital, product, architecture etc. We have gone from type…

asp.net mvc批量刪除的實現

<form action"Index" method"post"> <div> {<table><thead> <tr> <th width"100">編號</th><th width"100">名字</th></tr></thead> <tbody> foreach (var…

點擊頁面元素,這個Vite插件竟然幫我打開了Vue組件文件!超級好用!

大家好&#xff0c;我是若川。持續組織了8個月源碼共讀活動&#xff0c;感興趣的可以點此加我微信 ruochuan12 參與&#xff0c;每周大家一起學習200行左右的源碼&#xff0c;共同進步。同時極力推薦訂閱我寫的《學習源碼整體架構系列》 包含20余篇源碼文章。歷史面試系列前言這…

shields 徽標_符號,標志,文字標記:徽標類型的綜合指南

shields 徽標Designers and non-designers alike struggle with common terminology when talking about brand marks, often using different terms interchangeably. When it comes to clarifying definitions, sometimes even the most seasoned professionals get confused…

【原創】SVM小結

理論基礎&#xff1a; 機器學習有三類基本的問題&#xff0c;即模式識別、函數逼近和概率密度估計&#xff0e; SVM有著嚴格的理論基礎&#xff0c;建立了一套較好的有限訓練樣本下機器學習的理論框架和通用方法。他與機器學習是密切相關的&#xff0c;很多理論甚至解決了機器學…

React 18 帶給我們的驚喜

大家好&#xff0c;我是若川。持續組織了8個月源碼共讀活動&#xff0c;感興趣的可以點此加我微信 ruochuan12 參與&#xff0c;每周大家一起學習200行左右的源碼&#xff0c;共同進步。同時極力推薦訂閱我寫的《學習源碼整體架構系列》 包含20余篇源碼文章。歷史面試系列這篇文…

建模心法(2)——邁出建模第一步

原文地址&#xff1a;http://www.cnblogs.com/1-2-3/archive/2008/08/04/model-method-part1.html 原文作者&#xff1a;景春雷 一錯再錯的這故事才精彩 ——樸樹 《我愛你再見》摘要 即使讀了再多的書、跟過再多的項目&#xff0c;…

Web:你知道我這十幾年是怎么過來的嗎?!

大家好&#xff0c;我是若川。持續組織了8個月源碼共讀活動&#xff0c;感興趣的可以點此加我微信 ruochuan12 參與&#xff0c;每周大家一起學習200行左右的源碼&#xff0c;共同進步。同時極力推薦訂閱我寫的《學習源碼整體架構系列》 包含20余篇源碼文章。歷史面試系列1989 …

設計師更高效_如何丟掉我的工作使我成為一名更好的設計師

設計師更高效I lost my job a few times early on in my design career. In the process of getting back up after a job loss, it has made me a better designer not only in terms of hard skills but the soft skills required to be more resilient and empathetic, whic…

【ASP.NET】登陸成功后如何跳轉到上一個頁面

當用戶瀏覽網頁的時候會在某個地方需要用戶登陸才能繼續瀏覽&#xff0c;用戶登陸之后會自動跳轉到剛剛瀏覽的頁面。這個步驟是怎么實現的呢&#xff1f;net小伙在查閱相關資料實踐之后終于明白了&#xff0c;其實很簡單&#xff0c;先分享給大家吧。 當用戶在瀏覽一個頁面的時…

4月,誠邀你參加源碼共讀,學會看源碼,打開新世界!開闊視野

大家好&#xff0c;我是若川。很多關注我的新朋友可能不知道我組織了源碼共讀活動~也有很多人不知道我是誰。有人以為我是80后。有人以為我是全職自媒體等等。若川的 2021 年度總結&#xff0c;彈指之間 這篇文章寫了我是16年畢業的&#xff0c;或許有些啟發。源碼共讀按照從易…