I'm an absolute noobie at C++ as I've only been familiar with Java programming. What I'm trying to do is to read an Image file (.bmp) into a matrix where I can perform a convolution with a 3x3 matrix (filter) on the matrix to produce a new image file with the convoluted image.
I've looked around google and managed to come up with the following implementation:
Image.h file:
// Image.h
#include <fstream> // for file I/O
using namespace std;
typedef unsigned char unchar; // Easier to understand & code.
class MImage {
public:
MImage(const char* fileName); //Constructor
~MImage(); //Deconstructor
void write(const char* fileName);
void smoothFilter(); //smoothing filer.
private:
ifstream* pInFile;
ofstream* pOutFile;
unchar imageHeaderData[1078]; //.bmp header data with offset 1078.
unchar** imageData;
unchar m_smoothFilter[3][3]; // Smoothing Filter.
unchar** filteredData;
};
Image.cpp file:
// Image.cpp
//
#ifndef _Image_h
#define _Image_h
#define WIDTH 128
#define HEIGHT 128
#include "Image.h"
#include <cmath>
#endif
using namespace std;
typedef unsigned char unchar;
//Constructor
MImage::MImage(const char* fileName){
imageData = new unchar* [HEIGHT]; // create new array size: height of image.
filteredData = new unchar* [HEIGHT];// create new array size: height of image.
for (int i = 0; i < HEIGHT; i++) {
imageData[i] = new unchar [WIDTH]; //create matrix.
filteredData[i] = new unchar [WIDTH]; //create matrix.
}
//image I/O
pInFile = new ifstream;
pInFile->open(fileName, ios::in | ios::binary); // open fileName and read as binary.
pInFile->seekg(0, ios::beg); //pos filter at beginning of image file.
pInFile->read(reinterpret_cast<char*>(imageHeaderData),1078); //read bmp header data into array.
for(int i=0; i<HEIGHT; i++) {
pInFile->read(reinterpret_cast<char*>(imageData[i]),WIDTH);//read row into each array entry.
}
pInFile->close(); //close stream.
char m_smoothFilter[3][3] = {
{1,1,1},
{1,2,1},
{1,1,1}
};
}
MImage::~MImage(){
delete pInFile;
delete pOutFile;
for(int i=0; i<HEIGHT; i++){
delete[] imageData[i];
delete[] filteredData[i];
}
delete[] imageData;
delete[] filteredData;
}
void MImage::write(const char* fileName) {
smoothFilter();
pOutFile = new ofstream;
pOutFile->open(fileName, ios::out | ios::trunc | ios::binary);
pOutFile->write(reinterpret_cast<char*>(imageHeaderData), 1078); //write header data onto output
for(int i = 0; i < HEIGHT; i++){
pOutFile->write(reinterpret_cast<char*>(filteredData[i]),WIDTH); // write new image data.
}
pOutFile->close(); //close stream
}
void MImage::smoothFilter(){
//copy input image into new image
for(int i = 0; i < HEIGHT; i++) {
strcpy(reinterpret_cast<char*>(filteredData[i]), reinterpret_cast<char*>(imageData[i]));
}
int sumOfPixels = 0;
for(int i = 1; i < HEIGHT -1; i++) {
for(int j = 1; j < WIDTH -1; j++) {
sumOfPixels = m_smoothFilter[0][0] * imageData[i+1][j-1] + // top left corner
m_smoothFilter[0][1] * imageData[i+1][j] + // top center
m_smoothFilter[0][2] * imageData[i+1][j+1] + // top right corner
m_smoothFilter[1][0] * imageData[i][j-1] + // center left
m_smoothFilter[1][1] * imageData[i][j] + // center center
m_smoothFilter[1][2] * imageData[i][j+1] + // center right
m_smoothFilter[2][0] * imageData[i-1][j-1] + // bottom left corner
m_smoothFilter[2][1] * imageData[i-1][j] + // bottom center
m_smoothFilter[2][2] * imageData[i-1][j+1]; // bottom right corner
filteredData[i][j] = (sumOfPixels / ( m_smoothFilter[0][0] + m_smoothFilter[0][1] +
m_smoothFilter[0][2] + m_smoothFilter[1][0] +
m_smoothFilter[1][1] + m_smoothFilter[1][2] +
m_smoothFilter[2][0] + m_smoothFilter[2][1] +
m_smoothFilter[2][2]));
}
}
}
Main.cpp file:
//
// Main.cpp
//
#include <iostream>
#include "Image.cpp"
#include <fstream>
using namespace std;
int main() {
MImage img("test.bmp");
img.write("output.bmp");
return 0;
}
When I try to run the following file with:
g++ Main.cpp -o main
./main
I get a segmentation:11 error and not sure what is causing this error!
Note: I have to use a pure C++ implementation so cannot use other libraries.
HELP!
EDIT:
I think the code thats giving me the segmentation error is :
for(int i = 0; i < HEIGHT; i++) {
strcpy(reinterpret_cast<char*>(filteredData[i]), reinterpret_cast<char*>(imageData[i]));
}
But not sure why!
EDIT #3:
Works after replacing above code with :
for(int i= 0; i<HEIGHT; i++) {
strncpy (reinterpret_cast<char*>(filteredData[i]),
reinterpret_cast<char*>(imageData[i]),
sizeof(reinterpret_cast<char*>(filteredData[i])));
}
replaced strcpy() with strncpy() which is much safer apparently and that removed the segmentation fault.
In the main function you include "Image.cpp" and there should be "Image.h" otherwise the compiler would recognize another definition of functions already defined in the header file
Related
I have a simple C++ application on Windows that is using DirectX to capture the screen to a bitmap vector, stored in format like this, based on WinDesktopDup sample here :
struct Bitmap {
int Width = 0;
int Height = 0;
std::vector<uint8_t> Buf;
};
I am trying to write a collection of this captures to a video file. I am trying to write to an AVI file using Windows APIs
The Visual Studio 2019 project is here
An AVI file is being generated, but it's blank and while seems to be valid, playing in Windows 11's media player, and VLC, and doesn't have any content just black screens.
The screen capture is being performed on has a resolution of 3840 x 2160. Each frame is taking about 33 MB of RAM, which is not very efficient but at this stage my goal is to get something that works. I am running as x64 and have plenty of free memory.
After the AVI header in file, the file is just full of 0 byte values:
The main section of code here:
#include "stdafx.h"
#include "WinDesktopDup.h"
#include "WriteAVI.h"
int main()
{
WinDesktopDup desktopDup;
desktopDup.Initialize();
desktopDup.CaptureNext();
const int numberOfBitmaps = 60;
const int frameRatePerSecond = 2;
Bitmap bmp[numberOfBitmaps];
for (int i = 0; i < numberOfBitmaps; i++)
{
bmp[i].Width = desktopDup.Latest.Width;
bmp[i].Height = desktopDup.Latest.Height;
bmp[i].Buf.resize(desktopDup.Latest.Buf.size());
}
int counter = 0;
bool done = false;
while (!done)
{
printf("Capturing frame %i / %i\r\n", counter, numberOfBitmaps);
desktopDup.CaptureNext();
std::copy(desktopDup.Latest.Buf.begin(), desktopDup.Latest.Buf.end(), bmp[counter].Buf.begin());
counter++;
if (counter >= numberOfBitmaps)
{
counter = 0;
done = true;
}
//if <some event triggered> done = true;
Sleep(1000 / frameRatePerSecond);
}
printf("Writing to AVI ...\r\n");
// dump bitmaps to video
wchar_t fileName[] = L"C:\\support\\test.avi";
CAVIFile aviFile(fileName, desktopDup.Latest.Width, desktopDup.Latest.Height);
BITMAPINFO inf;
memset(&inf, 0, sizeof(inf));
inf.bmiHeader.biSize = sizeof(inf.bmiHeader);
inf.bmiHeader.biWidth = desktopDup.Latest.Width;
inf.bmiHeader.biHeight = -desktopDup.Latest.Height;
inf.bmiHeader.biPlanes = 1;
inf.bmiHeader.biBitCount = 32;
inf.bmiHeader.biCompression = BI_RGB;
void* bits = nullptr;
HDC srcDC = GetWindowDC(NULL);
for (int i = 0; i < numberOfBitmaps; i++)
{
HBITMAP dib = CreateDIBSection(srcDC, &inf, 0, &bits, nullptr, 0);
memcpy(bits, bmp[i].Buf.data(), bmp[i].Width * bmp[i].Height * 4);
for (int j = 0; j < (15 / frameRatePerSecond); j++)
{
aviFile.AddFrame(dib);
}
DeleteObject(dib);
}
DeleteObject(srcDC);
}
I have an image and want to encode it with OpenH264.
So far this is the code I derived from their wiki:
#include <fstream>
#include <iterator>
#include <iostream>
#include <codec_api.h> //standard api for openh264
//additional libaries used by sample code
#include <codec_app_def.h>
#include <codec_def.h>
#include <codec_ver.h>
#include <assert.h>
#include <vector>
#include <cstring>
int main()
{
//parameter values
int width = 1920;
int height = 1080;
int framerate = 60;
int bitrate = 5000000;
int total_num = 500; //what does this value do?
//end parameter values
//Read in the File from bmp
std::vector<char> buf; //to store the image information
std::basic_ifstream<char> file("/home/megamol/Git/h264_sample/build/test.bmp", std::ios::binary); //opens bitstream to source
buf = std::vector<char>((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>()); // reads in data to the vector
std::cout << "sizeof buf: " << buf.size() << std::endl;
//Step 1: set up Encoder
ISVCEncoder* encoder_; //declaration of encoder pointer
int rv = WelsCreateSVCEncoder (&encoder_);
//Step 2: initialize with basic parameter
SEncParamBase param;
memset(¶m, 0, sizeof (SEncParamBase));
param.iUsageType = EUsageType::SCREEN_CONTENT_REAL_TIME;
param.fMaxFrameRate = framerate;
param.iPicWidth = width;
param.iPicHeight = height;
param.iTargetBitrate = bitrate; //default value of example
encoder_->Initialize(¶m);
//Step 3: set video format
int videoFormat = videoFormatI420;
encoder_->SetOption (ENCODER_OPTION_DATAFORMAT, &videoFormat);
//Step 4: encocode and store output bitstream
int frameSize = width * height * 3 / 2;
buf.resize(frameSize);
SFrameBSInfo info;
std::vector<char> compressedData;
memset (&info, 0, sizeof (SFrameBSInfo));
SSourcePicture pic;
memset (&pic, 0, sizeof (SSourcePicture));
pic.iPicWidth = width;
pic.iPicHeight = height;
pic.iColorFormat = videoFormatI420;
pic.iStride[0] = pic.iPicWidth;
pic.iStride[1] = pic.iStride[2] = pic.iPicWidth >> 1;
pic.pData[0] = reinterpret_cast<unsigned char*>(&buf[0]);
pic.pData[1] = pic.pData[0] + width * height;
pic.pData[2] = pic.pData[1] + (width * height >> 2);
//encodes the frame
rv = encoder_->EncodeFrame (&pic, &info); // encodes the Frame
//encoding done encoded Frame should be stored in &info
//begin decoding block
ISVCDecoder *pSvcDecoder;
unsigned char *pBuf= &info;
return 0;
}
I'm not entirely sure whether this is the correct usage of OpenH264 but I'm also not sure how to test it properly.
Now the code example is kind of poorly documented.
What is BufferedData buf; for example? I get that that's supposed to be the input but what is that type? Like how do I load my test.bmp as BufferedData? I don't think that I'm doing that correctly yet.
Another thing I'm pretty confused about is how do I access the output after the encoding? In the example it just says //output bitstream and nothing about saving this output anywhere. I thought the output was info like it says in the codec_api.h header file:
/**
* #brief Encode one frame
* #param kpSrcPic the pointer to the source luminance plane
* chrominance data:
* CbData = kpSrc + m_iMaxPicWidth * m_iMaxPicHeight;
* CrData = CbData + (m_iMaxPicWidth * m_iMaxPicHeight)/4;
* the application calling this interface needs to ensure the data validation between the location
* #param pBsInfo output bit stream
* #return 0 - success; otherwise -failed;
*/
virtual int EXTAPI EncodeFrame (const SSourcePicture* kpSrcPic, SFrameBSInfo* pBsInfo) = 0;
But apparently it only saves informations about the output. I'm just really confused about all of this.
Based on https://github.com/cisco/openh264/blob/master/codec/console/enc/src/welsenc.cpp
#include <codec_api.h>
#include <cassert>
#include <cstring>
#include <vector>
#include <fstream>
#include <iostream>
//Tested with OpenCV 3.3
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
using namespace std;
using namespace cv;
int main()
{
ISVCEncoder *encoder_ = nullptr;
int rv = WelsCreateSVCEncoder (&encoder_);
assert (0==rv);
assert (encoder_ != nullptr);
int width = 640;
int height = 480;
int total_num = 100;
SEncParamBase param;
memset (¶m, 0, sizeof (SEncParamBase));
param.iUsageType = CAMERA_VIDEO_REAL_TIME;
param.fMaxFrameRate = 30;
param.iPicWidth = width;
param.iPicHeight = height;
param.iTargetBitrate = 5000000;
encoder_->Initialize (¶m);
Mat image = imread("test.jpg", IMREAD_COLOR );
Mat imageResized, imageYuv, imageYuvMini;
resize(image, imageResized, Size(width, height));
Mat imageYuvCh[3], imageYuvMiniCh[3];
cvtColor(imageResized, imageYuv, cv::COLOR_BGR2YUV);
split(imageYuv, imageYuvCh);
resize(imageYuv, imageYuvMini, Size(width/2, height/2));
split(imageYuvMini, imageYuvMiniCh);
SFrameBSInfo info;
memset (&info, 0, sizeof (SFrameBSInfo));
SSourcePicture pic;
memset (&pic, 0, sizeof (SSourcePicture));
pic.iPicWidth = width;
pic.iPicHeight = height;
pic.iColorFormat = videoFormatI420;
pic.iStride[0] = imageYuvCh[0].step;
pic.iStride[1] = imageYuvMiniCh[1].step;
pic.iStride[2] = imageYuvMiniCh[2].step;
pic.pData[0] = imageYuvCh[0].data;
pic.pData[1] = imageYuvMiniCh[1].data;
pic.pData[2] = imageYuvMiniCh[2].data;
ofstream outFi;
outFi.open ("test.264", ios::out | ios::binary);
for(int num = 0; num<total_num; num++)
{
//prepare input data
rv = encoder_->EncodeFrame (&pic, &info);
assert (rv == cmResultSuccess);
if (info.eFrameType != videoFrameTypeSkip /*&& cbk != nullptr*/)
{
//output bitstream
for (int iLayer=0; iLayer < info.iLayerNum; iLayer++)
{
SLayerBSInfo* pLayerBsInfo = &info.sLayerInfo[iLayer];
int iLayerSize = 0;
int iNalIdx = pLayerBsInfo->iNalCount - 1;
do {
iLayerSize += pLayerBsInfo->pNalLengthInByte[iNalIdx];
--iNalIdx;
} while (iNalIdx >= 0);
unsigned char *outBuf = pLayerBsInfo->pBsBuf;
outFi.write((char *)outBuf, iLayerSize);
}
}
}
if (encoder_) {
encoder_->Uninitialize();
WelsDestroySVCEncoder (encoder_);
}
outFi.close();
}
I have a exercise. It says, that the C program should be able to read the information of a bitmap file and after that it should display the picture on console.
I have already written a code but when it does not work correctly.
When I debugged the code it looks like the heap is corrupted. I thinks I have a known glitch/mistake in ScanPixelline function.
I don't know how to fix it. Can someone help me to check it?
I am relatively new to C programming.
#include "stdafx.h"
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include "stdint.h"
#include "windows.h"
#pragma pack(1)
struct BMP
{
char Type[2]; //File type. Set to "BM".
int32_t Size; //Size in BYTES of the file.
int16_t Reserved1; //Reserved. Set to zero.
int16_t Reserved2; //Reserved. Set to zero.
int32_t OffSet; //Offset to the data.
int32_t headsize; //Size of rest of header. Set to 40.
int32_t Width; //Width of bitmap in pixels.
int32_t Height; // Height of bitmap in pixels.
int16_t Planes; //Number of Planes. Set to 1.
int16_t BitsPerPixel; //Number of Bits per pixels.
int32_t Compression; //Compression. Usually set to 0.
int32_t SizeImage; //Size in bytes of the bitmap.
int32_t XPixelsPreMeter; //Horizontal pixels per meter.
int32_t YPixelsPreMeter; //Vertical pixels per meter.
int32_t ColorsUsed; //Number of colors used.
int32_t ColorsImportant; //Number of "important" colors.
};
struct Color
{
unsigned char B;
unsigned char G;
unsigned char R;
};
struct ColorTable
{
Color *colors;
unsigned long length;
};
struct PixelArray
{
Color **pixels;
unsigned long rowCount;
unsigned long columnCount;
};
void readBMP(char *File_Name, BMP &a)
{
FILE *p = fopen(File_Name, "rb");
if (p == NULL)
{
printf("Can't open file!");
fclose(p);
return;
}
else
{
fread(&a, sizeof(BMP), 1, p);
}
fclose(p);
}
void Get_Inf(BMP a)
{
if (a.Type[0] != 'B' || a.Type[1] != 'M')
{
printf("This is not a BMP file");
}
else
{
printf("This is a BMP file\n");
printf("The size of this file is %lu bytes\n", a.Size);
printf("The witdth of this image is %lu pixels\n", a.Width);
printf("The height of this image is %lu pixels\n", a.Height);
printf("The number of bits per pixels in this image is %u\n", a.BitsPerPixel);
}
}
void scanBmpPixelLine(Color *&line, unsigned long length)
{
FILE *pointer_ = fopen("test.bmp", "rb");
line = new Color[length];
fread(line, sizeof(Color), sizeof(Color)*length, pointer_);
fclose(pointer_);
//file.read((char *)line, length * sizeof(Color));
}
void skipBmpPadding(char count)
{
FILE *pointer__ = fopen("test.bmp", "rb");
if (count == 0)
{
fclose(pointer__);
return;
}
char padding[3];
fread(&padding, sizeof(char), count, pointer__);
fclose(pointer__);
//file.read((char *)&padding, count);
}
void ReadPixelArray(BMP a, PixelArray &data)
{
FILE *pointer = fopen("test.bmp", "rb");
data.rowCount = a.Height;
data.columnCount = a.Width;
data.pixels = new Color*[data.rowCount];
char paddingCount = (4 - (a.Width * (a.BitsPerPixel / 8) % 4)) % 4;
fseek(pointer, 54, SEEK_SET);
for (int i = 0; i < data.rowCount; i++)
{
scanBmpPixelLine(data.pixels[data.rowCount - i - 1], a.Width);
skipBmpPadding(paddingCount);
}
}
void drawBmp(BMP a, PixelArray data)
{
HWND console = GetConsoleWindow();
HDC hdc = GetDC(console);
for (int i = 0; i < a.Height; i++)
for (int j = 0; j < a.Width; j++)
{
Color pixel = data.pixels[i][j];
SetPixel(hdc, j, i, RGB(pixel.R, pixel.G, pixel.B));
}
ReleaseDC(console, hdc);
}
void releaseBmpPixelArray(PixelArray data)
{
for (int i = 0; i < data.rowCount; i++)
delete[]data.pixels[i];
delete[]data.pixels;
}
int main()
{
char file_name[] = "test.bmp";
BMP a;
PixelArray data;
readBMP(file_name, a);
Get_Inf(a);
ReadPixelArray(a, data);
drawBmp(a, data);
releaseBmpPixelArray(data);
}
This function:
void scanBmpPixelLine(Color *&line, unsigned long length)
{
FILE *pointer_ = fopen("test.bmp", "rb");
line = new Color[length];
fread(line, sizeof(Color), sizeof(Color)*length, pointer_);
fclose(pointer_);
//file.read((char *)line, length * sizeof(Color));
}
For starters, the intent of the function appears to be to read one line of pixel data from the file. But instead, it's re-opening the file and reading from the beginning (where the header bytes are). I'm not sure if you are aware of that...
But the crash is a result of this line:
fread(line, sizeof(Color), sizeof(Color)*length, pointer_);
The second parameter, sizeof(Color), is the size of each element. The third parameter is the number of elements to read. The total bytes read from the file will be the multiplication of the second parameter by the third parameter. So you've redundantly multiplied by sizeof(Color) one too many times. The result is that it will overwrite the line buffer.
To fix, it should be:
fread(line, sizeof(Color), length, pointer_);
You probably want to pass the FILE* pointer obtained from your ReadPixelArray function into this function instead of re-opening the file for every line.
Another code review comment. You should just read the entire file into memory instead of redundantly opening and closing the file for each operation. Then parse the header and set a pointer to the first "line" after the header.
#include <iostream>
#include <fstream> // for file I/O
#define WIDTH 128
#define HEIGHT 128
#include <cmath>
using namespace std;
typedef unsigned char unchar;
class MImage
{
///////////////////////////////////////////////////////////////////////////////////////read
public:
void readimage()
{
imageData = new unchar*[HEIGHT]; // create new array size: height of image.
for (int i = 0; i < HEIGHT; i++)
{
imageData[i] = new unchar[WIDTH]; //create matrix.
}
//image I/O
pInFile = new ifstream;
pInFile->open("L.bmp", ios::in | ios::binary); // open fileName and read as binary.
pInFile->read(reinterpret_cast<char*>(imageHeaderData), 1078); //read bmp header data into array.
for (int i = 0; i < HEIGHT; i++)
{
pInFile->read(reinterpret_cast<char*>(imageData[i]), WIDTH); //read row into each array entry.
}
pInFile->close(); //close stream.
}
public:
void write()
{
//smoothFilter();
pOutFile = new ofstream;
pOutFile->open("output.bmp", ios::out | ios::binary);
pOutFile->write(reinterpret_cast<char*>(imageHeaderData), 1078); //write header data onto output
for (int i = 0; i < HEIGHT; i++)
{
pOutFile->write(reinterpret_cast<char*>(imageData[i]), WIDTH); // write new image data.
}
pOutFile->close(); //close stream
}
public:
ifstream* pInFile;
ofstream* pOutFile;
unchar imageHeaderData[1078]; //.bmp header data with offset 1078. unchar** imageData;
};
int main()
{
MImage abc;
abc.readimage();
abc.write();
return 0;
}
I am unable to read image in a 2d array so that i could do some processing on it. i have used the code above but saved file is giving error.
what i am doing is first reading a .bmp file 128x128 then saving it in an other .bmp file. but when i try to open output file it gives error "file is corrupted or large in size"
Hope it helps ;)
I skipped error checking but you should add it to the final code. For writing a .bmp image, wirte BITMAPFILEHEADER first, then BITMAPINFOHEADER and at the end the actual raw data
FILE* filePtr;
int error;
unsigned int count;
BITMAPFILEHEADER bitmapFileHeader;
BITMAPINFOHEADER bitmapInfoHeader;
int imageSize;
unsigned char* bitmapImage;
// Open the height map file in binary.
error = fopen_s(&filePtr, filename, "rb");
// Read in the file header.
count = fread(&bitmapFileHeader, sizeof(BITMAPFILEHEADER), 1, filePtr);
// Read in the bitmap info header.
count = fread(&bitmapInfoHeader, sizeof(BITMAPINFOHEADER), 1, filePtr);
// Save the dimensions of the terrain.
Width= bitmapInfoHeader.biWidth;
Height= bitmapInfoHeader.biHeight;
// Calculate the size of the bitmap image data.
imageSize = Width* Height* 3;
// Allocate memory for the bitmap image data.
bitmapImage = new unsigned char[imageSize];
// Move to the beginning of the bitmap data.
fseek(filePtr, bitmapFileHeader.bfOffBits, SEEK_SET);
// Read in the bitmap image data.
count = fread(bitmapImage, 1, imageSize, filePtr);
// Close the file.
error = fclose(filePtr);
Don't forget to delete bitmapImage or use std::vector instead
I'm getting a bytearray (32 bit or 16 bit) from a source.
If the size width is odd, the last pixel in each row needs to be dropped.
If the height is odd, the last row needs to be dropped.
If the height is negative the bitmap needs to be flipped vertically.
Here is my code so far:
m_pbmiLast = new BITMAPINFO(*m_pbmi);
m_pbmiLast->bmiHeader.biWidth = abs(m_pbmiLast->bmiHeader.biWidth) - (abs(m_pbmiLast->bmiHeader.biWidth) % 2);
m_pbmiLast->bmiHeader.biHeight = abs(m_pbmiLast->bmiHeader.biHeight) - (abs(m_pbmiLast->bmiHeader.biHeight) % 2);
int biWidth = m_pbmiLast->bmiHeader.biWidth;
int biHeight = m_pbmiLast->bmiHeader.biHeight;
int iAdjustedStride = ((((biWidth * m_pbmiLast->bmiHeader.biBitCount) + 31) & ~31) >> 3);
int iRealStride = ((((m_pbmi->bmiHeader.biWidth * m_pbmi->bmiHeader.biBitCount) + 31) & ~31) >> 3);
if (m_pbmi->bmiHeader.biHeight < 0) {
/* Copy the actual data */
int iLineOffsetSource = 0;
int iLineOffsetDest = (biHeight - 1) * iRealStride;
for (int i = 0; i < biHeight; ++i) {
memcpy(&pData[iLineOffsetDest], &m_inputBuffer[iLineOffsetSource], iAdjustedStride);
iLineOffsetSource += iRealStride;
iLineOffsetDest -= iRealStride;
}
} else {
int iLineOffset = 0;
for (int i = 0; i < biHeight; ++i) {
memcpy(&pData[iLineOffset], &m_inputBuffer[iLineOffset], iAdjustedStride);
iLineOffset += iRealStride;
}
}
It doesn't flip the bitmap, and when the bitmap is an odd width, it slants the bitmap.
Can be done like so.. I include the reading and writing just to make it an SSCCE. It has little to no error.
As for my comment about new BITMAPINFO. I was saying that you don't have to allocate such a small structure on the HEAP. Ditch the new part. The only allocation you need for a bitmap is the pixels. The header and other info does not need an allocation at all.
See the Flip function below.
#include <iostream>
#include <fstream>
#include <cstring>
#include <windows.h>
typedef struct
{
BITMAPFILEHEADER Header;
BITMAPINFO Info;
unsigned char* Pixels;
} BITMAPDATA;
void LoadBmp(const char* path, BITMAPDATA* Data)
{
std::ifstream hFile(path, std::ios::in | std::ios::binary);
if(hFile.is_open())
{
hFile.read((char*)&Data->Header, sizeof(Data->Header));
hFile.read((char*)&Data->Info, sizeof(Data->Info));
hFile.seekg(Data->Header.bfOffBits, std::ios::beg);
Data->Pixels = new unsigned char[Data->Info.bmiHeader.biSizeImage];
hFile.read((char*)Data->Pixels, Data->Info.bmiHeader.biSizeImage);
hFile.close();
}
}
void SaveBmp(const char* path, BITMAPDATA* Data)
{
std::ofstream hFile(path, std::ios::out | std::ios::binary);
if (hFile.is_open())
{
hFile.write((char*)&Data->Header, sizeof(Data->Header));
hFile.write((char*)&Data->Info, sizeof(Data->Info));
hFile.seekp(Data->Header.bfOffBits, std::ios::beg);
hFile.write((char*)Data->Pixels, Data->Info.bmiHeader.biSizeImage);
hFile.close();
}
}
void Flip(BITMAPDATA* Data)
{
unsigned short bpp = Data->Info.bmiHeader.biBitCount;
unsigned int width = std::abs(Data->Info.bmiHeader.biWidth);
unsigned int height = std::abs(Data->Info.bmiHeader.biHeight);
unsigned char* out = new unsigned char[Data->Info.bmiHeader.biSizeImage];
unsigned long chunk = (bpp > 24 ? width * 4 : width * 3 + width % 4);
unsigned char* dst = out;
unsigned char* src = Data->Pixels + chunk * (height - 1);
while(src != Data->Pixels)
{
std::memcpy(dst, src, chunk);
dst += chunk;
src -= chunk;
}
std::memcpy(dst, src, chunk); //for 24-bit.
std::swap(Data->Pixels, out);
delete[] out;
}
int main()
{
BITMAPDATA Data;
LoadBmp("C:/Users/Brandon/Desktop/Bar.bmp", &Data);
Flip(&Data);
SaveBmp("C:/Users/Brandon/Desktop/Foo.bmp", &Data);
delete[] Data.Pixels;
return 0;
}