After converting .ppm output to .png with ImageMagick:
Created
November 24, 2012 22:54
-
-
Save Rhomboid/4141723 to your computer and use it in GitHub Desktop.
Simple ppm heatmap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <time.h> | |
#define COLORMAP_STEPS 2 | |
typedef double colormap[3][COLORMAP_STEPS + 1][3]; | |
/* interpolate 'val' from range [inlow, inhigh) into range [outlow, outhigh) */ | |
static double interpolate(double val, double inlow, double inhigh, double outlow, double outhigh) | |
{ | |
return (val - inlow) / (inhigh - inlow) * (outhigh - outlow) + outlow; | |
} | |
/* apply colormap 'map' to value 'val', and write it to 'outstream' */ | |
static void apply_colormap_and_write(double val, colormap map, FILE *outstream, int newline) | |
{ | |
for(int c = 0; c < 3; c++) | |
for(int i = 0; i < COLORMAP_STEPS; i++) | |
if(val >= map[c][i][0] && val < map[c][i+1][0]) { | |
fprintf(outstream, "%3d ", | |
(int)(256.0 * interpolate(val, map[c][i][0], map[c][i+1][0], map[c][i][2], map[c][i+1][1]))); | |
break; | |
} | |
if(newline) | |
fputc('\n', outstream); | |
} | |
int main() | |
{ | |
const int NROWS = 10, NCOLS = 10, SQUARE_SIZE_PX = 50; | |
double m[NROWS][NCOLS]; | |
colormap cmap = { { { 0.0, 0.0, 0.0 }, | |
{ 0.5, 1.0, 0.7 }, | |
{ 1.0, 1.0, 1.0 } }, | |
{ { 0.0, 0.0, 0.0 }, | |
{ 0.5, 1.0, 0.0 }, | |
{ 1.0, 1.0, 1.0 } }, | |
{ { 0.0, 0.0, 0.0 }, | |
{ 0.5, 1.0, 0.0 }, | |
{ 1.0, 0.5, 1.0 } } }; | |
srand48(time(NULL)); | |
for(int r = 0; r < NROWS; r++) | |
for(int c = 0; c < NCOLS; c++) | |
m[r][c] = drand48(); | |
printf("P3\n%d %d\n255\n", NCOLS * SQUARE_SIZE_PX, NROWS * SQUARE_SIZE_PX); | |
for(int r = 0; r < NROWS; r++) | |
for(int rr = 0; rr < SQUARE_SIZE_PX; rr++) | |
for(int c = 0; c < NCOLS; c++) | |
for(int cc = 0; cc < SQUARE_SIZE_PX; cc++) | |
apply_colormap_and_write(m[r][c], cmap, stdout, c == NCOLS - 1 && cc == SQUARE_SIZE_PX - 1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment