#include <stdio.h>
#include <stdlib.h>
#pragma pack(push, 1) // Alinear la estructura
typedef struct {
unsigned short bfType; // Tipo de archivo
unsigned int bfSize; // Tamaño del archivo
unsigned short bfReserved1; // Reservado
unsigned short bfReserved2; // Reservado
unsigned int bfOffBits; // Offset a los datos de la imagen
} BITMAPFILEHEADER;
typedef struct {
unsigned int biSize; // Tamaño de la estructura
int biWidth; // Ancho de la imagen
int biHeight; // Alto de la imagen
unsigned short biPlanes; // Planos de color
unsigned short biBitCount; // Bits por pixel
unsigned int biCompression; // Compresión
unsigned int biSizeImage; // Tamaño de la imagen
int biXPelsPerMeter; // Resolución horizontal
int biYPelsPerMeter; // Resolución vertical
unsigned int biClrUsed; // Colores utilizados
unsigned int biClrImportant; // Colores importantes
} BITMAPINFOHEADER;
#pragma pack(pop)
void convertirBMPaC(const char* nombreArchivo) {
FILE* archivo = fopen(nombreArchivo, "rb");
if (!archivo) {
printf("No se pudo abrir el archivo.\n");
return;
}
BITMAPFILEHEADER bfh;
fread(&bfh, sizeof(BITMAPFILEHEADER), 1, archivo);
BITMAPINFOHEADER bih;
fread(&bih, sizeof(BITMAPINFOHEADER), 1, archivo);
// Leer los datos de la imagen
int tamanoImagen = bih.biWidth * bih.biHeight * (bih.biBitCount / 8);
unsigned char* datos = (unsigned char*)malloc(tamanoImagen);
fseek(archivo, bfh.bfOffBits, SEEK_SET);
fread(datos, tamanoImagen, 1, archivo);
fclose(archivo);
// Imprimir el array en formato C
printf("unsigned char imagen[%d][%d][3] = {\n", bih.biHeight, bih.biWidth);
for (int i = 0; i < bih.biHeight; i++) {
printf(" {");
for (int j = 0; j < bih.biWidth; j++) {
int index = (i * bih.biWidth + j) * (bih.biBitCount / 8);
printf("{%d, %d, %d}", datos[index + 2], datos[index + 1], datos[index]); // RGB
if (j < bih.biWidth - 1) printf(", ");
}
printf("}");
if (i < bih.biHeight - 1) printf(",\n");
}
printf("\n};\n");
free(datos);
}
int main() {
convertirBMPaC("tu_imagen.bmp"); // Cambia "tu_imagen.bmp" por el nombre de tu archivo
return 0;
}