鱼C论坛

 找回密码
 立即注册
查看: 3545|回复: 3

图片转二维数组

[复制链接]
发表于 2021-5-30 18:43:25 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
求大神指导,图片怎么转换成二维数组呀?在网上看了很多也没弄成
可不可以有代码参考一下
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-5-31 17:55:20 | 显示全部楼层
一般是用专门的软件转的哈
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-5-31 22:47:03 From FishC Mobile | 显示全部楼层
opencv CvMat
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-6-2 15:59:34 | 显示全部楼层
参考
https://www.cnblogs.com/wainiwann/p/7086844.html

bmp.h
  1. #ifndef _READ_BMP_H_
  2. #define _READ_BMP_H_

  3. #include <stddef.h>
  4. #include <stdint.h>

  5. typedef struct {
  6.     size_t width, height;
  7.     uint8_t *data;
  8. } *bmp_t;

  9. bmp_t bmp_init(const char *filename);
  10. void bmp_deinit(bmp_t bmp);
  11. uint32_t bmp_get_pixel(const bmp_t bmp, size_t x, size_t y);

  12. #endif
复制代码


bmp.c
  1. #include "bmp.h"
  2. #include <stdlib.h>
  3. #include <fcntl.h>
  4. #include <unistd.h>
  5. #include <sys/stat.h>
  6. #include <sys/mman.h>

  7. #define BI_RGB 0

  8. typedef struct tagBITMAPFILEHEADER {
  9.     uint16_t bfType;
  10.     uint32_t bfSize;
  11.     uint16_t bfReserved1;
  12.     uint16_t bfReserved2;
  13.     uint32_t bfOffBits;
  14. } __attribute__((packed)) BITMAPFILEHEADER,*LPBITMAPFILEHEADER,*PBITMAPFILEHEADER;

  15. typedef struct tagBITMAPINFOHEADER {
  16.     uint32_t biSize;
  17.     uint32_t biWidth;
  18.     uint32_t biHeight;
  19.     uint16_t biPlanes;
  20.     uint16_t biBitCount;
  21.     uint32_t biCompression;
  22.     uint32_t biSizeImage;
  23.     uint32_t biXPelsPerMeter;
  24.     uint32_t biYPelsPerMeter;
  25.     uint32_t biClrUsed;
  26.     uint32_t biClrImportant;
  27. } __attribute__((packed)) BITMAPINFOHEADER,*LPBITMAPINFOHEADER,*PBITMAPINFOHEADER;

  28. static bmp_t parse_bmp(uint8_t *base) {
  29.     PBITMAPFILEHEADER bfh = (PBITMAPFILEHEADER)base;
  30.     PBITMAPINFOHEADER bih = (PBITMAPINFOHEADER)(bfh + 1);
  31.     uint8_t *bmp_data = base + bfh->bfOffBits;
  32.     if(!(bih->biBitCount == 24 && bih->biCompression == BI_RGB)) return NULL;
  33.     bmp_t bmp = malloc(sizeof(*bmp));
  34.     if(!bmp) return NULL;
  35.     bmp->width = bih->biWidth;
  36.     bmp->height = bih->biHeight;
  37.     bmp->data = malloc(bmp->width * bmp->height * 4);
  38.     if(!bmp->data) {
  39.         free(bmp); return NULL;
  40.     }
  41.     size_t row_size = bmp->width * 3;
  42.     row_size = row_size & 0x03 ? (row_size & ~0x03) + 4 : row_size;
  43.     uint32_t *dest_pixel = (uint32_t *)bmp->data;
  44.     for(size_t h = 0; h < bmp->height; ++h) {
  45.         uint8_t *row_base = bmp_data + (bmp->height - h - 1) * row_size;
  46.         for(size_t w = 0; w < bmp->width; ++w) {
  47.             uint8_t *rgb = row_base + w * 3;
  48.             uint32_t src_pixel = rgb[0] | rgb[1] << 8 | rgb[2] << 16;
  49.             dest_pixel[h * bmp->width + w] = src_pixel;
  50.         }
  51.     }
  52.     return bmp;
  53. }

  54. bmp_t bmp_init(const char *filename) {
  55.     if(!filename) return NULL;
  56.     int fd = open(filename, O_RDONLY);
  57.     if(fd < 0) return NULL;
  58.     struct stat stat_buf;
  59.     fstat(fd, &stat_buf);
  60.     uint8_t *bmp_file = mmap(NULL, stat_buf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
  61.     if(bmp_file == MAP_FAILED) {
  62.         close(fd); return NULL;
  63.     }
  64.     bmp_t bmp = parse_bmp(bmp_file);
  65.     munmap(bmp_file, stat_buf.st_size);
  66.     close(fd);
  67.     return bmp;
  68. }

  69. void bmp_deinit(bmp_t bmp) {
  70.     if(bmp) free(bmp->data);
  71.     free(bmp);
  72. }

  73. uint32_t bmp_get_pixel(const bmp_t bmp, size_t x, size_t y) {
  74.     if(!(x < bmp->width && y < bmp->height)) return 0;
  75.     return ((uint32_t *)bmp->data)[y * bmp->width + x];
  76. }
复制代码


main.c
  1. #include "bmp.h"
  2. #include <SDL.h>

  3. int main(void) {
  4.     bmp_t bmp = bmp_init("pic.bmp");
  5.     SDL_Init(SDL_INIT_EVERYTHING);
  6.     SDL_Window *win = SDL_CreateWindow("hello world", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, \
  7.             bmp->width, bmp->height, SDL_WINDOW_SHOWN);
  8.     SDL_Renderer *ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
  9.     for(size_t h = 0; h < bmp->height; ++h) {
  10.         for(size_t w = 0; w < bmp->width; ++w) {
  11.             uint32_t pixel = bmp_get_pixel(bmp, w, h);
  12.             uint8_t a = (pixel >> 24) & 0xff;
  13.             uint8_t r = (pixel >> 16) & 0xff;
  14.             uint8_t g = (pixel >> 8) & 0xff;
  15.             uint8_t b = (pixel >> 0) & 0xff;
  16.             SDL_SetRenderDrawColor(ren, r, g, b, a);
  17.             SDL_RenderDrawPoint(ren, w, h);
  18.         }
  19.     }
  20.     SDL_RenderPresent(ren);
  21.     SDL_Delay(3000);
  22.     SDL_DestroyRenderer(ren);
  23.     SDL_DestroyWindow(win);
  24.     SDL_Quit();
  25.     bmp_deinit(bmp);
  26.     return 0;
  27. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-28 01:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表