From 41f24e88e901c5ec855cdf381efe34094b729695 Mon Sep 17 00:00:00 2001 From: Yingjie Wang Date: Sun, 7 Dec 2025 12:14:14 -0500 Subject: [PATCH] update: change buffer to double* --- src/common.h | 4 ++++ src/render.c | 14 ++++++++++---- src/render.h | 2 +- src/test_png.c | 6 +++--- src/write_png.c | 21 +++++++++++++-------- src/write_png.h | 4 ++-- 6 files changed, 33 insertions(+), 18 deletions(-) diff --git a/src/common.h b/src/common.h index b1b1a7d..3fa0d4a 100644 --- a/src/common.h +++ b/src/common.h @@ -1,3 +1,5 @@ +#ifndef __COMMON_H +#define __COMMON_H #include #include #include @@ -11,6 +13,7 @@ #define bmin (1.5*sqrt(3.0)+5e-10) #define cotpsi_max (sqrt(R0*R0/(bmin*bmin*f(R0)) - 1)) #define THETAERROR 100000 +#define color_index(i,j,c) (j*W*3+i*3+c) typedef struct { int W; @@ -19,3 +22,4 @@ typedef struct { int (*angle_to_pixel)(double *, double *); } System; +#endif // !__COMMON_H diff --git a/src/render.c b/src/render.c index 663798d..a19cb26 100644 --- a/src/render.c +++ b/src/render.c @@ -3,6 +3,7 @@ #include #include #include +#include #include "render.h" typedef struct { @@ -71,6 +72,7 @@ int MC_pixel_render(const System *system, int i, int j, const Spline_data spline gsl_monte_function F; F.params = &integrand_params; F.dim = 2; + printf("i=%d, j=%d, rgb=( ", i, j); for (int c = 0; c < 3; c++) { switch (c) { case 0: @@ -85,8 +87,10 @@ int MC_pixel_render(const System *system, int i, int j, const Spline_data spline } gsl_monte_miser_init(miser_state); gsl_monte_miser_integrate(&F, xl, xu, 2, 100, r, miser_state, &color, &err); + printf("%g +- %g ", color, err); rgb[c] = color; } + printf(")\n"); return 0; } @@ -95,7 +99,7 @@ int pixel_render(const System *system, int i, int j, const Spline_data spline_da return MC_pixel_render(system, i, j, spline_data, rgb, pixel_render_max, pixel_render_err, r); } -int render(System *system, double (*buffer)[3], int pixel_render_max, double pixel_render_err, double chi_rela_err) { +int render(System *system, double *buffer, int pixel_render_max, double pixel_render_err, double chi_rela_err) { double h = (system->H)*(system->w)/(system->W); double tanpsi2 = (system->w)*(system->w)+h*h; double cotpsi2 = 1/tanpsi2; @@ -106,6 +110,8 @@ int render(System *system, double (*buffer)[3], int pixel_render_max, double pix double *x=NULL; double *y = NULL; int size; + int W = system->W; + int H = system->H; init(rmax, chi_rela_err, &size, &x, &y); @@ -114,9 +120,9 @@ int render(System *system, double (*buffer)[3], int pixel_render_max, double pix Spline_data spline_data = {spline, acc}; gsl_spline_init(spline, x, y, size); - for(int j = 0; j < system->H; j++) { - for (int i = 0; i < system->W; i++) { - pixel_render(system, i, j, spline_data, buffer[j*(system->W) + i], pixel_render_max, pixel_render_err, r); + for(int j = 0; j < H; j++) { + for (int i = 0; i < W; i++) { + pixel_render(system, i, j, spline_data, buffer+color_index(i,j,0), pixel_render_max, pixel_render_err, r); } } diff --git a/src/render.h b/src/render.h index 5f5dfe9..8c21653 100644 --- a/src/render.h +++ b/src/render.h @@ -1,4 +1,4 @@ #include "common.h" #include "init.h" -int render(System *system, double (*buffer)[3], int pixel_render_max, double pixel_render_err, double chi_rela_err); +int render(System *system, double *buffer, int pixel_render_max, double pixel_render_err, double chi_rela_err); diff --git a/src/test_png.c b/src/test_png.c index 94f386b..a713fdf 100644 --- a/src/test_png.c +++ b/src/test_png.c @@ -10,12 +10,12 @@ int angle_to_pixel_white(double *angle, double *rgb) { } int main(){ - int W = 1; - int H = 1; + int W = 9; + int H = 9; double w = 1; System system = {W, H, w, angle_to_pixel_white}; - double buffer[W*H][3]; + double buffer[W*H*3]; render(&system, buffer, 10000, 1.0/256, 1e-1); write_png("test_white.png", buffer, W, H); diff --git a/src/write_png.c b/src/write_png.c index d45a903..aacc153 100644 --- a/src/write_png.c +++ b/src/write_png.c @@ -1,7 +1,9 @@ #include #include #include +#include #include "write_png.h" +#include "common.h" static inline double linear_to_srgb(double x){ if (x <= 0.0) return 0.0; @@ -10,40 +12,43 @@ static inline double linear_to_srgb(double x){ return 1.055 * pow(x, 1.0/2.4) - 0.055; } -int buffer_normalize_srgb(double (*buffer)[3], int W, int H) { +int buffer_normalize_srgb(double *buffer, int W, int H) { double max = 0; for (int j = 0; j < H; j++) { for (int i = 0; i < W; i++) { for (int c = 0; c < 3; c++) { - if (max < buffer[j*W + i][c]) max = buffer[j*W + i][c]; + if (max < buffer[color_index(i, j, c)]) max = buffer[color_index(i, j, c)]; } } } + printf("max in the buffer: %g\n", max); if (max == 0) return 1; for (int j = 0; j < H; j++) { for (int i = 0; i < W; i++) { for (int c = 0; c < 3; c++) { - buffer[j*W + i][c] /= max; - buffer[j*W+i][c] = linear_to_srgb(buffer[j*W+i][c]); + buffer[color_index(i, j, c)] /= max; + buffer[color_index(i, j, c)] = linear_to_srgb(buffer[color_index(i, j, c)]); } } } return 0; } -int write_png(char *filename, double (*buffer)[3], int W, int H) { +int write_png(char *filename, double *buffer, int W, int H) { buffer_normalize_srgb(buffer, W, H); size_t bufsize = W*H*3; - uint8_t *img = (uint8_t *)malloc(bufsize); + printf("bufsize: %d\n", bufsize); + uint8_t *img = (uint8_t *)malloc(bufsize*sizeof(uint8_t)); if (!img) {return 1;} for (int j = 0; j < H; j++) { for (int i = 0; i < W; i++) { for (int c = 0; c < 3; c++) { - int v = (int)lrint(buffer[j*W*i][c]); + int v = (int)lrint(buffer[color_index(i,j,c)]*255); if (v < 0) v = 0; if (v > 255) v = 255; - img[j*W*3+i*3+c] = (uint8_t) v; + img[color_index(i, j, c)] = (uint8_t) v; + printf("i: %d, j: %d, c:%d, buffer=%g, v=%d\n", i,j,c,buffer[color_index(i, j, c)],v); } } } diff --git a/src/write_png.h b/src/write_png.h index f40d4a1..b10c110 100644 --- a/src/write_png.h +++ b/src/write_png.h @@ -1,3 +1,3 @@ +#include "common.h" - -int write_png(char *filename, double (*buffer)[3], int W, int H); +int write_png(char *filename, double *buffer, int W, int H);