Advertisement
EfimSirotkin

Untitled

Apr 20th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.33 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <io.h>
  4. /*
  5. unsigned long int - DWORD
  6. unsigned short int - WORD
  7. unsigned char - BYTE
  8. */
  9. #define File_Name "lab3.1.bmp"
  10.  
  11. #define ERR_OPEN_FILE 1
  12. #define ERR_INVALID_FILE 2
  13. #define ERR_BUFFSIZE 3
  14. #define ERR_MALLOC 4
  15. #define GAUSS_SIZE 5
  16.  
  17. typedef struct tagBITMAPFILEHEADER
  18. {
  19.     unsigned short int FileType;//Тип файла, должен быть BM(BitMap)
  20.     unsigned long int FileSize;//размер файла в байтах
  21.     unsigned short int Reserved1;//зарезервированы и должны быть 0
  22.     unsigned short int Reserved2;//
  23.     unsigned long int OffBits;//показывает, где начинается битовый массив
  24. }BMFileHeader;
  25.  
  26. typedef struct tagBITMAPINFOHEADER
  27. {
  28.     unsigned long int StructSize;//размер структуры в байтах
  29.     long int Width;//ширина картинки в пикселях
  30.     long int Height;//высота картинки
  31.     unsigned short int Planes;//кол-во плоскостей
  32.     unsigned short int BitCount;//кол-во бит.пиксель
  33.     unsigned long int Compression;//BI_RGB - несжатый bmp
  34.     unsigned long int ImageSize;//размер картинки в байтах
  35.     long int XperMeter;//горизонтальное разрешение
  36.     long int YperMeter;//вертикальное разрешение
  37.     unsigned long int ColoursUsed;//кол-во используемых цветов из палитры
  38.     unsigned long int ColoursImportant;//=0, если все цвета важные
  39.  
  40. }BMInfoHeader;
  41.  
  42. typedef struct tagRGBQUAD
  43. {
  44.     unsigned char Red;
  45.     unsigned char Green;
  46.     unsigned char Blue;
  47. }RGB;
  48.  
  49. #pragma pack(pop)
  50.  
  51. struct Image
  52. {
  53.     long int width;
  54.     long int height;
  55.     RGB** raster=nullptr;
  56. };
  57. struct ErrorObject
  58. {
  59.     int code;
  60.     char message[256];
  61. };
  62.  
  63. void MatrixMalloc(RGB*** image,long int height, long int width)
  64. {
  65.     *image = (RGB**)malloc(height * sizeof(RGB*));
  66.     if (*image)
  67.     {
  68.         for (int i = 0; i < height; i++)
  69.         {
  70.             (*image)[i] = (RGB*)malloc(width * sizeof(RGB));
  71.             if (!(*image)[i])
  72.             {
  73.                 while (i)
  74.                 {
  75.                     i--;
  76.                     free((*image)[i]);
  77.                 }
  78.                 free(*image);
  79.                 break;
  80.             }
  81.         }
  82.     }
  83. }
  84.  
  85. void Copy_Matrix(RGB **Destination_Matrix, RGB **Source_Matrix, long int height, long int width)
  86. {
  87.     for (int i = 0; i < height; i++)
  88.         for (int j = 0; j < width; j++)
  89.             Destination_Matrix[i][j] = Source_Matrix[i][j];
  90. }
  91. void FreeMatrix(RGB** raster, long int height)
  92. {
  93.     for (int i = 0; i < height; i++)
  94.         free(raster[i]);
  95. }
  96.  
  97. Image OpenBmpFile(const char* file_name, ErrorObject* error)
  98. {
  99.     Image image = { 0 };
  100.     FILE  *bmp_file;
  101.     fopen_s(&bmp_file, file_name, "rb");
  102.     if (bmp_file == NULL)
  103.     {
  104.         error->code = ERR_OPEN_FILE;
  105.         strcpy_s(error->message, "Can't open file ");
  106.         strcat_s(error->message, file_name);
  107.         return image;
  108.     }
  109.     BMFileHeader File_Header;
  110.     int read_count = fread(&File_Header, sizeof(BMFileHeader), 1, bmp_file);
  111.     if (read_count == 0)
  112.     {
  113.         error->code = ERR_INVALID_FILE;
  114.         strcpy_s(error->message, "Can't read File Header ");
  115.         strcat_s(error->message, file_name);    
  116.         fclose(bmp_file);
  117.         return image;
  118.     }
  119.     BMInfoHeader Info_Header;
  120.     read_count = fread(&Info_Header, sizeof(BMInfoHeader), 1, bmp_file);
  121.     if (read_count == 0)
  122.     {
  123.         error->code = ERR_INVALID_FILE;
  124.         strcpy_s(error->message, "Can't read InfoHeader ");
  125.         strcat_s(error->message, file_name);
  126.         fclose(bmp_file);
  127.         return image;
  128.     }
  129.     MatrixMalloc(&image.raster,Info_Header.Height, Info_Header.Width);
  130.     if (!image.raster)
  131.     {
  132.         error->code = ERR_MALLOC;
  133.         strcpy_s(error->message, "Can't allocate memory ");
  134.         strcat_s(error->message, file_name);
  135.         fclose(bmp_file);
  136.         return image;
  137.     }
  138.     for (int i = 0; i < Info_Header.Height; i++)
  139.         fread(image.raster[i], sizeof(RGB), Info_Header.Width, bmp_file);
  140.     for (int i = 0; i < Info_Header.Height; i++)
  141.         for (int j = 0; j < Info_Header.Width; j++)
  142.             printf("%d %d %d\t", image.raster[i][j].Red, image.raster[i][j].Green, image.raster[i][j].Blue);
  143.     fclose(bmp_file);
  144.     image.width = Info_Header.Width;
  145.     image.height = Info_Header.Height;
  146.     return image;
  147. }
  148.  
  149. int main()
  150. {
  151.     ErrorObject Error;
  152.     Image image=OpenBmpFile(File_Name, &Error);
  153.     if (Error.code > 0 && Error.code < 6)
  154.     {
  155.         printf("%s", Error.message);
  156.         system("pause>nul");
  157.         return 0;
  158.     }
  159.     system("pause");
  160.     return 0;
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement