JPEG庫的移植(arm平臺)

来源:https://www.cnblogs.com/ljw-boke/p/18190135
-Advertisement-
Play Games

JPEG庫的移植(arm平臺) 目錄JPEG庫的移植(arm平臺)介紹頭文件及全局變數1、圖片顯示2、其他圖片壓縮到jpg圖片3、主函數及驗證程式輸出結果 介紹 圖解 頭文件及全局變數 #include <stdio.h> #include <stdlib.h> #include <sys/type ...


JPEG庫的移植(arm平臺)

目錄

介紹

圖解

頭文件及全局變數

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
/*
 * Include file for users of JPEG library.
 * You will need to have included system headers that define at least
 * the typedefs FILE and size_t before you can include jpeglib.h.
 * (stdio.h is sufficient on ANSI-conforming systems.)
 * You may also wish to include "jerror.h".
 */

#include "jpeglib.h"

#pragma pack(1)

typedef struct tagBmpFileHeader // 文件頭
{
  unsigned short bfType;      // 標識該文件為bmp文件,判斷文件是否為bmp文件,即用該值與"0x4d42"比較是否相等即可,0x4d42 = 19778
  unsigned long bfSize;       // 點陣圖文件大小,包括這14個位元組。
  unsigned short bfReserved1; // 預保留位,暫不用。
  unsigned short bfReserved2; // 預保留位,暫不用。
  unsigned long bfOffBits;    // 圖像數據區的起始位置
} BmpFileHeader;              // 14位元組:short2個,long4個

typedef struct tagBmpInfoHeader // 信息頭
{
  unsigned long biSize;          // 本結構的長度,為40個位元組。
  long biWidth;                  // 寬度
  long biHeight;                 // 高度
  unsigned short biPlanes;       // 目標設備的級別,必須是1。
  unsigned short biBitCount;     // 每個像素所占的位數(bit),其值必須為1(黑白圖像)、4(16色圖)、8(256色)、24(真彩色圖),新的BMP格式支持32位色。
  unsigned longbiCompression;    // 壓縮方式,有效的值為BI_RGB(未經壓縮)、BI_RLE8、BI_RLE4、BI_BITFILEDS(均為Windows定義常量)。
  unsigned longbiSizeImage;      // 圖像區數據大小,即實際的點陣圖數據占用的位元組數
  long biXPelsPerMeter;          // 水平解析度,像素每米
  long biYPelsPerMeter;          // 垂直解析度,單位是像素/米
  unsigned long biClrUsed;       // 點陣圖實際用到的顏色數,如果該值為零,則用到的顏色數為2的biBitCount次冪。
  unsigned short biClrImportant; // 點陣圖顯示過程,重要的顏色數;0--所有都重要
} BmpInfoHeader;                 // 40位元組
#pragma pack()

int *lcd_mp;

1、圖片顯示

