86 lines
1.8 KiB
C
86 lines
1.8 KiB
C
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
int main(int argc, char *argv[]) {
|
|
if (argc < 2) {
|
|
fprintf(stderr, "Argumentos invalidos\n");
|
|
return 1;
|
|
}
|
|
|
|
char *fname;
|
|
|
|
fname = strdup(argv[1]);
|
|
|
|
if (fname == NULL) {
|
|
perror("strdup");
|
|
return 1;
|
|
}
|
|
|
|
FILE *pfile = fopen(fname, "rb");
|
|
|
|
if (pfile == NULL) {
|
|
perror("fopen");
|
|
fclose(pfile);
|
|
return 1;
|
|
}
|
|
|
|
uint8_t *header = NULL;
|
|
|
|
header = malloc(sizeof(uint8_t) * 3);
|
|
|
|
printf("El sizeof de header es: %d\n", (int)sizeof(header));
|
|
|
|
if (fread(header, sizeof(uint8_t) * 2, 1, pfile) == 0) {
|
|
perror("fread");
|
|
fclose(pfile);
|
|
return 1;
|
|
}
|
|
|
|
header[3] = '\0';
|
|
|
|
int w, h, s;
|
|
|
|
if (fscanf(pfile, " %d %d %d", &w, &h, &s) != 3) {
|
|
fprintf(stderr, "Error leyendo las caracteristicas de la imagen.\n");
|
|
fclose(pfile);
|
|
return 1;
|
|
}
|
|
|
|
fprintf(stdout, "Header readed: %s\n", (unsigned char *)header);
|
|
fprintf(stdout,
|
|
"Las dimensiones son %dx%d pixeles y una escala maxima de color de "
|
|
"%d\n",
|
|
w, h, s);
|
|
|
|
// consumo un caracter mas y queda listo para leer los datos de los pixeles.
|
|
fgetc(pfile);
|
|
|
|
unsigned char *data = NULL;
|
|
unsigned long pixel_count = w * h;
|
|
|
|
data = malloc(sizeof(data) * pixel_count * 3);
|
|
|
|
if (data == NULL) {
|
|
perror("malloc");
|
|
fclose(pfile);
|
|
return 1;
|
|
}
|
|
|
|
unsigned long readed =
|
|
fread(data, sizeof(unsigned char) * 3, pixel_count, pfile);
|
|
|
|
fprintf(stdout, "%zu pixeles leidos de %zu\n", readed, pixel_count);
|
|
|
|
if (readed != pixel_count) {
|
|
fprintf(stderr, "Error leyendo datos del archivo\n");
|
|
free(data);
|
|
fclose(pfile);
|
|
}
|
|
|
|
free(data);
|
|
fclose(pfile);
|
|
return 0;
|
|
}
|