I have image.pgm on SD card. In this code, I can't print the size of the image after reading, the code inters in loop. The commented lines represent the code in c, and it was work (as a c code) without any problem. I convert some functions the deal with files to can work on Arduino but the problem I can't get the size of image. any help please? to read the image in correct way?
Code:
#include <SD.h>
typedef struct _PGMData {
int row;
int col;
int max_gray;
int **matrix;
} PGMData;
#define HI(num) (((num) & 0x0000FF00) << 8)
#define LO(num) ((num) & 0x000000FF)
int **allocate_dynamic_matrix(int row, int col)
{
int **ret_val;
int i;
ret_val = (int **)malloc(sizeof(int *) * row);
if (ret_val == NULL) {
perror("memory allocation failure");
exit(EXIT_FAILURE);
}
for (i = 0; i < row; ++i) {
ret_val[i] = (int *)malloc(sizeof(int) * col);
if (ret_val[i] == NULL) {
perror("memory allocation failure");
exit(EXIT_FAILURE);
}
}
return ret_val;
}
void deallocate_dynamic_matrix(int **matrix, int row)
{
int i;
for (i = 0; i < row; ++i) {
free(matrix[i]);
}
free(matrix);
}
void SkipComments(File &fp)
{
int ch;
char line[100];
/*
while ((ch = fgetc(fp)) != EOF && isspace(ch)) {
;
}
if (ch == '#') {
fgets(line, sizeof(line), fp);
SkipComments(fp);
} else {
fseek(fp, -1, SEEK_CUR);
}
*/
while (fp.available() && isspace(ch) ){
// Serial.write(fin.read());
ch = fp.read();
}
if (ch == '#') {
// fgets(line, sizeof(line), fp);
int bar=0;
int in_char;
while((in_char = fp.read()) >= 0){
line[bar++] = (char)in_char;
}
SkipComments(fp);
} else {
// fseek(fp, -1, SEEK_CUR);
fp.seek(fp.position());
}
}
//for reading:*
PGMData* readPGM( PGMData *data)
{
File pgmFile;
char version[3];
int i, j;
int lo, hi;
if (!SD.begin(BUILTIN_SDCARD)) {
Serial.println("Failed to mount card read pgm");
}
pgmFile = SD.open("/image.pgm", FILE_READ);
if (!pgmFile) {
Serial.println("Opening file to read failed read pgm");
}
/* if (pgmFile == NULL) {
perror("cannot open file to read");
exit(EXIT_FAILURE);
}*/
/* fgets(version, sizeof(version), pgmFile);
if (strcmp(version, "P5")) {
fprintf(stderr, "Wrong file type!\n");
exit(EXIT_FAILURE);
}
*/
/*
SkipComments(pgmFile);
// fscanf(pgmFile, "%d", &data->col);
SkipComments(pgmFile);
// fscanf(pgmFile, "%d", &data->row);
SkipComments(pgmFile);
// fscanf(pgmFile, "%d", &data->max_gray);
*/
while (pgmFile.available()){
SkipComments(pgmFile);
// fscanf(pgmFile, "%d", &data->col);
data->col = pgmFile.read();
SkipComments(pgmFile);
// fscanf(pgmFile, "%d", &data->row);
data->row = pgmFile.read();
SkipComments(pgmFile);
// fscanf(pgmFile, "%d", &data->max_gray);
data->max_gray = pgmFile.read();
}
/// fgetc(pgmFile);
data->matrix = allocate_dynamic_matrix(data->row, data->col);
if (data->max_gray > 255) {
for (i = 0; i < data->row; ++i) {
for (j = 0; j < data->col; ++j) {
hi = pgmFile.read(); //fgetc(pgmFile);
lo = pgmFile.read(); //fgetc(pgmFile);
data->matrix[i][j] = (hi << 8) + lo;
}
}
}
else {
for (i = 0; i < data->row; ++i) {
for (j = 0; j < data->col; ++j) {
lo = pgmFile.read(); //fgetc(pgmFile);
data->matrix[i][j] = lo;
}
}
}
pgmFile.close();
return data;
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
while (!Serial){};
PGMData im;
readPGM( &im);
Serial.print("The size of image = ");
Serial.println(im.col);
Serial.println(im.row);
}
void loop() {
// put your main code here, to run repeatedly:
}