int read_JPEG_file(char *filename,
                   int start_x,
                   int start_y)
{
  /* This struct contains the JPEG decompression parameters and pointers to
   * working space (which is allocated as needed by the JPEG library).
   */
  struct jpeg_decompress_struct cinfo;
  /* We use our private extension JPEG error handler.
   * Note that this struct must live as long as the main JPEG parameter
   * struct, to avoid dangling-pointer problems.
   */
  struct jpeg_error_mgr jerr;
  /* More stuff */
  FILE *infile;          /* source file */
  unsigned char *buffer; /* Output row buffer */
  int row_stride;        /* physical row width in output buffer */

  /* In this example we want to open the input file before doing anything else,
   * so that the setjmp() error recovery below can assume the file is open.
   * VERY IMPORTANT: use "b" option to fopen() if you are on a machine that
   * requires it in order to read binary files.
   */

  if ((infile = fopen(filename, "rb")) == NULL)
  {
    fprintf(stderr, "can't open %s\n", filename);
    return 0;
  }

  /* Step 1: allocate and initialize JPEG decompression object */

  /* We set up the normal JPEG error routines, then override error_exit. */
  cinfo.err = jpeg_std_error(&jerr);

  /* Now we can initialize the JPEG decompression object. */
  jpeg_create_decompress(&cinfo);

  /* Step 2: specify data source (eg, a file) */

  jpeg_stdio_src(&cinfo, infile);

  /* Step 3: read file parameters with jpeg_read_header() */

  (void)jpeg_read_header(&cinfo, TRUE);
  /* We can ignore the return value from jpeg_read_header since
   *   (a) suspension is not possible with the stdio data source, and
   *   (b) we passed TRUE to reject a tables-only JPEG file as an error.
   * See libjpeg.txt for more info.
   */

  /* Step 4: set parameters for decompression */

  /* In this example, we don't need to change any of the defaults set by
   * jpeg_read_header(), so we do nothing here.
   */

  /* Step 5: Start decompressor */

  (void)jpeg_start_decompress(&cinfo);
  /* We can ignore the return value since suspension is not possible
   * with the stdio data source.
   */

  /* We may need to do some setup of our own at this point before reading
   * the data.  After jpeg_start_decompress() we have the correct scaled
   * output image dimensions available, as well as the output colormap
   * if we asked for color quantization.
   * In this example, we need to make an output work buffer of the right size.
   */
  /* JSAMPLEs per row in output buffer */
  row_stride = cinfo.output_width * cinfo.output_components; // 計算一行的大小

  /* Make a one-row-high sample array that will go away when done with image */
  buffer = calloc(1, row_stride);

  /* Step 6: while (scan lines remain to be read) */
  /*           jpeg_read_scanlines(...); */

  /* Here we use the library's state variable cinfo.output_scanline as the
   * loop counter, so that we don't have to keep track ourselves.
   */
  int data = 0;

  while (cinfo.output_scanline < cinfo.output_height)
  {
    /* jpeg_read_scanlines expects an array of pointers to scanlines.
     * Here the array is only one element long, but you could ask for
     * more than one scanline at a time if that's more convenient.
     */
    (void)jpeg_read_scanlines(&cinfo, &buffer, 1); // 從上到下,從左到右  RGB RGB RGB RGB

    for (int i = 0; i < cinfo.output_width; ++i) // 012 345
    {
      data |= buffer[3 * i] << 16;    // R
      data |= buffer[3 * i + 1] << 8; // G
      data |= buffer[3 * i + 2];      // B

      // 把像素點寫入到LCD的指定位置
      lcd_mp[800 * start_y + start_x + 800 * (cinfo.output_scanline - 1) + i] = data;

      data = 0;
    }
  }

  /* Step 7: Finish decompression */

  (void)jpeg_finish_decompress(&cinfo);
  /* We can ignore the return value since suspension is not possible
   * with the stdio data source.
   */

  /* Step 8: Release JPEG decompression object */

  /* This is an important step since it will release a good deal of memory. */
  jpeg_destroy_decompress(&cinfo);

  /* After finish_decompress, we can close the input file.
   * Here we postpone it until after no more JPEG errors are possible,
   * so as to simplify the setjmp error logic above.  (Actually, I don't
   * think that jpeg_destroy can do an error exit, but why assume anything...)
   */
  fclose(infile);

  /* At this point you may want to check to see whether any corrupt-data
   * warnings occurred (test whether jerr.pub.num_warnings is nonzero).
   */

  /* And we're done! */
  return 1;
}

2、其他圖片壓縮到jpg圖片

write_JPEG_file(char *filename,
                int image_width,
                int image_height,
                unsigned char *image_buffer)
{
  /* This struct contains the JPEG compression parameters and pointers to
   * working space (which is allocated as needed by the JPEG library).
   * It is possible to have several such structures, representing multiple
   * compression/decompression processes, in existence at once.  We refer
   * to any one struct (and its associated working data) as a "JPEG object".
   */
  /* Step 1*/
  struct jpeg_compress_struct cinfo;

  struct jpeg_error_mgr jerr;
  /* More stuff */
  FILE *outfile;                 // 文檔指針
  unsigned char *row_pointer[1]; // 行指針
  int row_stride;

  cinfo.err = jpeg_std_error(&jerr);

  jpeg_create_compress(&cinfo);

  /* Step 2: specify data destination (eg, a file) */

  if ((outfile = fopen(filename, "wb")) == NULL)
  {
    fprintf(stderr, "can't open %s\n", filename);
    exit(1);
  }
  jpeg_stdio_dest(&cinfo, outfile);

  /* Step 3: set parameters for compression */

  // 寬高
  cinfo.image_width = image_width; /* image width and height, in pixels */
  cinfo.image_height = image_height;
  // 色深
  cinfo.input_components = 3; /* # of color components per pixel */
  // 頭文件內東西
  cinfo.in_color_space = JCS_RGB; /* colorspace of input image */

  jpeg_set_defaults(&cinfo);

  /* Step 4: Start compressor */

  /* TRUE ensures that we will write a complete interchange-JPEG file.
   * Pass TRUE unless you are very sure of what you're doing.
   */
  jpeg_start_compress(&cinfo, TRUE);

  /* Step 5: while (scan lines remain to be written) */
  /*           jpeg_write_scanlines(...); */

  /* Here we use the library's state variable cinfo.next_scanline as the
   * loop counter, so that we don't have to keep track ourselves.
   * To keep things simple, we pass one scanline per call; you can pass
   * more if you wish, though.
   */
  row_stride = image_width * 3; /* JSAMPLEs per row in image_buffer */

  while (cinfo.next_scanline < cinfo.image_height)
  {
    /* jpeg_write_scanlines expects an array of pointers to scanlines.
     * Here the array is only one element long, but you could pass
     * more than one scanline at a time if that's more convenient.
     */
    row_pointer[0] = &image_buffer[cinfo.next_scanline * row_stride];
    (void)jpeg_write_scanlines(&cinfo, row_pointer, 1);
  }

  /* Step 6: Finish compression */

  jpeg_finish_compress(&cinfo);
  /* After finish_compress, we can close the output file. */
  fclose(outfile);

  /* Step 7: release JPEG compression object */

  /* This is an important step since it will release a good deal of memory. */
  jpeg_destroy_compress(&cinfo);

  /* And we're done! */
}

