update: change buffer to double*
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
|
#ifndef __COMMON_H
|
||||||
|
#define __COMMON_H
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <gsl/gsl_errno.h>
|
#include <gsl/gsl_errno.h>
|
||||||
@@ -11,6 +13,7 @@
|
|||||||
#define bmin (1.5*sqrt(3.0)+5e-10)
|
#define bmin (1.5*sqrt(3.0)+5e-10)
|
||||||
#define cotpsi_max (sqrt(R0*R0/(bmin*bmin*f(R0)) - 1))
|
#define cotpsi_max (sqrt(R0*R0/(bmin*bmin*f(R0)) - 1))
|
||||||
#define THETAERROR 100000
|
#define THETAERROR 100000
|
||||||
|
#define color_index(i,j,c) (j*W*3+i*3+c)
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int W;
|
int W;
|
||||||
@@ -19,3 +22,4 @@ typedef struct {
|
|||||||
int (*angle_to_pixel)(double *, double *);
|
int (*angle_to_pixel)(double *, double *);
|
||||||
} System;
|
} System;
|
||||||
|
|
||||||
|
#endif // !__COMMON_H
|
||||||
|
|||||||
14
src/render.c
14
src/render.c
@@ -3,6 +3,7 @@
|
|||||||
#include <gsl/gsl_rng.h>
|
#include <gsl/gsl_rng.h>
|
||||||
#include <gsl/gsl_spline.h>
|
#include <gsl/gsl_spline.h>
|
||||||
#include <gsl/gsl_monte_miser.h>
|
#include <gsl/gsl_monte_miser.h>
|
||||||
|
#include <stdio.h>
|
||||||
#include "render.h"
|
#include "render.h"
|
||||||
|
|
||||||
typedef struct {
|
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;
|
gsl_monte_function F;
|
||||||
F.params = &integrand_params;
|
F.params = &integrand_params;
|
||||||
F.dim = 2;
|
F.dim = 2;
|
||||||
|
printf("i=%d, j=%d, rgb=( ", i, j);
|
||||||
for (int c = 0; c < 3; c++) {
|
for (int c = 0; c < 3; c++) {
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case 0:
|
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_init(miser_state);
|
||||||
gsl_monte_miser_integrate(&F, xl, xu, 2, 100, r, miser_state, &color, &err);
|
gsl_monte_miser_integrate(&F, xl, xu, 2, 100, r, miser_state, &color, &err);
|
||||||
|
printf("%g +- %g ", color, err);
|
||||||
rgb[c] = color;
|
rgb[c] = color;
|
||||||
}
|
}
|
||||||
|
printf(")\n");
|
||||||
|
|
||||||
return 0;
|
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);
|
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 h = (system->H)*(system->w)/(system->W);
|
||||||
double tanpsi2 = (system->w)*(system->w)+h*h;
|
double tanpsi2 = (system->w)*(system->w)+h*h;
|
||||||
double cotpsi2 = 1/tanpsi2;
|
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 *x=NULL;
|
||||||
double *y = NULL;
|
double *y = NULL;
|
||||||
int size;
|
int size;
|
||||||
|
int W = system->W;
|
||||||
|
int H = system->H;
|
||||||
|
|
||||||
init(rmax, chi_rela_err, &size, &x, &y);
|
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};
|
Spline_data spline_data = {spline, acc};
|
||||||
gsl_spline_init(spline, x, y, size);
|
gsl_spline_init(spline, x, y, size);
|
||||||
|
|
||||||
for(int j = 0; j < system->H; j++) {
|
for(int j = 0; j < H; j++) {
|
||||||
for (int i = 0; i < system->W; i++) {
|
for (int i = 0; i < W; i++) {
|
||||||
pixel_render(system, i, j, spline_data, buffer[j*(system->W) + i], pixel_render_max, pixel_render_err, r);
|
pixel_render(system, i, j, spline_data, buffer+color_index(i,j,0), pixel_render_max, pixel_render_err, r);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "init.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);
|
||||||
|
|||||||
@@ -10,12 +10,12 @@ int angle_to_pixel_white(double *angle, double *rgb) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main(){
|
int main(){
|
||||||
int W = 1;
|
int W = 9;
|
||||||
int H = 1;
|
int H = 9;
|
||||||
double w = 1;
|
double w = 1;
|
||||||
System system = {W, H, w, angle_to_pixel_white};
|
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);
|
render(&system, buffer, 10000, 1.0/256, 1e-1);
|
||||||
write_png("test_white.png", buffer, W, H);
|
write_png("test_white.png", buffer, W, H);
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
#include <spng.h>
|
#include <spng.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
#include "write_png.h"
|
#include "write_png.h"
|
||||||
|
#include "common.h"
|
||||||
|
|
||||||
static inline double linear_to_srgb(double x){
|
static inline double linear_to_srgb(double x){
|
||||||
if (x <= 0.0) return 0.0;
|
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;
|
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;
|
double max = 0;
|
||||||
for (int j = 0; j < H; j++) {
|
for (int j = 0; j < H; j++) {
|
||||||
for (int i = 0; i < W; i++) {
|
for (int i = 0; i < W; i++) {
|
||||||
for (int c = 0; c < 3; c++) {
|
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;
|
if (max == 0) return 1;
|
||||||
for (int j = 0; j < H; j++) {
|
for (int j = 0; j < H; j++) {
|
||||||
for (int i = 0; i < W; i++) {
|
for (int i = 0; i < W; i++) {
|
||||||
for (int c = 0; c < 3; c++) {
|
for (int c = 0; c < 3; c++) {
|
||||||
buffer[j*W + i][c] /= max;
|
buffer[color_index(i, j, c)] /= max;
|
||||||
buffer[j*W+i][c] = linear_to_srgb(buffer[j*W+i][c]);
|
buffer[color_index(i, j, c)] = linear_to_srgb(buffer[color_index(i, j, c)]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
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);
|
buffer_normalize_srgb(buffer, W, H);
|
||||||
size_t bufsize = W*H*3;
|
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;}
|
if (!img) {return 1;}
|
||||||
|
|
||||||
for (int j = 0; j < H; j++) {
|
for (int j = 0; j < H; j++) {
|
||||||
for (int i = 0; i < W; i++) {
|
for (int i = 0; i < W; i++) {
|
||||||
for (int c = 0; c < 3; c++) {
|
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 < 0) v = 0;
|
||||||
if (v > 255) v = 255;
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
|
#include "common.h"
|
||||||
|
|
||||||
|
int write_png(char *filename, double *buffer, int W, int H);
|
||||||
int write_png(char *filename, double (*buffer)[3], int W, int H);
|
|
||||||
|
|||||||
Reference in New Issue
Block a user