git ssb

1+

cel / pngspark



Commit 1954e349b19978c7e219c6610f4b143d6658af82

Add min scaling factor

Charles Lehner committed on 2/10/2015, 2:02:33 PM
Parent: c52a3123eba4e4c49aabce41af35e284092287ad

Files changed

main.cchanged
pngspark.cchanged
pngspark.hchanged
main.cView
@@ -18,16 +18,17 @@
1818 {
1919 struct pngspark ps;
2020 const char *color = "#000000";
2121 const char *filename = "pngspark.png";
22 + double scaling = 0.8;
2223 int height = 10;
2324
2425 for (int i = 1; i < argc; i++) {
2526 if (argv[i][0] != '-') continue;
2627 if (argv[i][1] == '-') {
2728 if (!strcmp("help", argv[i]+2)) {
2829 errx(1, "Usage: %s [--help] [-h height] "
29- "[-o output.png] [-c color]", argv[0]);
30 + "[-o output.png] [-c color] [-s scale_min]", argv[0]);
3031 }
3132 } else if (!argv[i][2]) switch (argv[i][1]) {
3233 case 'c':
3334 if (++i < argc) color = argv[i];
@@ -37,15 +38,18 @@
3738 break;
3839 case 'h':
3940 if (++i < argc) height = atoi(argv[i]);
4041 break;
42 + case 's':
43 + if (++i < argc) scaling = atof(argv[i]);
44 + break;
4145 }
4246 }
4347
4448 FILE *file = fopen(filename, "w");
4549 if (!file) err(1, "unable to open file %s", filename);
4650
47- if (pngspark_init(&ps, height, color) < 0)
51 + if (pngspark_init(&ps, height, color, scaling) < 0)
4852 return 1;
4953
5054 char c;
5155 char buffer[32];
pngspark.cView
@@ -1,7 +1,8 @@
1 +#include <values.h>
2 +#include <stdio.h>
13 #include <stdlib.h>
24 #include <string.h>
3-#include <stdio.h>
45 #include <unistd.h>
56
67 #include "lupng.h"
78 #include "pngspark.h"
@@ -16,17 +17,20 @@
1617 return 0xff000000 |
1718 ((color >> 16) | (color & 0x00ff00) | (color & 0xff) << 16);
1819 }
1920
20-int pngspark_init(struct pngspark *ps, size_t height, const char *color)
21 +int pngspark_init(struct pngspark *ps, size_t height, const char *color,
22 + double scaling)
2123 {
2224 ps->size = initial_size;
2325 ps->num_values = 0;
2426 ps->values = malloc(initial_size * sizeof(double));
2527 if (!ps->values) return 1;
2628 ps->color = parse_color(color);
2729 ps->height = height;
2830 ps->max_value = 0;
31 + ps->min_value = DBL_MAX;
32 + ps->scaling = scaling;
2933 return 0;
3034 }
3135
3236 int pngspark_append(struct pngspark *ps, double value)
@@ -38,8 +42,10 @@
3842 }
3943 ps->values[i] = value;
4044 if (value > ps->max_value)
4145 ps->max_value = value;
46 + if (value < ps->min_value)
47 + ps->min_value = value;
4248 return 0;
4349 }
4450
4551 static size_t write_fd(const void *ptr, size_t size, size_t count,
@@ -57,12 +63,13 @@
5763
5864 double *values = ps->values;
5965 uint32_t *pixels = (uint32_t *)img->data;
6066 uint32_t color = ps->color;
61- double height_scale = (double)height / (double)ps->max_value;
67 + ps->min_value *= ps->scaling;
68 + double height_scale = (double)height / (ps->max_value - ps->min_value);
6269
6370 for (size_t x = 0; x < width; x++) {
64- size_t value = height - (values[x] * height_scale);
71 + size_t value = height - ((values[x] - ps->min_value) * height_scale);
6572 for (size_t y = 0; y < value; y++)
6673 pixels[x + width * y] = 0;
6774 for (size_t y = value; y < height; y++)
6875 pixels[x + width * y] = color;
pngspark.hView
@@ -5,15 +5,17 @@
55
66 struct pngspark {
77 size_t num_values;
88 size_t size;
9- size_t max_value;
109 size_t height;
1110 uint32_t color;
1211 double *values;
12 + double max_value;
13 + double min_value;
14 + double scaling;
1315 };
1416
15-int pngspark_init(struct pngspark *, size_t, const char *);
17 +int pngspark_init(struct pngspark *, size_t, const char *, double);
1618 int pngspark_append(struct pngspark *, double);
1719 int pngspark_write(struct pngspark *, FILE *);
1820 int pngspark_end(struct pngspark *);
1921

Built with git-ssb-web