A program (in C) to find the distance to an object using two images and computing distances accross these images (thanks to a stereoscopy algorithm).

bmp.c 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. //Note : BMP files are written from left to right but from bottom to top !
  5. struct file_header
  6. {
  7. char signature[3];
  8. int size;
  9. int reserved;
  10. int offset;
  11. };
  12. struct img_header
  13. {
  14. int headerSize;
  15. int width;
  16. int height;
  17. int planes;
  18. int depth;
  19. int compress;
  20. int imgSize;
  21. int resX;
  22. int resY;
  23. int colors;
  24. int importantColors;
  25. };
  26. struct file_header get_file_header(FILE* file)
  27. {
  28. struct file_header fh={"", 0, 0, 0};
  29. fread(&fh.signature, 2, 1, file);
  30. fread(&fh.size, 4, 1, file);
  31. fread(&fh.reserved, 4, 1, file);
  32. fread(&fh.offset, 4, 1, file);
  33. return fh;
  34. }
  35. struct img_header get_img_header(FILE* file)
  36. {
  37. struct img_header ih={0,0,0,0,0,0,0,0,0,0,0};
  38. fread(&ih.headerSize, 4, 1, file);
  39. fread(&ih.width, 4, 1, file);
  40. fread(&ih.height, 4, 1, file);
  41. fread(&ih.planes, 2, 1, file);
  42. fread(&ih.depth, 2, 1, file);
  43. fread(&ih.compress, 4, 1, file);
  44. fread(&ih.imgSize, 4, 1, file);
  45. fread(&ih.resX, 4, 1, file);
  46. fread(&ih.resY, 4, 1, file);
  47. fread(&ih.colors, 4, 1, file);
  48. fread(&ih.importantColors, 4, 1, file);
  49. return ih;
  50. }
  51. int main (int argc, char* argv[])
  52. {
  53. if(argc < 2)
  54. {
  55. fprintf(stderr, "Error : you must specify a bmp image to open.\n");
  56. return EXIT_FAILURE;
  57. }
  58. //All the variables we'll use
  59. FILE *file;
  60. const char* filename;
  61. filename = argv[1];
  62. struct file_header fileHeader;
  63. struct img_header imageHeader;
  64. int x, y, yImage;
  65. unsigned char ***image;
  66. file = fopen(filename, "rb"); //First, open the file in binary mode
  67. if(file == NULL)
  68. {
  69. fprintf(stderr, "Error : can't open the bmp image.\n");
  70. return EXIT_FAILURE;
  71. }
  72. fileHeader = get_file_header(file); //Get the file header and check it is a bmp file
  73. if(strcmp(fileHeader.signature, "BM") != 0 && strcmp(fileHeader.signature, "BA") != 0 && strcmp(fileHeader.signature, "CI") != 0 && strcmp(fileHeader.signature, "CP") != 0 && strcmp(fileHeader.signature, "IC") != 0 && strcmp(fileHeader.signature, "PT") != 0)
  74. {
  75. fprintf(stderr, "Error : This file is not a valid BMP image.\n");
  76. return EXIT_FAILURE;
  77. }
  78. imageHeader = get_img_header(file); //Get the image header
  79. if(imageHeader.compress != 0)
  80. {
  81. fprintf(stderr, "Error : The BMP file is compressed. This program can't open such files.\n");
  82. return EXIT_FAILURE;
  83. }
  84. if(imageHeader.depth != 24) //If it is not a "true-color" RGB bmp file (ie 24 bits per pixel)
  85. {
  86. fprintf(stderr, "Error : This BMP is not a standard true-color BMP file. It may be a 256 colors BMP file for example.\n");
  87. return EXIT_FAILURE;
  88. }
  89. //Allocation dynamique pour l'image
  90. if((image = malloc(sizeof(*image) * imageHeader.height)) == NULL)
  91. {
  92. perror("malloc:");
  93. return EXIT_FAILURE;
  94. }
  95. for(y = 0; y <= imageHeader.height; y++)
  96. {
  97. if((image[y] = malloc(sizeof(**image) * imageHeader.width)) == NULL)
  98. {
  99. perror("malloc:");
  100. return EXIT_FAILURE;
  101. }
  102. }
  103. for(y = 0; y <= imageHeader.height; y++)
  104. {
  105. for(x = 0; x <= imageHeader.width; x++)
  106. {
  107. if((image[y][x] = malloc(sizeof(***image) * 3)) == NULL) //image[y][x][R,G,B]
  108. {
  109. perror("malloc:");
  110. return EXIT_FAILURE;
  111. }
  112. image[y][x][0] = 0;
  113. image[y][x][1] = 0;
  114. image[y][x][2] = 0;
  115. }
  116. }
  117. fseek(file, fileHeader.offset, SEEK_SET); //We don't get all the possible headers, so go to the start of the image informations
  118. for(y = 0; y <= imageHeader.height; y++) //Get all the values for all the pixels in the image
  119. {
  120. for(x = 0; x <= imageHeader.width; x++)
  121. {
  122. yImage = imageHeader.height - y; //Due to the fact that BMP file are written from bottom to top
  123. fread(&image[yImage][x][2], 1, 1, file);
  124. fread(&image[yImage][x][1], 1, 1, file);
  125. fread(&image[yImage][x][0], 1, 1, file);
  126. }
  127. }
  128. printf("Output is : (Coordinates of the pixel) : value of each channel");
  129. for(y = 0; y <= imageHeader.height; y++)
  130. {
  131. for(x = 0; x <= imageHeader.width; x++)
  132. {
  133. printf("(%d,%d) : R = %d,G = %d,B = %d\n", x, y, image[y][x][0], image[y][x][1], image[y][x][2]);
  134. }
  135. }
  136. free(image);
  137. fclose(file);
  138. return EXIT_SUCCESS;
  139. }