C语言 如何正确保存rgb颜色到ppm文件?

m4pnthwp  于 11个月前  发布在  其他
关注(0)|答案(2)|浏览(128)

我试图使简单的c程序保存rgb颜色ppm文件usinf 1D数组。这是教育项目

#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;
}

字符串
程序工作,但不是简单的单色红色梯度我得到黄色一.

我做错了什么?

nkcskrwz

nkcskrwz1#

将你的程序最小化,并改变redblu的计算方式,以显示你想要的效果(好吧,再多一点,另一个轴上有一个黑色到蓝色的渐变):

#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编写代码是绝对好的。

7cwmlq89

7cwmlq892#

我发现了我的错误。我想要单色1d颜色梯度,但我已经将其他颜色通道设置为1而不是0。

相关问题