0

I am trying implement below C# code in C++ C#:

 Bitmap rawInput = new Bitmap(opf.FileName);
Bitmap videoBuffer = new Bitmap(1280,720);

//BitmapData imageLock = videoBuffer.LockBits(new Rectangle(0, 0, 1280, 720), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); BitmapData imageLock = videoBuffer.LockBits(new Rectangle(0, 0, videoBuffer.Width, videoBuffer.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);

t.SetData(imageLock.Scan0, imageLock.Stride, imageLock.Width, imageLock.Height);

C++: I followed this and I am passing

I am passing unsigned_char received from

std::vector<char> readBMP(const std::string& file)
{
    static constexpr size_t HEADER_SIZE = 54;

    std::ifstream bmp(file, std::ios::binary);

    std::array<char, HEADER_SIZE> header;
    bmp.read(header.data(), header.size());

    auto fileSize = *reinterpret_cast<uint32_t*>(&header[2]);
    auto dataOffset = *reinterpret_cast<uint32_t*>(&header[10]);
    auto width = *reinterpret_cast<uint32_t*>(&header[18]);
    auto height = *reinterpret_cast<uint32_t*>(&header[22]);
    auto depth = *reinterpret_cast<uint16_t*>(&header[28]);

    std::cout << "fileSize: " << fileSize << std::endl;
    std::cout << "dataOffset: " << dataOffset << std::endl;
    std::cout << "width: " << width << std::endl;
    std::cout << "height: " << height << std::endl;
    std::cout << "depth: " << depth << "-bit" << std::endl;


    std::vector<char> img(dataOffset - HEADER_SIZE);
    bmp.read(img.data(), img.size());

    auto dataSize = ((width * 3 + 3) & (~3)) * height; //The width times 3 is for the 3 8 R G and B byte values, +3, and then trimmed using mod 3 to be a multiple of 3 bytes after the calulation
    img.resize(dataSize);//
    bmp.read(img.data(), img.size());

    char temp = 0

;

    //for (auto i = dataSize - 4; i >= 0; i -= 3)
    //{
    //    temp = img[i];
    //    img[i] = img[i + 2];
    //    img[i + 2] = temp;

    //   // std::cout << "R: " << int(img[i] & 0xff) << " G: " << int(img[i + 1] & 0xff) << " B: " << int(img[i + 2] & 0xff) << std::endl;
    //}

    return img;
}

#include <stdio.h>

const int bytesPerPixel = 3; /// red, green, blue
const int fileHeaderSize = 14;
const int infoHeaderSize = 40;

void generateBitmapImage(unsigned char *image, int height, int width, char* imageFileName);
unsigned char* createBitmapFileHeader(int height, int width, int paddingSize);
unsigned char* createBitmapInfoHeader(int height, int width);


int main(){
    int height = 341;
    int width = 753;
    unsigned char image[height][width][bytesPerPixel];
    char* imageFileName = "bitmapImage.bmp";

    int i, j;
    for(i=0; i<height; i++){
        for(j=0; j<width; j++){
            image[i][j][2] = (unsigned char)((double)i/height*255); ///red
            image[i][j][1] = (unsigned char)((double)j/width*255); ///green
            image[i][j][0] = (unsigned char)(((double)i+j)/(height+width)*255); ///blue
        }
    }

    generateBitmapImage((unsigned char *)image, height, width, imageFileName);
    printf("Image generated!!");
}




   void generateBitmapImage(unsigned char *image, int height, int width, char* imageFileName){
    
        unsigned char padding[3] = {0, 0, 0};
        int paddingSize = (4 - (width*bytesPerPixel) % 4) % 4;
    
        unsigned char* fileHeader = createBitmapFileHeader(height, width, paddingSize);
        unsigned char* infoHeader = createBitmapInfoHeader(height, width);
    
        FILE* imageFile = fopen(imageFileName, "wb");
    
        fwrite(fileHeader, 1, fileHeaderSize, imageFile);
        fwrite(infoHeader, 1, infoHeaderSize, imageFile);
    
        int i;
        for(i=0; i<height; i++){
            fwrite(image+(i*width*bytesPerPixel), bytesPerPixel, width, imageFile);
            fwrite(padding, 1, paddingSize, imageFile);
        }
    
        fclose(imageFile);
    }
    
    unsigned char* createBitmapFileHeader(int height, int width, int paddingSize){
        int fileSize = fileHeaderSize + infoHeaderSize + (bytesPerPixel*width+paddingSize) * height;
    
        static unsigned char fileHeader[] = {
            0,0, /// signature
            0,0,0,0, /// image file size in bytes
            0,0,0,0, /// reserved
            0,0,0,0, /// start of pixel array
        };
    
        fileHeader[ 0] = (unsigned char)('B');
        fileHeader[ 1] = (unsigned char)('M');
        fileHeader[ 2] = (unsigned char)(fileSize    );
        fileHeader[ 3] = (unsigned char)(fileSize>> 8);
        fileHeader[ 4] = (unsigned char)(fileSize>>16);
        fileHeader[ 5] = (unsigned char)(fileSize>>24);
        fileHeader[10] = (unsigned char)(fileHeaderSize + infoHeaderSize);
    
        return fileHeader;
    }
    
    unsigned char* createBitmapInfoHeader(int height, int width){
        static unsigned char infoHeader[] = {
            0,0,0,0, /// header size
            0,0,0,0, /// image width
            0,0,0,0, /// image height
            0,0, /// number of color planes
            0,0, /// bits per pixel
            0,0,0,0, /// compression
            0,0,0,0, /// image size
            0,0,0,0, /// horizontal resolution
            0,0,0,0, /// vertical resolution
            0,0,0,0, /// colors in color table
            0,0,0,0, /// important color count
        };
    
        infoHeader[ 0] = (unsigned char)(infoHeaderSize);
        infoHeader[ 4] = (unsigned char)(width    );
        infoHeader[ 5] = (unsigned char)(width>> 8);
        infoHeader[ 6] = (unsigned char)(width>>16);
        infoHeader[ 7] = (unsigned char)(width>>24);
        infoHeader[ 8] = (unsigned char)(height    );
        infoHeader[ 9] = (unsigned char)(height>> 8);
        infoHeader[10] = (unsigned char)(height>>16);
        infoHeader[11] = (unsigned char)(height>>24);
        infoHeader[12] = (unsigned char)(1);
        infoHeader[14] = (unsigned char)(bytesPerPixel*8);
    
        return infoHeader;
    }
}

