Print file number of stored image

This commit is contained in:
fruchti 2024-12-18 22:01:04 +01:00
parent dc61ac7f85
commit 59b2022e48
7 changed files with 79 additions and 21 deletions

View file

@ -64,7 +64,7 @@ void BMP_ConstructFilename(int number, char *buffer, unsigned int size)
memcpy(buffer + size - 5, ".BMP", 5);
}
void BMP_Save(uint8_t *data, int width, int height)
int BMP_Save(uint8_t *data, int width, int height)
{
FATFS fs;
FIL fp;
@ -74,19 +74,19 @@ void BMP_Save(uint8_t *data, int width, int height)
rc = f_mount(&fs, "", 0);
if(rc)
{
return;
return -1;
}
int file_number = BMP_PickFileNumber();
if(file_number == -1)
return;
return -2;
char filename[13];
BMP_ConstructFilename(file_number, filename, sizeof(filename));
rc = f_open(&fp, filename, FA_WRITE | FA_CREATE_NEW);
if(rc)
{
return;
return -3;
}
// Rows must be padded to a multiple of 4 bytes
@ -121,24 +121,26 @@ void BMP_Save(uint8_t *data, int width, int height)
};
if(f_write(&fp, &file_header, sizeof(file_header), &bw) != FR_OK)
return;
return -4;
if(f_write(&fp, &core_header, sizeof(core_header), &bw) != FR_OK)
return;
return -4;
if(f_write(&fp, &colour_table, sizeof(colour_table), &bw) != FR_OK)
return;
return -4;
// BMPs are stored with their rows from the bottom upwards
for(int y = height - 1; y >= 0; y--)
{
uint8_t *row = data + y * (width / 8);
if(f_write(&fp, row, width / 8, &bw) != FR_OK)
return;
return -4;
if(padding_length != 0)
{
if(f_write(&fp, padding_bytes, padding_length, &bw) != FR_OK)
return;
return -4;
}
}
f_close(&fp);
return file_number;
}