#include <SD.h>
#include <SPI.h>
#include<Stream.h>
#include<stdlib.h>
#include<stdbool.h>
#include<math.h>
#define CS_PIN 53
File fp;
File fp2;
int widthb;
int width; //picture width pixel
int heightb;
int height; //picture height pixel
int pixelDataStartb;
int pixelDataStart; //pixel data start
int bytesprow; //number of bytes per row including padding
double widthbytesRD; //numer of bytes per row without padding
int widthbytesRI; //numer of bytes per row without padding
int totalbytesWPadding;
byte buff;
int bufff;
bool* arr1;
bool* khewabytesinBIN[1];
bool B[8];
int intg;
void setup() {
Serial.begin(115200);
SD.begin();
fp2 = SD.open("wokwi.bmp");
fp2.seek(22);
fp2.read(&bufff, 4);
int height= bufff;
// fp.seek(0);
fp2.close();
fp2 = SD.open("wokwi.bmp");
fp2.seek(18);
fp2.read(&bufff, 4);
int width= bufff;
//fp.seek(0);
fp2.close();
fp = SD.open("wokwi.bmp");
fp.seek(10);
fp.read(&buff, 4);
int pixelDataStart= buff;
//fp.seek(0);
fp.close();
bytesprow = ((width+31)/32)*4;
widthbytesRD = ceil(((double)width)/8);
widthbytesRI = (int)widthbytesRD;
Serial.print("picture width pixel = ");
Serial.println(width);
Serial.print("picture height pixel = ");
Serial.println(height);
Serial.print("pixel data start from = ");
Serial.println(pixelDataStart);
Serial.print("number of bytes per row including padding = ");
Serial.println(bytesprow);
Serial.print("number of bytes per row without padding = ");
Serial.println(widthbytesRI);
totalbytesWPadding = bytesprow*height;
Serial.print("total bytes with padding = ");
Serial.println(totalbytesWPadding);
fp = SD.open("wokwi.bmp");
int fullPaddingPRow = bytesprow - widthbytesRI;
fp.close();
int j;
int p=0;
for (size_t i = 0; i < 3; i++) {
Serial.println(widthbytesRI);
j=0;
khewabytesinBIN[i]= calloc(widthbytesRI*8, sizeof(bool));
int w=widthbytesRI;
Serial.println(widthbytesRI);
Serial.println(widthbytesRI);
//int w=widthbytesRI;
Serial.print("w= ");
Serial.println(w);
for (int k = 0; k <w ; k++) {
fp = SD.open("wokwi.bmp");
fp.seek(pixelDataStart+p);
p++;
buff = fp.read();
int intg = (int)buff;
//intg=intgr;
int b1=7;
int b2=0;
while (intg>0) {
B[b1]=intg%2;
intg=intg/2;
b1--;
b2++;
}
int b3=b1;
for (int b = 0; b < b1+1; b++) {
B[b3]=0;
b3--;
}
for (int m = 0; m < 8; m++) {
//Serial.print(B[m]);
}
for (int z = 0; z < 8; z++) {
khewabytesinBIN[i][j]=B[z];
j++;
}
///Serial.println(j);
fp.close();
}
p=p+fullPaddingPRow;
Serial.println(j);
Serial.println(widthbytesRI);
}
Serial.println("");
Serial.println(j);
Serial.println(widthbytesRI);
Serial.println("");
for(size_t i=0; i<40*8; i++){
Serial.print(khewabytesinBIN[0][i]);
}
// arr1 = calloc(3,sizeof(bool));
// arr1[0]= 1;
// arr1[1]=0;
// arr1[2]=1;
// Serial.println(arr1[2]);
}
void loop() {
// nothing happens after setup finishes.
}