int main()
{
std::vector< char>v = readBMP("\URI_List.bmp");
    char* c = v.data();



unsigned char* t =(unsigned char*) v.data();
const int height = 720;
const int width = 1280;
char* imageFileName =(char*) "bitmapImage.bmp";
 int i, j;
        for (i = 0; i < height; i++) {
            for (j = 0; j < width; j++) {
                image[i][j][2] = (unsigned char)((double)i / height * 255); ///red
                image[i][j][1] = (unsigned char)((double)j / width * 255); ///green
                image[i][j][0] = (unsigned char)(((double)i + j) / (height + width) * 255); ///blue
            }
        }
    
        generateBitmapImage((unsigned char*)t, height, width, imageFileName);
        printf("Image generated!!");

But generated image is all distorted. I need to implement the same way as C# code.

Another try

bool Resample(int newWidth, int newHeight, char * data)
    {
        int _width=500;
        int _height=289;
       unsigned char* _data;

        std::vector< char>v = readBMP("t1_24.bmp");
         _data = (unsigned char*) v.data();
        //_data = new unsigned char[GetByteCount()];
        if (_data == NULL) return false;
        //
        // Get a new buuffer to interpolate into
        //unsigned char* newData = new unsigned char[newWidth * newHeight * 3];

        unsigned char* newData = (unsigned char*)data;

        double scaleWidth = (double)newWidth / (double)_width;
        double scaleHeight = (double)newHeight / (double)_height;

        for (int cy = 0; cy < newHeight; cy++)
        {
            for (int cx = 0; cx < newWidth; cx++)
            {
                int pixel = (cy * (newWidth * 3)) + (cx * 3);
                int nearestMatch = (((int)(cy / scaleHeight) * (_width * 3)) + ((int)(cx / scaleWidth) * 3));

                newData[pixel] = _data[nearestMatch];
                newData[pixel + 1] = _data[nearestMatch + 1];
                newData[pixel + 2] = _data[nearestMatch + 2];
            }
        }

        //
        delete[] _data;
        _data = newData;
        _width = newWidth;
        _height = newHeight;

        write("img_test_24bits.bmp");
        return true;
    }
user2801184
  • 329
  • 7
  • 27
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173) – Quimby Jul 23 '20 at 16:36
  • Note: `auto fileSize = *reinterpret_cast(&header[2]);` is firmly in the "Might work" camp, but no guarantees. This is violating [Strict Aliasing.](https://stackoverflow.com/questions/98650/what-is-the-strict-aliasing-rule) – user4581301 Jul 23 '20 at 16:37
  • Thanks for StrictAliasing concept.Let me know if you see anything else wrong. – user2801184 Jul 23 '20 at 17:24
  • Tried another way bool resample still failing – user2801184 Jul 24 '20 at 04:17
  • distorted how? share image. beware BMP is aligning the ScanLines to some values ... meaning they have usually more bytes then `width*bpp/8` making your index/corrdinate conversion computations wrong... the alignment size is stored in the BMP header...Also hope you have uncompressed 24bpp BMP files for testing... see [Importing BMP file in Turboc++ issue:BMP file is not being displayed properly in the output screen](https://stackoverflow.com/a/55277088/2521214) and [BMP fileformat](https://en.wikipedia.org/wiki/BMP_file_format) – Spektre Jul 24 '20 at 08:56
  • I am opening the .bmp with paint.I am looking into http://www.cplusplus.com/forum/general/2615/ but I am getting errors. – user2801184 Jul 24 '20 at 15:17

0 Answers0