【问题标题】:Change RGB values of a JPEG or PNG Image from C using jpeglib.h使用 jpeglib.h 从 C 更改 JPEG 或 PNG 图像的 RGB 值
【发布时间】:2021-05-05 05:45:53
【问题描述】:

首先,我从未在 C 语言上使用过图像,因此我正在寻找一种方法,可以更改 JPEG 或 PNG 图像的 RGB 值以获得红色、蓝色和绿色的结果图像,只需就像滤色器一样,将 R 值更改为 0,将 G 值更改为 0,将 B 值更改为 255,反之亦然,但我需要在 C 中进行,我使用 jpeglib.h 和参考代码作为指导,因为我'以前从未使用过此 API,我可以加载图像并将其写入输出图像,但是我还没有找到改变 RGB 值的方法,我该怎么做?

我在 UNIX 环境下工作,由于学术原因,我正在尝试学习如何将任何 jpeg 图像处理为 ppm 格式。

我的代码如下:

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <syslog.h>
#include <sys/stat.h>
#include <jpeglib.h>

int main (int argc, char *argv[]) {
  int rc, i, j;
  char *syslog_prefix = (char*) malloc(1024);
  sprintf(syslog_prefix, "%s", argv[0]);
  openlog(syslog_prefix, LOG_PERROR | LOG_PID, LOG_USER);

  if (argc != 2) {
    fprintf(stderr, "usage: %s filename.jpg\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  struct stat file_info;
  unsigned long jpg_size;
  unsigned char *jpg_buffer;
  struct jpeg_decompress_struct cinfo;
  struct jpeg_error_mgr jerr;
  unsigned long bmp_size;
  unsigned char *bmp_buffer;
  int row_stride, width, height, pixel_size;

  rc = stat(argv[1], &file_info);
  if (rc) {
    syslog(LOG_ERR, "FAILED to stat source jpg");
    exit(EXIT_FAILURE);
  }

  jpg_size = file_info.st_size;
  jpg_buffer = (unsigned char*) malloc(jpg_size + 100);

  int fd = open(argv[1], O_RDONLY);
  i = 0;
  while (i < jpg_size) {
    rc = read(fd, jpg_buffer + i, jpg_size - i);
    i += rc;
  }
  close(fd);

  cinfo.err = jpeg_std_error(&jerr);  
  jpeg_create_decompress(&cinfo);
  jpeg_mem_src(&cinfo, jpg_buffer, jpg_size);

  rc = jpeg_read_header(&cinfo, TRUE);

  if (rc != 1) {
    syslog(LOG_ERR, "File does not seem to be a normal JPEG");
    exit(EXIT_FAILURE);
  }

  jpeg_start_decompress(&cinfo);

  width = cinfo.output_width;
  height = cinfo.output_height;
  pixel_size = cinfo.output_components;

  bmp_size = width * height * pixel_size;
  bmp_buffer = (unsigned char*) malloc(bmp_size);

  row_stride = width * pixel_size;

  while (cinfo.output_scanline < cinfo.output_height) {
    unsigned char *buffer_array[1];
    buffer_array[0] = bmp_buffer + \
               (cinfo.output_scanline) * row_stride;

    jpeg_read_scanlines(&cinfo, buffer_array, 1);

  }

  jpeg_finish_decompress(&cinfo);

  jpeg_destroy_decompress(&cinfo);

  free(jpg_buffer);

  fd = open("output.ppm", O_CREAT | O_WRONLY, 0666);
  char buf[1024];

  rc = sprintf(buf, "P6 %d %d 255\n", width, height);
  write(fd, buf, rc);
  write(fd, bmp_buffer, bmp_size);

  close(fd);
  free(bmp_buffer);

  return EXIT_SUCCESS;
}

【问题讨论】:

  • 请出示读入一张图片并将其原封不动地写入三色通道图片的程序。 IE。显示您的代码框架上下文,包括输入和输出。然后,您可能会在 itnerpreting 图片格式方面获得帮助。事实上,问题是广泛的,或者要求教程或工具;所有三个读数都是题外话。
  • 请不要发布你一半的代码——你可能需要一些头文件#included 所以也显示那部分。还要展示你如何编译它并说出你在什么平台上 - Windows? Linux?您的目标是学习一些图像处理编程,还是您正在尝试做一些具体的事情?为什么你认为你要输出一个 PPM 文件?

标签: c image rgb


【解决方案1】:

我认为更好的方法是通过解压缩图像更改颜色,您想更改并再次保存它,我做了第一部分,但我不知道如何再次将数据放入新图像中,在您的情况下,在下一步中,您需要转换为 ppm。这是保存格式正确的图像的路径,但我不知道如何以这种格式放置图像。 https://www.huinfinito.com.br/motores/1438-valvula-solenoide-para-agua-12v-180-x-.html,如果您发现了如何做到这一点,请回复此帖子。要更改 rgb 值,您可以直接从它们的地址更改,因为它是整数的结合。

if(i2==3||(sair==1&&i2==2)){         
 contCorSelecionada++;
 int i;
 for(i=0;i<3;i++){
 *datamenos3=corTroca.limiteinferior[i];
 datamenos3++;`}               

我这样做了,但陷入了我之前提到的问题。

【讨论】:

    猜你喜欢
    • 2019-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多