mardi 3 mars 2020

C++11 multithreading version after sequential code in C++ [closed]

I have already written code in a Sequential version for image processing; however I now wish to write a version using C++11 multithreading code to make it more efficient.

Does anyone know if there is a simple way of doing this? - Whether I need to just include extra code at the end

Any help would be greatly appreciated - or any recommended tutorials would be great also.

This is the sequential code:

   int main()
{
    int nt = task_scheduler_init::default_num_threads();
    task_scheduler_init T(nt);

    //Part 1 (Image Comparison): -----------------DO NOT REMOVE THIS COMMENT----------------------------//

    //Load 2 image pairs 
    //inputting first image pair
    //setup and load input image dataset -- copied 
    fipImage inputTop1;
    inputTop1.load("render_top_1.png");

    fipImage inputTop2;
    inputTop2.load("render_top_2.png");

    //loading second image pair
    fipImage inputBottom1;
    inputBottom1.load("render_bottom_1.png");

    fipImage inputBottom2;
    inputBottom2.load("render_bottom_2.png");

    unsigned int width = inputTop1.getWidth();
    unsigned int height = inputTop1.getHeight();

//  unsigned int width = inputBottom1.getWidth();
// unsigned int height = inputBottom1.getHeight();

    //not sure if i need this?  
    float pixels = height * width;

    //Outputting pixels - setting up output image array 
    fipImage renderTopImage = fipImage(FIT_BITMAP, width, height, inputTop1.getBitsPerPixel());
    //OR fipImage renderTopImage = fipImage(FIT_BITMAP, width, height, 24);
    fipImage renderBottomImage = fipImage(FIT_BITMAP, width, height, inputBottom1.getBitsPerPixel());
    //OR fipImage renderBottomImage = fipImage(FIT_BITMAP, width, height, 24);

    RGBQUAD* settingColour = new RGBQUAD();
    RGBQUAD* pair1 = new RGBQUAD();
    RGBQUAD* pair2 = new RGBQUAD();
    BYTE* renderTop1Pixels = inputTop1.accessPixels();
    BYTE* renderTop2Pixels = inputTop2.accessPixels();
    BYTE* renderBottom1Pixels = inputBottom1.accessPixels();
    BYTE* renderBottom2Pixels = inputBottom2.accessPixels();

    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            //Comparing first image pairs render_top_1 and render_top_2
            inputTop1.getPixelColor(x, y, pair1);
            inputTop2.getPixelColor(x, y, pair2);

            if (pair1->rgbRed == pair2->rgbRed && pair1->rgbGreen == pair2->rgbGreen && pair1->rgbBlue == pair2->rgbBlue) {
                settingColour->rgbRed = 0;
                settingColour->rgbGreen = 0;
                settingColour->rgbBlue = 0;
            }
            else
            {
                settingColour->rgbRed = 255;
                settingColour->rgbGreen = 255;
                settingColour->rgbBlue = 255;
            }

            renderTopImage.setPixelColor(x, y, settingColour);
            inputBottom1.getPixelColor(x, y, pair1);
            inputBottom2.getPixelColor(x, y, pair2);

            //Comparing bottom image pairs render_bottom_1 and render_bottom_2 
            //if the pixels are the same then set them to black 
            //else set them to white 
            if (pair1->rgbRed == pair2->rgbRed && pair1->rgbGreen == pair2->rgbGreen && pair1->rgbBlue == pair2->rgbBlue) {
                settingColour->rgbRed = 0;
                settingColour->rgbGreen = 0;
                settingColour->rgbBlue = 0;
            }
            else
            {
                settingColour->rgbRed = 255;
                settingColour->rgbGreen = 255;
                settingColour->rgbBlue = 255;
            }
            //setting pixel colour
            renderBottomImage.setPixelColor(x, y, settingColour);

        }

        renderTop1Pixels += inputTop1.getScanWidth();
        renderTop2Pixels += inputTop2.getScanWidth();
        renderBottom1Pixels += inputBottom1.getScanWidth();
        renderBottom2Pixels += inputBottom2.getScanWidth();
    }

    renderTopImage.save("stage1_top.png");
    renderBottomImage.save("stage1_bottom.png");

    //Combining both image pairs 
    BYTE* renderPair1Pixels = renderTopImage.accessPixels();
    BYTE* renderPair2Pixels = renderBottomImage.accessPixels();
    fipImage composite = fipImage(FIT_BITMAP, width, height, inputBottom1.getBitsPerPixel()); //change composite word? 

    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            renderTopImage.getPixelColor(x, y, pair1);
            renderBottomImage.getPixelColor(x, y, pair2);
            //Ensuring there is an equal split of 50% of each pixel value 
            settingColour->rgbRed = (pair1->rgbRed + pair2->rgbRed) * 0.5;
            settingColour->rgbGreen = (pair1->rgbGreen + pair2->rgbGreen) * 0.5;
            settingColour->rgbBlue = (pair1->rgbBlue + pair2->rgbBlue) * 0.5;
            composite.setPixelColor(x, y, settingColour);
        }

        renderPair1Pixels += renderTopImage.getScanWidth();
        renderPair2Pixels += renderBottomImage.getScanWidth();
    }
    //saving the combined image 
    composite.save("stage1_combined.png");

Aucun commentaire:

Enregistrer un commentaire