3、主函數及驗證程式

int main(int argc,
         char const *argv[])
{
  // 1.打開LCD   open
  int lcd_fd = open("/dev/fb0", O_RDWR);

  // 2.對LCD進行記憶體映射  mmap
  lcd_mp = (int *)mmap(NULL,
                       800 * 480 * 4,
                       PROT_READ | PROT_WRITE, MAP_SHARED,
                       lcd_fd, 0);

  // 1.打開待顯示的BMP圖像  fopen
  FILE *bmp_fp1 = fopen("jojo.bmp", "rb");
  BmpFileHeader headfile1;
  BmpInfoHeader headerinfo1;
  fseek(bmp_fp1, 0, SEEK_SET);
  fread(&headfile1, 1, 14, bmp_fp1);   // 讀取14位元組
  fread(&headerinfo1, 1, 40, bmp_fp1); // 讀取40位元組

  int H1 = headerinfo1.biHeight *
           headerinfo1.biWidth *
           headerinfo1.biBitCount / 8;
  // 3.讀取BMP圖*片的顏色分量  800*480*3
  char bmp_buf1[800 * 480 * 3];
  fread(bmp_buf1, 1, 800 * 480 * 3, bmp_fp1);
  write_JPEG_file("1.jpg",
                  800,
                  480,
                  bmp_buf1);

  // 3.顯示一張jpg
  read_JPEG_file("1.jpg", 0, 0);

  return 0;
}

輸出結果

原圖

image

輸出圖

image


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • Qwerty Learner —— 一款為鍵盤工作者設計的單詞記憶與英語肌肉記憶鍛煉軟體,主要服務於以英語作為主要工作語言的鍵盤工作者。 ...
  • ​ UIOTOS可以瞭解下,uiotos.net,通過連線來代替腳本邏輯開發,複雜的交互界面,通過頁面嵌套輕鬆解決,是個很新穎的思路,前端零代碼! 藍圖連線尤其是獨創的頁面嵌套和屬性繼承技術,好家伙相當於把vue的組件化、增量式面向對象開發,直接搬到前端拖拽工具上,無代碼編程了。 總的來說,這上面的 ...
  • 這裡給大家分享我在網上總結出來的一些知識,希望對大家有所幫助 一、背景 在前端JSON.stringfy是我們常用的一個方法,可以將一個對象序列化。 例如將如下對象序列化 const person = { name: 'kalory', age:18} JSON.stringfy(person) / ...
  • 引言 有這麼一個需求:列表頁進入詳情頁後,切換回列表頁,需要對列表頁進行緩存,如果從首頁進入列表頁,就要重新載入列表頁。 對於這個需求,我的第一個想法就是使用keep-alive來緩存列表頁,列表和詳情頁切換時,列表頁會被緩存;從首頁進入列表頁時,就重置列表頁數據並重新獲取新數據來達到列表頁重新載入 ...
  • 在當今的信息時代,無線通信技術的發展日新月異,為我們的工作和生活帶來了極大的便利。其中,無線通信模塊通過TCP/IP協議向PC端傳送數據已經成為了一種常見的通信方式。本文將詳細介紹這一過程的主要步驟和涉及的關鍵技術,並以WIFI模塊為例,探討如何在QT平臺下實現數據的無線傳輸。 一、無線通信模塊與T ...
  • 前言 MVC,MVP,MVVM 屬於 GUI 軟體設計,它們都強調將軟體的視圖顯示與業務邏輯進行分離,將軟體拆分為三個部分,分別負責數據操作、視圖邏輯、業務邏輯。 業務邏輯指的是任何與數據操作、視圖操作的定義與實現無關的功能。 具體來說,業務邏輯中不應該出現如何操作視圖或如何操作數據的細節。 對於這 ...
  • X-Frame-Options 是一個HTTP響應頭,用於控制網頁是否可以嵌套在 <frame>, <iframe>, <embed> 或者 <applet> 中。通過設置 X-Frame-Options 頭部,網站管理員可以防止網頁被嵌套到其他網站的框架中,從而有效防範點擊劫持等安全風險。下麵是關 ...
  • nginx 系列 Nginx-01-聊一聊 nginx Nginx-01-Nginx 是什麼 Nginx-02-為什麼使用 Nginx Nginx-02-Nginx Ubuntu 安裝 + windows10 + WSL ubuntu 安裝 nginx 實戰筆記 Nginx-02-基本使用 Ngin ...
