.txt RGB array to BMP FAULTY OUTPUT

Hi everyone,


My purpose;

RGB values at txt,To convert a file in BMP format.


Bmp to blabla.txt


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
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include "bitmap_image.hpp"

using namespace std;

int main () {
    FILE *streamIn;

    streamIn = fopen("./Lenna.bmp", "r");
    if (streamIn == (FILE *)0) {
        printf("File opening error ocurred. Exiting program.\n");
        return 0;
    }

    unsigned char info[54];
    fread(info, sizeof(unsigned char), 54, streamIn);

    unsigned int width1 = *(int*)&info[18];
    unsigned int height1 = *(int*)&info[22];

    unsigned char image11[width1*height1][3];
    unsigned int i = 0;
    FILE *fw;
    fw = fopen("blabla.txt","w");

    for(i=0;i<width1*height1;i++) {
        image11[i][2] = getc(streamIn);
        image11[i][1] = getc(streamIn);
        image11[i][0] = getc(streamIn);
        //printf("pixel %d : [%d,%d,%d]\n",i+1,image11[i][0],image11[i][1],image11[i][2]);
        fprintf(fw,"%d %d %d\n",image11[i][0],image11[i][1],image11[i][2]);

    }
/*
    fclose(streamIn);
    bitmap_image image(512,512);
               image_drawer draw(image);
				int k = 0;
                for (unsigned int d = 0; d < image.width(); ++d)
               {
                  for (unsigned int m = 0; m < image.height(); ++m)
                  {
                  		image.set_pixel(d,m,image11[k][0],image11[k][1],image11[k][2]);
                    k++;

                  }
               }

               image.save_image("test18_color_maps.bmp");
*/

    return 0;
}


My blabla.txt;

82 22 57
82 22 57
96 32 62
93 28 62
97 30 65
94 21 57
92 26 62
93 28 60
98 36 68
94 27 66
89 21 58
93 25 66
95 27 75
...

.txt to BMP
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
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string.h>
#include "bitmap_image.hpp"

using namespace std;
int next;

void numberlines(){
    ifstream openFile("blabla.txt");
    string line;

    while (getline(openFile, line)) {
    if (line.empty()) continue;

    istringstream iss(line);

    next = 1+next;

  }
  cout<<next<<endl;
}

int main () {
    FILE *streamIn;
    numberlines();  
    streamIn = fopen("./blabla.txt", "r");
    if (streamIn == (FILE *)0) {
        printf("File opening error ocurred. Exiting program.\n");
        return 0;
    }

    //unsigned char info[54];
    //fread(info, sizeof(unsigned char), 54, streamIn);

    unsigned char image11[next][3];
    unsigned int i = 0;
    for(i=0;i<next;i++) {
        image11[i][0] = getc(streamIn);
        image11[i][1] = getc(streamIn);
        image11[i][2] = getc(streamIn);
        printf("pixel %d : [%d,%d,%d]\n",i+1,image11[i][0],image11[i][1],image11[i][2]);    //FAULTY OUTPUT
    }


fclose(streamIn);
    bitmap_image image(512,512);
               image_drawer draw(image);
				int k = 0;
                for (unsigned int d = 0; d < image.width(); ++d)
               {
                  for (unsigned int m = 0; m < image.height(); ++m)
                  {
                  		image.set_pixel(d,m,image11[k][0],image11[k][1],image11[k][2]);
                    k++;

                  }
               }
  cout<<next<<endl;
               image.save_image("test18_color_maps.bmp");


    return 0;
}


FAULTY OUTPUT

My blabla.txt
Line 1 = 82 22 57
txt to BMP
Line 1 = 56 50 32

İf;

unsigned char info[54];
fread(info, sizeof(unsigned char), 54, streamIn);
Line 1 = 57 50 32

54 instead of 12
Line 1 = 50 50 32

...

Why is this happening ?
Last edited on
getc(streamIn); gets a single character like '8'. The ascii code for '8' is 56. See:

http://www.asciitable.com/


What you need is fscanf. See:

http://www.cplusplus.com/reference/cstdio/fscanf/?kw=fscanf
Hmm I got, but;

1
2
3
4
5
6
7
unsigned char info [54];
....
...

image11[i][0] = fscanf (streamIn, "%s", info);
image11[i][1] = fscanf (streamIn, "%s", info);
image11[i][2] = fscanf (streamIn, "%s", info);


Line 1 = 1 1 1
Line 2 = 1 1 1
Line 3 = 1 1 1
....

Values do not get again ?
man fscanf wrote:
RETURN VALUE
These functions return the number of input items successfully matched and assigned, which can be fewer
than provided for, or even zero in the event of an early matching failure.

I understand exactly what you mean,

How do I need to change the code ?

getline() it works but when writing a bitmap, unsigned char I have to do the conversion.

How can I solve this problem?
Well, fscanf works similar to printf, hence use it similar:
1
2
if(fscanf (streamIn, "%d %d %d", &image11[i][0], &image11[i][1], &image11[i][2]) != 3)
  ... // End of file or error -> break; 
But image11 is an array of unsigned chars, not integers. I think you want:
1
2
3
4
5
6
int r, g, b, i;
for (i=0; fscanf (streamIn, "%d %d %d", &r, &g, &b) == 3; ++i) {
    image11[i][0] = r;
    image11[i][1] = g;
    image11[i][2] = b;
}


This has the advantage of counting the lines (or entries anyway) for you too.
It is OK

Thanks to everyone who helped
Topic archived. No new replies allowed.