English 中文(简体)
阅读BMP文档并轮流使用
原标题:Reading a BMP file and rotating it
  • 时间:2024-05-04 01:43:09
  •  标签:
  • c

几个星期以来,我一直在努力在C设立一个职能,以便BMP能够轮流提出90度锁定或反锁定。 无论我做些什么,还是我读/换头脑看什么似乎行不通,我甚至试图让大赦国际产生该守则,但似乎没有任何工作,我不知道我做错做什么,或者说我说什么错了。 如果有人失踪,或者如果能比较容易去做,请让我知道。

谢谢。

EDIT:将第二个问题移到这里,然后会 ask问,对此感到担忧。

(ary, s in spanish)

typedef struct
{
    unsigned short  bm;       //tipo de fichero "BM"
    unsigned int    tam;      //tamanyo
    unsigned short  res1;     //reservado
    unsigned short  res2;     //reservado
    unsigned int    inicio;   //inicio datos de imagen
    unsigned int    head;     //tamanyo de la cabecera del bitmap
    unsigned int    x;        //ancho en pixeles
    unsigned int    y;        //alto en pixeles
    unsigned short  planes;   //numero de planos
    unsigned short  tam_pun;  //tamanyo de cada punto
    unsigned int    comp;     //compresion
    unsigned int    img;      //tamanyo imagen
    unsigned int    r_hor;    //resolucion horizontal
    unsigned int    r_ver;    //resolucion vertical
    unsigned int    t_color;  //tamanyo tabla de color
    unsigned int    impor;    //contador de colores importantes
}bmpHeader;

void rotar(FILE *f, int rotar) {
    bmpHeader header;
    unsigned char *data;
    unsigned char *data_rot;
    int i, j, k;

    // Leer la cabecera del BMP
    fread(&header, sizeof(bmpHeader), 1, f);

    // Asignar memoria para los datos de la imagen
    data = (unsigned char *)malloc(header.img);
    data_rot = (unsigned char *)malloc(header.img);

    // Leer los datos de la imagen
    fread(data, header.img, 1, f);

    // Rotar la imagen
    if (rotar == 0) {
        // Rotar 90 grados a la derecha
        for (i = 0; i < header.x; i++) {
            for (j = 0; j < header.y; j++) {
                k = (header.x - i - 1) * header.y + j;
                data_rot[k] = data[i * header.y + j];
            }
        }
    } else {
        // Rotar 90 grados a la izquierda
        for (i = 0; i < header.x; i++) {
            for (j = 0; j < header.y; j++) {
                k = i * header.y + (header.y - j - 1);
                data_rot[k] = data[i * header.y + j];
            }
        }
    }

    // Escribir la cabecera del BMP
    fwrite(&header, sizeof(bmpHeader), 1, f);

    // Escribir los datos de la imagen
    fwrite(data_rot, header.img, 1, f);

    // Liberar la memoria
    free(data);
    free(data_rot);
}
问题回答

BMP档案中的一个问题是,由于档案一开始的两部“BM”魔鬼,头部所有四级的数值均不成体系。 因此,通常最好从您的“主席”结构中删除,并单独阅读。

bmpHeader header;
char magic[2];

if (fread(magic, 1, sizeof(magic), f) != sizeof(magic) ||
    magic[0] !=  B  || magic[1] !=  M  ||
    fread(&header, 1, sizeof(header), f) != sizeof(header)) {
    fprintf(stderr, "error reading bmp header");
    exit(1);
}

这样,你就不必担心主子的调和或dding。

您也应当永远检查您的<代码>fread打!





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签