BMP图像的读取与保存

需要下载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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值