Thread: Reading bmp file to 2d array

  1. #1
    Registered User
    Join Date
    Mar 2020
    Posts
    18

    Reading bmp file to 2d array

    hey guys i need some help.i need to get rgb from bmp file
    my problem: when i get the colors my output is CDCDCD32,CDCDCD84,CDCDCDF9
    i need to get
    32,84,F9
    how can i move the bite?
    my code is :
    Code:
    int bitmapin(FILE* fp, int height, int width,  pixel** img) {	long n;
    	int t;
    	fseek(fp, 10, SEEK_SET);
    	fread(&t, 1, 4, fp);     //reads the offset and puts it in t
    	fseek(fp, t, SEEK_SET);
    	int i, p, e;
    	e = 4 - ((height * 3) % 4);
    	if (e == 4) {
    		e = 0;
    	}
    	for (i = height - 1; i >= 0; i--) {
    		for (p = 0; p < width; p++) {
    			n = fread(&img[i][p].blue, 1, 1, fp);
    			if (n != 1) {
    				return 29;
    			}
    			n = fread(&img[i][p].green, 1, 1, fp);
    			if (n != 1) {
    				return 30;
    			}
    			n = fread(&img[i][p].red, 1, 1, fp);
    			if (n != 1) {
    				return 31;
    			}
    		}
    		fseek(fp, e, SEEK_CUR);
    	}
    	printf("%X,%X,%X",img[1][1].red, img[1][1].green, img[1][1].blue);
    	return 0;
    }
    Code:
    typedef struct {
    	int blue;
    	int green;
    	int red;
    } pixel;
    Code:
    pixel** make2Dpixelarray(int Y, int X) {	pixel** theArray;
    	theArray = (pixel**)malloc(X * sizeof(pixel*));
    	for (int i = 0; i < X; i++) {
    		theArray[i] = (pixel*)malloc(Y * sizeof(pixel));
    	}
    	return theArray;
    }

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    421
    The fields of the struct pixel are ints. Probably 4 bytes. The values in the .bmp file are 8 bit. You are reading 1 byte into a 4 byte variable, and getting nonsense results.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  3. #3
    Registered User
    Join Date
    Mar 2020
    Posts
    18
    the struct of colors is int so i need to exchange it to char ?

  4. #4
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    738
    Change them to uint8_t and add #include <stdint.h> to the top of the source file (that's from C99 which has been around for over 20 years now).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. writing array into file and reading array from file
    By isolatedleo in forum C Programming
    Replies: 1
    Last Post: 04-20-2014, 02:59 AM
  2. Reading from a file into an array
    By fmsguy06 in forum C Programming
    Replies: 6
    Last Post: 10-18-2008, 09:25 PM
  3. Reading int's from file to array HELP plz
    By GARiMTO in forum C Programming
    Replies: 3
    Last Post: 12-14-2007, 06:12 AM
  4. Replies: 1
    Last Post: 04-25-2006, 12:14 AM
  5. Reading a *.csv file into an array
    By AQWst in forum C++ Programming
    Replies: 5
    Last Post: 12-18-2004, 01:12 AM

Tags for this Thread