-
Notifications
You must be signed in to change notification settings - Fork 204
Expand file tree
/
Copy pathLeptonActionFilePgm.cpp
More file actions
95 lines (82 loc) · 1.86 KB
/
LeptonActionFilePgm.cpp
File metadata and controls
95 lines (82 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include <linux/limits.h>
#include "LeptonActionFilePgm.h"
LeptonActionFilePgm::LeptonActionFilePgm()
{
}
LeptonActionFilePgm::LeptonActionFilePgm(int w, int h)
{
create(w, h);
}
LeptonActionFilePgm::~LeptonActionFilePgm() {
if (image_pgm != NULL) {
free(image_pgm);
}
}
void LeptonActionFilePgm::create(int w, int h)
{
if (image_pgm != NULL) {
free(image_pgm);
}
image_pgm = (uint16_t *)malloc(sizeof(uint16_t) * w * h);
image_width = w;
image_height = h;
}
char * LeptonActionFilePgm::getDefaultFilename(char *filename)
{
return LeptonActionFile::getDefaultFilename(filename);
}
char* LeptonActionFilePgm::getFileExt(char *extname)
{
sprintf(extname, ".pgm");
return extname;
}
void LeptonActionFilePgm::saveBasename(char *filebase)
{
LeptonActionFile::saveBasename(filebase);
}
void LeptonActionFilePgm::save()
{
LeptonActionFile::save();
}
void LeptonActionFilePgm::save(char *filename)
{
if (filename == NULL) {
save();
return;
}
char *magicNumber = "P2"; // PGM
uint16_t valMin = UINT16_MAX;
uint16_t valMax = 0;
for (int row = 0; row < image_height; row++) {
for (int col = 0; col < image_width; col++) {
uint16_t val = *(image_pgm + row * image_width + col);
if (val < valMin) {
valMin = val;
}
if (valMax < val) {
valMax = val;
}
}
}
//
FILE *fh = fopen(filename, "w");
fprintf(fh, "%s\n", magicNumber);
fprintf(fh, "%d %d\n", image_width, image_height);
fprintf(fh, "%d\n", valMax - valMin);
for (int row = 0; row < image_height; row++) {
for (int col = 0; col < image_width; col++) {
uint16_t val = *(image_pgm + row * image_width + col) - valMin;
fprintf(fh, "%d ", val);
}
fprintf(fh, "\n");
}
fclose(fh);
}
void LeptonActionFilePgm::setRawValue(int col, int row, uint16_t val)
{
*(image_pgm + row * image_width + col) = val;
}