一周排行
    -Advertisement-
    Play Games
  • 前言 微服務架構已經成為搭建高效、可擴展系統的關鍵技術之一,然而,現有許多微服務框架往往過於複雜,使得我們普通開發者難以快速上手並體驗到微服務帶了的便利。為瞭解決這一問題,於是作者精心打造了一款最接地氣的 .NET 微服務框架,幫助我們輕鬆構建和管理微服務應用。 本框架不僅支持 Consul 服務註 ...
  • 先看一下效果吧: 如果不會寫動畫或者懶得寫動畫,就直接交給Blend來做吧; 其實Blend操作起來很簡單,有點類似於在操作PS,我們只需要設置關鍵幀,滑鼠點來點去就可以了,Blend會自動幫我們生成我們想要的動畫效果. 第一步:要創建一個空的WPF項目 第二步:右鍵我們的項目,在最下方有一個,在B ...
  • Prism:框架介紹與安裝 什麼是Prism? Prism是一個用於在 WPF、Xamarin Form、Uno 平臺和 WinUI 中構建鬆散耦合、可維護和可測試的 XAML 應用程式框架 Github https://github.com/PrismLibrary/Prism NuGet htt ...
  • 在WPF中,屏幕上的所有內容,都是通過畫筆(Brush)畫上去的。如按鈕的背景色,邊框,文本框的前景和形狀填充。藉助畫筆,可以繪製頁面上的所有UI對象。不同畫筆具有不同類型的輸出( 如:某些畫筆使用純色繪製區域,其他畫筆使用漸變、圖案、圖像或繪圖)。 ...
  • 前言 嗨,大家好!推薦一個基於 .NET 8 的高併發微服務電商系統,涵蓋了商品、訂單、會員、服務、財務等50多種實用功能。 項目不僅使用了 .NET 8 的最新特性,還集成了AutoFac、DotLiquid、HangFire、Nlog、Jwt、LayUIAdmin、SqlSugar、MySQL、 ...
  • 本文主要介紹攝像頭(相機)如何採集數據,用於類似攝像頭本地顯示軟體,以及流媒體數據傳輸場景如傳屏、視訊會議等。 攝像頭採集有多種方案,如AForge.NET、WPFMediaKit、OpenCvSharp、EmguCv、DirectShow.NET、MediaCaptre(UWP),網上一些文章以及 ...
  • 前言 Seal-Report 是一款.NET 開源報表工具,擁有 1.4K Star。它提供了一個完整的框架,使用 C# 編寫,最新的版本採用的是 .NET 8.0 。 它能夠高效地從各種資料庫或 NoSQL 數據源生成日常報表,並支持執行複雜的報表任務。 其簡單易用的安裝過程和直觀的設計界面,我們 ...
  • 背景需求: 系統需要對接到XXX官方的API,但因此官方對接以及管理都十分嚴格。而本人部門的系統中包含諸多子系統,系統間為了穩定,程式間多數固定Token+特殊驗證進行調用,且後期還要提供給其他兄弟部門系統共同調用。 原則上:每套系統都必須單獨接入到官方,但官方的接入複雜,還要官方指定機構認證的證書 ...
  • 本文介紹下電腦設備關機的情況下如何通過網路喚醒設備,之前電源S狀態 電腦Power電源狀態- 唐宋元明清2188 - 博客園 (cnblogs.com) 有介紹過遠程喚醒設備,後面這倆天瞭解多了點所以單獨加個隨筆 設備關機的情況下,使用網路喚醒的前提條件: 1. 被喚醒設備需要支持這WakeOnL ...
  • 前言 大家好,推薦一個.NET 8.0 為核心,結合前端 Vue 框架,實現了前後端完全分離的設計理念。它不僅提供了強大的基礎功能支持,如許可權管理、代碼生成器等,還通過採用主流技術和最佳實踐,顯著降低了開發難度,加快了項目交付速度。 如果你需要一個高效的開發解決方案,本框架能幫助大家輕鬆應對挑戰,實 ...