Untitled
raw download clone
TEXT
views 15
,
size 2327 b
#include<iostream>
using namespace std;

struct Image
{
    int* pixels;
    int width;
    int height;
};

void printImage(const Image& img)
{
    for(int i=0;i<img.height;i++)
    {
        for(int j=0;j<img.width;j++)
        {
            cout << img.pixels[i*img.width + j] << " ";
        }
        cout << endl;
    }
}

Image halve(const Image& img)
{
    Image halveImg;
    halveImg.height = (img.height+1)/2;
    halveImg.width = (img.width+1)/2;
    int n = 0; // height
    int m = 0; // width
    for(int i=0;i<img.height;i++)
    {
        for(int j=0;j<img.width;j++)
        {
            if( i%2 == 0 && j%2 == 0 )
            {
                halveImg.pixels[n*halveImg.width+m] = img.pixels[i*img.width + j];
                    m++;
                if ( m == halveImg.width )
                {
                    m = 0;
                    n++;
                }
            }
        }
    }
    return halveImg;
}

void halveTest(const Image& img)
{
	Image halveImg;
    halveImg.height = (img.height+1)/2;
    halveImg.width = (img.width+1)/2;
    int n = 0; // height
    int m = 0; // width
    for(int i=0;i<img.height;i++)
    {
        for(int j=0;j<img.width;j++)
        {
            if( i%2 == 0 && j%2 == 0 )
            {
                halveImg.pixels[n*halveImg.width+m] = img.pixels[i*img.width + j];
                    m++;
                if ( m == halveImg.width )
                {
                    m = 0;
                    n++;
                }
            }
        }
    }
    printImage(halveImg);
}

void halveTestTan(const Image& img)
{
	Image copimg;
    int k = 0;
    copimg.width = (img.width + 1)/2;
    copimg.height = (img.height + 1)/2;
    for (int i = 0; i < img.height; i ++) {
        for (int j = 0; j < img.width ; j++) {
            if ((i % 2==0) && (j%2 == 0)) {
                copimg.pixels[k] = img.pixels[i*img.width + j];
                k++;
            }
        }
    }
    printImage(copimg);
}

int main()
{
    int pixels[] = {
    1, 3, 0, 5, 2,
    4, 1, 8, 3, 1,
    3, 3, 1, 3, 2
};
    Image img = {pixels, 5, 3};
    printImage(img);
    cout << endl;
    halveTest(img);
    cout << endl;
    halveTestTan(img);
    return 0;
}
close fullscreen
Login or Register to edit or fork this paste. It's free.