Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- //#include "stdafx.h"
- #pragma pack(push, 2)
- typedef struct {
- char signature[2];
- int size;
- short emptyField1;
- short emptyField2;
- int data;
- int lengthHead;
- int width;
- int heidth;
- short countColorPlace;
- short bit;
- char methodArch[4];
- int lengthRastrMas;
- int gorisResolution;
- int vertResolution;
- int countColorPic;
- int countMainColor;
- } head_bmp;
- #pragma pack(pop)
- char nameFile[] = "1.bmp";
- int saveImg(char *buf, head_bmp head, int width, int heigth, int comp)
- {
- FILE *f;
- f = fopen("1_1_1.bmp", "wb");
- fwrite(&head.signature, sizeof(char), 2, f);
- fwrite(&head.size, sizeof(int), 1, f);
- fseek(f, sizeof(char)* 2 + sizeof(int)+4, SEEK_SET);
- fwrite(&head.data, sizeof(int), 1, f);
- fwrite(&head.lengthHead, sizeof(int), 1, f);
- fwrite(&head.width, sizeof(int), 1, f);
- fwrite(&head.heidth, sizeof(int), 1, f);
- fwrite(&head.countColorPlace, sizeof(short), 1, f);
- fwrite(&head.bit, sizeof(short), 1, f);
- fwrite(&head.methodArch, sizeof(char), 4, f);
- fwrite(&head.lengthRastrMas, sizeof(int), 1, f);
- fwrite(&head.gorisResolution, sizeof(int), 1, f);
- fwrite(&head.vertResolution, sizeof(int), 1, f);
- fwrite(&head.countColorPic, sizeof(int), 1, f);
- fwrite(&head.countMainColor, sizeof(int), 1, f);
- fseek(f, head.data, SEEK_SET);
- fwrite(buf, sizeof(char)* ((3 * width*heigth) + (comp*heigth)), 1, f);
- fclose(f);
- return 0;
- }
- int main()
- {
- head_bmp imgHead;
- FILE* f;
- char *buffer;
- char *temp;
- int comp;
- fpos_t *posData;
- int i, j = 0;
- char t,p;
- int index,w;
- if ((f = fopen("1.bmp", "rb")) == NULL)
- {
- printf("Ошибочка вышла: файла не существует");
- return -1;
- }
- fread(&imgHead, sizeof(char)*54, 1, f);
- /*if (imgHead.signature[0] == 66 && imgHead.signature[1] == 77)
- {
- fread(&imgHead.size, sizeof(int), 1, f);
- fseek(f, sizeof(char) * 2 + sizeof(int) + 4, SEEK_SET);
- fread(&imgHead.data, sizeof(int), 1, f);
- fread(&imgHead.lengthHead, sizeof(int), 1, f);
- fread(&imgHead.width, sizeof(int), 1, f);
- fread(&imgHead.heidth, sizeof(int), 1, f);
- fread(&imgHead.countColorPlace, sizeof(short), 1, f);
- fread(&imgHead.bit, sizeof(short), 1, f);
- fread(&imgHead.methodArch, sizeof(char), 4, f);
- fread(&imgHead.lengthRastrMas, sizeof(int), 1, f);
- fread(&imgHead.gorisResolution, sizeof(int), 1, f);
- fread(&imgHead.vertResolution, sizeof(int), 1, f);
- fread(&imgHead.countColorPic, sizeof(int), 1, f);
- fread(&imgHead.countMainColor, sizeof(int), 1, f);
- }
- else
- {
- printf("Ошибочка вышла: файла не формата BMP");
- fclose(f);
- }*/
- if (imgHead.bit == 24)
- {
- comp = (4 - (3*imgHead.width) % 4);
- buffer = (char*)malloc(sizeof(char) * ((3 * imgHead.width*imgHead.heidth) + (comp*imgHead.heidth)));
- fseek(f, imgHead.data, SEEK_SET);
- fread(buffer, sizeof(char)* ((3 * imgHead.width*imgHead.heidth) + (comp*imgHead.heidth)), 1, f);
- for (i = 1; i <= imgHead.heidth; i = i + 1)
- {
- index = 3 * (i - 1)*imgHead.width + (i - 1)*comp;
- w = (int)3 * floor(imgHead.width / 2);
- for (j = 0; j <= w; j = j + 3)
- {
- t = buffer[index + j];
- buffer[index + j] = buffer[3 * i*imgHead.width - j - 3 + (i-1)*comp];
- buffer[3 * i*imgHead.width - j - 3 + (i-1)*comp] = t;
- t = buffer[index + j + 1];
- buffer[index + j + 1] = buffer[3 * i*imgHead.width - j - 2 + (i-1)*comp];
- buffer[3 * i*imgHead.width - j -2 + (i-1)*comp] = t;
- t = buffer[index + j + 2];
- buffer[index + j + 2] = buffer[3 * i*imgHead.width - j-1 + (i-1)*comp];
- buffer[3 * i*imgHead.width - j-1 + (i-1)*comp] = t;
- }
- }
- saveImg(buffer, imgHead, imgHead.width, imgHead.heidth, comp);
- }
- else
- {
- printf("Ошибочка вышла: глубина цвета должна быть равна 24");
- fclose(f);
- }
- printf("Hello World");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement