Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <string.h>
- #include <io.h>
- /*
- unsigned long int - DWORD
- unsigned short int - WORD
- unsigned char - BYTE
- */
- #define File_Name "lab3.1.bmp"
- #define ERR_OPEN_FILE 1
- #define ERR_INVALID_FILE 2
- #define ERR_BUFFSIZE 3
- #define ERR_MALLOC 4
- #define GAUSS_SIZE 5
- typedef struct tagBITMAPFILEHEADER
- {
- unsigned short int FileType;//Тип файла, должен быть BM(BitMap)
- unsigned long int FileSize;//размер файла в байтах
- unsigned short int Reserved1;//зарезервированы и должны быть 0
- unsigned short int Reserved2;//
- unsigned long int OffBits;//показывает, где начинается битовый массив
- }BMFileHeader;
- typedef struct tagBITMAPINFOHEADER
- {
- unsigned long int StructSize;//размер структуры в байтах
- long int Width;//ширина картинки в пикселях
- long int Height;//высота картинки
- unsigned short int Planes;//кол-во плоскостей
- unsigned short int BitCount;//кол-во бит.пиксель
- unsigned long int Compression;//BI_RGB - несжатый bmp
- unsigned long int ImageSize;//размер картинки в байтах
- long int XperMeter;//горизонтальное разрешение
- long int YperMeter;//вертикальное разрешение
- unsigned long int ColoursUsed;//кол-во используемых цветов из палитры
- unsigned long int ColoursImportant;//=0, если все цвета важные
- }BMInfoHeader;
- typedef struct tagRGBQUAD
- {
- unsigned char Red;
- unsigned char Green;
- unsigned char Blue;
- }RGB;
- #pragma pack(pop)
- struct Image
- {
- long int width;
- long int height;
- RGB** raster=nullptr;
- };
- struct ErrorObject
- {
- int code;
- char message[256];
- };
- void MatrixMalloc(RGB*** image,long int height, long int width)
- {
- *image = (RGB**)malloc(height * sizeof(RGB*));
- if (*image)
- {
- for (int i = 0; i < height; i++)
- {
- (*image)[i] = (RGB*)malloc(width * sizeof(RGB));
- if (!(*image)[i])
- {
- while (i)
- {
- i--;
- free((*image)[i]);
- }
- free(*image);
- break;
- }
- }
- }
- }
- void Copy_Matrix(RGB **Destination_Matrix, RGB **Source_Matrix, long int height, long int width)
- {
- for (int i = 0; i < height; i++)
- for (int j = 0; j < width; j++)
- Destination_Matrix[i][j] = Source_Matrix[i][j];
- }
- void FreeMatrix(RGB** raster, long int height)
- {
- for (int i = 0; i < height; i++)
- free(raster[i]);
- }
- Image OpenBmpFile(const char* file_name, ErrorObject* error)
- {
- Image image = { 0 };
- FILE *bmp_file;
- fopen_s(&bmp_file, file_name, "rb");
- if (bmp_file == NULL)
- {
- error->code = ERR_OPEN_FILE;
- strcpy_s(error->message, "Can't open file ");
- strcat_s(error->message, file_name);
- return image;
- }
- BMFileHeader File_Header;
- int read_count = fread(&File_Header, sizeof(BMFileHeader), 1, bmp_file);
- if (read_count == 0)
- {
- error->code = ERR_INVALID_FILE;
- strcpy_s(error->message, "Can't read File Header ");
- strcat_s(error->message, file_name);
- fclose(bmp_file);
- return image;
- }
- BMInfoHeader Info_Header;
- read_count = fread(&Info_Header, sizeof(BMInfoHeader), 1, bmp_file);
- if (read_count == 0)
- {
- error->code = ERR_INVALID_FILE;
- strcpy_s(error->message, "Can't read InfoHeader ");
- strcat_s(error->message, file_name);
- fclose(bmp_file);
- return image;
- }
- MatrixMalloc(&image.raster,Info_Header.Height, Info_Header.Width);
- if (!image.raster)
- {
- error->code = ERR_MALLOC;
- strcpy_s(error->message, "Can't allocate memory ");
- strcat_s(error->message, file_name);
- fclose(bmp_file);
- return image;
- }
- for (int i = 0; i < Info_Header.Height; i++)
- fread(image.raster[i], sizeof(RGB), Info_Header.Width, bmp_file);
- for (int i = 0; i < Info_Header.Height; i++)
- for (int j = 0; j < Info_Header.Width; j++)
- printf("%d %d %d\t", image.raster[i][j].Red, image.raster[i][j].Green, image.raster[i][j].Blue);
- fclose(bmp_file);
- image.width = Info_Header.Width;
- image.height = Info_Header.Height;
- return image;
- }
- int main()
- {
- ErrorObject Error;
- Image image=OpenBmpFile(File_Name, &Error);
- if (Error.code > 0 && Error.code < 6)
- {
- printf("%s", Error.message);
- system("pause>nul");
- return 0;
- }
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement