需要下载EasyBMP:
1. HOGImage.h
#ifndef __HOGIMAGE_H__
#define __HOGIMAGE_H__
#include "EasyBMP.h"
namespace HOG
{
class HOGImage
{
public:
//must me uchar4
bool isLoaded;
BMP Image;
BMP SImage;
int width, height;
unsigned char* pixels;
HOGImage(const char* fileName);
HOGImage(int width, int height);
HOGImage(int width, int height, unsigned char *pixels);
int GetWidth();
int GetHeight();
void HOGSaveImage(unsigned char* pixels,char* filename,int w,int h);
void HOGDrawRect(int x,int y,int width1,int height1);
virtual ~HOGImage();
};
}
#endif /* HOGIMAGE_H_ */
2. HOGImage.cpp
/*
* HOGImage.cpp
*
* Re-Created on: Mar 03, 2014
* Author: zhangjie
*/
#include "HOGImage.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
using namespace HOG;
void ConvertTo32Bits(unsigned char*img,BMP Image,int bpp,int w,int h)
{
switch(bpp)
{
case 32:
memcpy(img,&Image,sizeof(4*w*h*sizeof(unsigned char))); break;
default:
for(int i=0;i<w;i++)
for(int j=0;j<h;j++)
{
img[j*w*4+i*4]= Image(i, j)->Red;
img[j*w*4+i*4+1]= Image(i, j)->Green;
img[j*w*4+i*4+2]= Image(i, j)->Blue;
img[j*w*4+i*4+3]=255;
}
}
}
HOGImage::HOGImage(int width, int height)
{
this->width = width;
this->height = height;
isLoaded = false;
this->pixels = (unsigned char*) malloc(sizeof(unsigned char) * 4 * width * height);
memset(this->pixels, 0, sizeof(unsigned char) * 4 * width * height);
}
HOGImage::HOGImage(int width, int height, unsigned char* pixels)
{
this->width = width;
this->height = height;
this->pixels = (unsigned char*) malloc(sizeof(unsigned char) * 4 * width * height);
memcpy(this->pixels, pixels, sizeof(unsigned char) * 4 * width * height);
isLoaded = true;
}
HOGImage::HOGImage(const char* fileName)
{
bool bLoaded = false;
unsigned char *imgBit;
Image.ReadFromFile(fileName);
int bpp;
if(&Image)
bLoaded = true;
else
bLoaded = false;
if (bLoaded)
{
width = Image.TellWidth();
height = Image.TellHeight();
bpp = Image.TellBitDepth();
imgBit=(unsigned char*) malloc(sizeof(unsigned char) * 4 * width * height);
ConvertTo32Bits(imgBit,Image,bpp,width,height);
this->pixels = (unsigned char*) malloc(sizeof(unsigned char) * 4 * width * height);
memcpy(this->pixels,imgBit,sizeof(unsigned char) * 4 * width * height);
isLoaded = true;
}
else
isLoaded = false;
}
int HOGImage::GetWidth()
{
return width;
}
int HOGImage::GetHeight()
{
return height;
}
void HOGImage::HOGSaveImage(unsigned char* pixels,char* filename,int w,int h)
{
SImage.SetBitDepth(24);
SImage.SetSize(w,h);
RGBApixel NewPixel;
for(int i=0;i<w;i++)
for(int j=0;j<h;j++)
{
NewPixel.Red =pixels[j*w*4+i*4];
NewPixel.Green=pixels[j*w*4+i*4+1];
NewPixel.Blue=pixels[j*w*4+i*4+2];
SImage.SetPixel(i,j,NewPixel);
}
SImage.WriteToFile(filename);
}
void HOGImage::HOGDrawRect(int x,int y,int width1,int height1)
{
for(int i=x;i<x+width1;i=i+width1-1)
for(int j=y;j<y+height1;j+=1)
{
if((i>=0&&i<width)&&(j>=0&&j<height))
{
pixels[j*width*4+i*4]=0;
pixels[j*width*4+i*4+1]=255;
pixels[j*width*4+i*4+2]=0;
}
}
for(int i=x+1;i<x+width1-1;i+=1)
for(int j=y;j<y+height1;j=j+height1-1)
{
if((i>=0&&i<width)&&(j>=0&&j<height))
{
pixels[j*width*4+i*4]=0;
pixels[j*width*4+i*4+1]=255;
pixels[j*width*4+i*4+2]=0;
}
}
}
HOGImage::~HOGImage()
{
//free(Image);
free(pixels);
}
#endif
BMP图像的读取与保存
最新推荐文章于 2020-05-19 11:02:42 发布