如何正确保存RGB颜色到ppm文件?

问题描述 投票:0回答:1

我尝试制作简单的 C 程序,使用 inf 1D 数组将 RGB 颜色保存到 ppm 文件。这是教育项目

#include <complex.h>
#include <math.h>
#include <omp.h> //OpenM
#include <stdio.h>
#include <stdlib.h>

/*
fork of
mandelbrot-book how to write a book about the Mandelbrot set by Claude
Heiland-Alle https://code.mathr.co.uk/mandelbrot-book/blob/HEAD:/book/
https://stackoverflow.com/questions/77135883/why-do-my-functions-not-work-in-parallel


gcc c.c -lm -Wall -fopenmp

./a.out > rgb.ppm   // P6 = binary Portable PixMap see
https://en.wikipedia.org/wiki/Netpbm#File_formats

convert rgb.ppm rgb.png
*/

double aspect_ratio = 16.0 / 9.0; // = w/h
const int h = 200;
int w; // = h * aspect_ratio;

const double r = 2.0; // plane radius
const double px = r / (h / 2);

double width;
double xMin;

// https://stackoverflow.com/questions/2594913/getting-the-fractional-part-of-a-float-without-using-modf
double frac(double d) { return d - (int)d; }

// rgb in [0,1] range
void giveRGBColor(const complex double z, double *r, double *g, double *b) {

  *r = frac(abs((creal(z) - xMin)) / width); //
  *g = 1.0;
  *b = 1.0;
}

int main() {
  w = h * aspect_ratio; // The aspect ratio of an image is the ratio of its
                        // width to its height, and is expressed with two
                        // numbers separated by a colon, such as 16:9,
  unsigned char *img = malloc(3 * w * h); // image
  width = r * 2.0 * aspect_ratio;
  double height = r * 2.0;
  xMin = (0 + 0.5 - w / 2) / (h / 2) * r;

  fprintf(stderr, "image width = %.2f in world coordinate\n", width);
  fprintf(stderr, "image height = %.2f in world coordinate\n", height);
  fprintf(stderr, "image aspect ratio = width:height = %d:%d = %f = %f = %f\n",
          w, h, w / (1.0 * h), width / (1.0 * height), aspect_ratio);

#pragma omp parallel for schedule(dynamic)
  for (int j = 0; j < h; ++j) {
    double y = (h / 2 - (j + 0.5)) / (h / 2) * r;
    for (int i = 0; i < w; ++i) {
      double x = (i + 0.5 - w / 2) / (h / 2) * r; // for q=2 add -0.5
      double _Complex z = x + I * y;

      // color

      double red, grn, blu;

      giveRGBColor(z, &red, &grn, &blu); // compute rgb

      // convert to [0,255] integer and save rgb color to array
      img[3 * (j * w + i) + 0] = (int)(255 * red);
      img[3 * (j * w + i) + 1] = (int)(255 * grn);
      img[3 * (j * w + i) + 2] = (int)(255 * blu);
    }
  }

  //
  printf("P6\n%d %d\n255\n", w, h);
  fwrite(img, 3 * w * h, 1, stdout);
  free(img);

  return 0;
}

程序可以工作,但我得到的是yellow,而不是简单的单色red渐变。 我做错了什么?

c rgb ppm
1个回答
0
投票

将您的程序调整到实用的最低限度,并更改

red
blu
的计算方式以显示您想要的效果(好吧,还有一点,在另一个轴上有一个从黑到蓝的渐变) :

#include <math.h>
#include <stdio.h>
#include <stdlib.h>

int main() {
  double aspect_ratio = 16.0 / 9.0;
  int h = 200;
  int w = h * aspect_ratio;
  unsigned char *img = malloc(3 * w * h); // image
  for (int j = 0; j < h; ++j) {
    for (int i = 0; i < w; ++i) {
      double red = i / (double)w, grn = 0, blu = j / (double)h;

      // convert to [0,255] integer and save rgb color to array
      img[3 * (j * w + i) + 0] = (int)(255 * red);
      img[3 * (j * w + i) + 1] = (int)(255 * grn);
      img[3 * (j * w + i) + 2] = (int)(255 * blu);
    }
  }

  printf("P6\n%d %d\n255\n", w, h);
  fwrite(img, 3 * w * h, 1, stdout);
  free(img);

  return 0;
}

这会产生这个图像:

换句话说,我们可以告诉你,你的PPM编写代码绝对没问题。

© www.soinside.com 2019 - 2024. All rights reserved.