[IE C Samples] Implement bmp reader (#8848)
* Implement bmp reader * Use not os specific functions * Fix code style * Move `i` declaration from `for` loop Co-authored-by: Vladimir Dudnik <vladimir.dudnik@intel.com> Co-authored-by: Vladimir Dudnik <vladimir.dudnik@intel.com>
This commit is contained in:
co-authored by
Vladimir Dudnik
parent
ec6f57872f
commit
3f96a1bccd
@@ -2,10 +2,10 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
#
|
||||
|
||||
project(OpenCV_C_Wrapper)
|
||||
project(opencv_c_wrapper)
|
||||
set(TARGET_NAME opencv_c_wrapper)
|
||||
|
||||
file(GLOB SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)
|
||||
file(GLOB SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/*.c)
|
||||
file(GLOB HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/*.h)
|
||||
|
||||
# create library
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
#include "bmp_reader.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int readBmpImage(const char* fileName, BitMap* image) {
|
||||
FILE* input = fopen(fileName, "rb");
|
||||
|
||||
if (input == NULL) {
|
||||
printf("[BMP] file %s is not opened\n", fileName);
|
||||
return 1;
|
||||
}
|
||||
|
||||
fread(&image->header.type, 2, 1, input);
|
||||
|
||||
if (image->header.type != 'M' * 256 + 'B') {
|
||||
printf("[BMP] file is not bmp type\n");
|
||||
return 2;
|
||||
}
|
||||
|
||||
fread(&image->header.size, 4, 1, input);
|
||||
fread(&image->header.reserved, 4, 1, input);
|
||||
fread(&image->header.offset, 4, 1, input);
|
||||
|
||||
fread(&image->infoHeader, sizeof(BmpInfoHeader), 1, input);
|
||||
|
||||
image->width = image->infoHeader.width;
|
||||
image->height = image->infoHeader.height;
|
||||
|
||||
if (image->infoHeader.bits != 24) {
|
||||
printf("[BMP] 24bpp only supported. But input has: %d\n", image->infoHeader.bits);
|
||||
return 3;
|
||||
}
|
||||
|
||||
if (image->infoHeader.compression != 0) {
|
||||
printf("[BMP] compression not supported\n");
|
||||
return 4;
|
||||
}
|
||||
|
||||
int padSize = image->width & 3;
|
||||
char pad[3];
|
||||
size_t size = image->width * image->height * 3;
|
||||
|
||||
image->data = malloc(sizeof(char) * size);
|
||||
|
||||
fseek(input, image->header.offset, 0);
|
||||
|
||||
// reading by rows in invert vertically
|
||||
int i;
|
||||
for (i = 0; i < image->height; i++) {
|
||||
unsigned int storeAt = image->infoHeader.height < 0 ? i : (unsigned int)image->height - 1 - i;
|
||||
fread(image->data + image->width * 3 * storeAt, image->width * 3, 1, input);
|
||||
fread(pad, padSize, 1, input);
|
||||
}
|
||||
|
||||
fclose(input);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
|
||||
typedef struct BmpHeaderType {
|
||||
unsigned short type; /* Magic identifier */
|
||||
unsigned int size; /* File size in bytes */
|
||||
unsigned int reserved;
|
||||
unsigned int offset; /* Offset to image data, bytes */
|
||||
} BmpHeader;
|
||||
|
||||
typedef struct BmpInfoHeaderType {
|
||||
unsigned int size; /* Header size in bytes */
|
||||
int width, height; /* Width and height of image */
|
||||
unsigned short planes; /* Number of colour planes */
|
||||
unsigned short bits; /* Bits per pixel */
|
||||
unsigned int compression; /* Compression type */
|
||||
unsigned int imagesize; /* Image size in bytes */
|
||||
int xresolution, yresolution; /* Pixels per meter */
|
||||
unsigned int ncolours; /* Number of colours */
|
||||
unsigned int importantcolours; /* Important colours */
|
||||
} BmpInfoHeader;
|
||||
|
||||
typedef struct BitMapType {
|
||||
BmpHeader header;
|
||||
BmpInfoHeader infoHeader;
|
||||
int width, height;
|
||||
unsigned char* data;
|
||||
} BitMap;
|
||||
|
||||
int readBmpImage(const char* fileName, BitMap* image);
|
||||
@@ -4,10 +4,24 @@
|
||||
|
||||
#include "opencv_c_wrapper.h"
|
||||
|
||||
extern "C" {
|
||||
#include "bmp_reader.h"
|
||||
}
|
||||
|
||||
#ifndef USE_OPENCV
|
||||
|
||||
int image_read(const char* img_path, c_mat_t* img) {
|
||||
return -1;
|
||||
BitMap bmp;
|
||||
int retCode = readBmpImage(img_path, &bmp);
|
||||
|
||||
img->mat_data = bmp.data;
|
||||
img->mat_data_size = bmp.width * bmp.height * 3;
|
||||
img->mat_width = bmp.width;
|
||||
img->mat_height = bmp.height;
|
||||
img->mat_channels = 3;
|
||||
img->mat_type = bmp.header.type;
|
||||
|
||||
return retCode;
|
||||
}
|
||||
int image_resize(const c_mat_t* src_img, c_mat_t* dst_img, const int width, const int height) {
|
||||
return -1;
|
||||
@@ -16,7 +30,8 @@ int image_save(const char* img_path, c_mat_t* img) {
|
||||
return -1;
|
||||
}
|
||||
int image_free(c_mat_t* img) {
|
||||
return -1;
|
||||
delete img->mat_data;
|
||||
return 0;
|
||||
}
|
||||
int image_add_rectangles(c_mat_t* img, rectangle_t rects[], int classes[], int num, int thickness) {
|
||||
return -1;
|
||||
|
||||
Reference in New Issue
Block a user