void createMasks(const RGBImage& img) { cout << "creating masks ..." << endl; int w = img.width(), h = img.height(); GrayScaleImagef I(w, h); for(int i=0;i<h;i++) { for(int j=0;j<w;j++) { RGBImage::pixel_t pix = img.getPixel(j, i); if( pix.r > 0 ) { I.setPixel(j, i, 1.0); //cout << (int)pix.r << endl; } else { //cout << (int)pix.r << endl; I.setPixel(j, i, 0.0); } } } float invSamples = 1.0 / (samples * samples); float step = 1.0 / samples; // create a set of masks for(int i=0;i<256;i++) { cout << "level " << i << endl; float maskSize = i+1; float topY = 0.5 * (h - maskSize); float bottomY = topY + maskSize; float leftX = topY; float rightX = bottomY; GrayScaleImagef m(w, h); for(int i=0;i<w;i++) { int y = i; for(int j=0;j<h;j++) { int x = j; //cout << i << "," << j << endl; int cnt = 0; for(int ny=0;ny<samples;ny++) { float yy = ny * step + y; float yyy =(yy-topY) / maskSize; for(int nx=0;nx<samples;nx++) { float xx = nx * step + x; float xxx = (xx-leftX) / maskSize; if( xxx >= 0 && xxx < 1.0 && yyy >= 0 && yyy < 1.0 ) { GrayScaleImage::pixel_t pix = I.sample(xxx*(w-1), yyy*(h-1)); cnt += pix; } } } m.setPixel(j, i, cnt * invSamples); } } masks.push_back(m); } cout << "masks created!" << endl; }
void fillCircle_jitter( const RGB& c, const RGB& cb, int centerX, int centerY, int radius ) { const int N = 4; float h = 1.0f / N; int radius2 = radius * radius; for(int y = 0; y < img.height() ; y++) { for(int x = 0; x < img.width(); x++) { // for this pixel, sample N^2 times int cnt = 0; float dy = y - centerY + 0.5 * h; for(int i=0;i<N;i++) { float dx = x - centerX + 0.5 * h; for(int j=0;j<N;j++) { float rr = dx * dx + dy * dy; if( rr <= radius2 ) cnt++; dx += h; } dy += h; } float t = cnt / (float)(N * N); img.setPixel(x, y, interpolate(c, cb, t)); } } }
void safePixelDraw(RGBImage &image, const double dx, const double dy, const RGB color) { int x = (int) std::round(dx); int y = (int) std::round(dy); if (x > -1 && x < image.getWidth() && y > -1 && y < image.getHeight()) { image.setPixel(x, y, color); } }
static void windowDisplay(void) { glClear(GL_COLOR_BUFFER_BIT); glRasterPos2i(0,0); glPixelStorei(GL_UNPACK_ALIGNMENT,1); glDrawPixels(img.width(), img.height(), GL_RGB, GL_UNSIGNED_BYTE, img.raw_data()); glFlush(); }
RGBImage * ImageFactory::newRGBImage(const RGBImage &other) { RGBImage * image = ImageFactory::newRGBImage(other.getWidth(), other.getHeight()); int size = other.getHeight() * other.getWidth(); for (int i = 0; i < size; i++) { image->setPixel(i, other.getPixel(i)); } return image; }
RGBImageStudent::RGBImageStudent(const RGBImage &other) : RGBImage(other.getWidth(), other.getHeight()), pixelMap(nullptr) { const int SIZE = other.getSize(); if (SIZE > 0) { pixelMap = new RGB[SIZE]; for (int i = 0; i < SIZE; i++) { pixelMap[i] = other.getPixel(i); } } }
RGBImage::RGBImage(const RGBImage& i) : RGBAlgorithm( i.doc()) , m_filename(i.filename()) , m_animationStyle(i.animationStyle()) , m_xOffset(i.xOffset()) , m_yOffset(i.yOffset()) { reloadImage(); }
void RGBMatrixEditor::slotImageEdited() { if (m_matrix->algorithm() != NULL && m_matrix->algorithm()->type() == RGBAlgorithm::Image) { RGBImage* algo = static_cast<RGBImage*> (m_matrix->algorithm()); Q_ASSERT(algo != NULL); algo->setFilename(m_imageEdit->text()); slotRestartTest(); } }
void RGBMatrixEditor::slotImageAnimationActivated(const QString& text) { if (m_matrix->algorithm() != NULL && m_matrix->algorithm()->type() == RGBAlgorithm::Image) { RGBImage* algo = static_cast<RGBImage*> (m_matrix->algorithm()); Q_ASSERT(algo != NULL); algo->setAnimationStyle(RGBImage::stringToAnimationStyle(text)); slotRestartTest(); } }
QString RGBMatrixEditor::algoImagePath() const { if (m_matrix != NULL && m_matrix->algorithm() != NULL && m_matrix->algorithm()->type() == RGBAlgorithm::Image) { RGBImage* algo = static_cast<RGBImage*> (m_matrix->algorithm()); return algo->filename(); } return QString(); }
void RGBImageStudent::set(const RGBImage &other) { const int SIZE = other.getSize(); RGBImage::set(other.getWidth(), other.getHeight()); if (SIZE > 0) { delete[] pixelMap; pixelMap = new RGB[SIZE]; for (int i = 0; i < SIZE; i++) { pixelMap[i] = other.getPixel(i); } } }
void HereBeDragons::ToStandInThyAffairsFallByThySide(const RGBImage &src, cv::Mat &dst) { int w = src.getWidth(); int h = src.getHeight(); dst.create(h, w, CV_8UC3); for (int x = 0; x < dst.cols; x++) { for (int y = 0; y < dst.rows; y++) { RGB color = src.getPixel(x, y); dst.at<cv::Vec3b>(y, x) = cv::Vec3b(color.b, color.g, color.r); } } }
void fillCircle( const RGB& c, int centerX, int centerY, int radius ) { int radius2 = radius * radius; for(int y = 0; y < img.height() ; y++) { for(int x = 0; x < img.width(); x++) { int dx = x - centerX; int dy = y - centerY; int rr = dx * dx + dy * dy; if( rr > radius2 ) continue; img.setPixel(x, y, c); } } }
RGBImage* Texture::createImage( unsigned char* Data, unsigned int width, unsigned int height ) { // create CPU accessible image RGBImage* pImage = new RGBImage(width, height); assert(pImage); for( unsigned int i=0; i<height; i++) for (unsigned int j = 0; j<width; j++) { Color c( (float)*(Data)/255.0f, (float)*(Data+1)/255.0f, (float)*(Data+2)/255.0f); pImage->setPixelColor(j, i, c); Data+=3; } return pImage; }
void saveImage(const RGBImage& img, const string& filename) { int w = img.width(); int h = img.height(); QImage I(w, h, QImage::Format_ARGB32); for(int y=0;y<h;y++) { for(int x=0;x<w;x++) { RGBImage::pixel_t pix = img.getPixel(x, y); I.setPixel(x, y, qRgb(pix.r, pix.g, pix.b)); } } I.save(filename.c_str()); }
void SimpleRayTracer::traceScene(const Scene& SceneModel, RGBImage& Image) { //TODO Camera eye(-8.0f, 1.0f, 1.0f, 0.75f, 640.0f, 480.0f); Color pColor; Vector ray; for (unsigned int y = 0; y < Image.height(); y++) { for (unsigned int x = 0; x < Image.width(); x++) { ray = eye.generateRay(x, y); pColor = trace(SceneModel, eye.Position(), ray, m_MaxDepth); Image.setPixelColor(x, y, pColor); } } }
void RGBMatrixEditor::updateExtraOptions() { if (m_matrix->algorithm() == NULL || m_matrix->algorithm()->type() == RGBAlgorithm::Script || m_matrix->algorithm()->type() == RGBAlgorithm::Audio || m_matrix->algorithm()->type() == RGBAlgorithm::Plain) { m_textGroup->hide(); m_imageGroup->hide(); m_offsetGroup->hide(); } else if (m_matrix->algorithm()->type() == RGBAlgorithm::Image) { m_textGroup->hide(); m_imageGroup->show(); m_offsetGroup->show(); RGBImage* image = static_cast<RGBImage*> (m_matrix->algorithm()); Q_ASSERT(image != NULL); m_imageEdit->setText(image->filename()); int index = m_imageAnimationCombo->findText(RGBImage::animationStyleToString(image->animationStyle())); if (index != -1) m_imageAnimationCombo->setCurrentIndex(index); m_xOffsetSpin->setValue(image->xOffset()); m_yOffsetSpin->setValue(image->yOffset()); } else if (m_matrix->algorithm()->type() == RGBAlgorithm::Text) { m_textGroup->show(); m_offsetGroup->show(); m_imageGroup->hide(); RGBText* text = static_cast<RGBText*> (m_matrix->algorithm()); Q_ASSERT(text != NULL); m_textEdit->setText(text->text()); int index = m_animationCombo->findText(RGBText::animationStyleToString(text->animationStyle())); if (index != -1) m_animationCombo->setCurrentIndex(index); m_xOffsetSpin->setValue(text->xOffset()); m_yOffsetSpin->setValue(text->yOffset()); } }
RGBImage loadImage(const string& filename) { cout << "loading image " << filename << endl; QImage img(filename.c_str()); int w = img.width(); int h = img.height(); RGBImage I(w, h); for(int y=0;y<h;y++) { for(int x=0;x<w;x++) { QRgb pix = img.pixel(x, y); RGBPixel c(qRed(pix), qGreen(pix), qBlue(pix)); I.setPixel(x, y, c); } } cout << "done." << endl; return I; }
void RGBMatrixEditor::setAlgoImagePath(QString path) { if (m_matrix != NULL && m_matrix->algorithm() != NULL && m_matrix->algorithm()->type() == RGBAlgorithm::Image) { RGBImage* algo = static_cast<RGBImage*> (m_matrix->algorithm()); if (path.startsWith("file:")) path = QUrl(path).toLocalFile(); if (algo->filename() == path) return; QMutexLocker algorithmLocker(&m_matrix->algorithmMutex()); algo->setFilename(path); emit algoImagePathChanged(path); } }
void fillBlock(int x0, int y0, int x1, int y1, const RGBImage& img, RGBImage& I) { // compute average color float rSum = 0, gSum = 0, bSum = 0; for(int y=y0;y<y1;y++) { for(int x=x0;x<x1;x++) { RGBImage::pixel_t pix = img.getPixel(x, y); rSum += pix.r; gSum += pix.g; bSum += pix.b; } } float H = y1 - y0; float W = x1 - x0; float invA = 1.0 / (H * W); // compute average brightness float rAvg = rSum * invA, gAvg = gSum * invA, bAvg = bSum * invA; int lev = (int)Utils::clamp<float>(0.2989*rAvg + 0.5870*gAvg + 0.1140*bAvg, 128.f, 255.f); const GrayScaleImagef& m = masks[lev]; float hFactor = (m.height() - 1) / H; float wFactor = (m.width() - 1) / W; float invSamples = 1.0 / (samples * samples); float step = 1.0 / samples; // apply the mask for(int y=y0, i=0;y<y1;y++,i++) { float yy = (y - y0); for(int x=x0, j=0;x<x1;x++, j++) { float xx = (x - x0); float mVal = 0; for(int ny=0;ny<samples;ny++) { float yyy = yy + ny * step; for(int nx=0;nx<samples;nx++) { float xxx = xx + nx * step; mVal += m.sample(xxx * wFactor, yyy * hFactor); } } mVal *= invSamples; //cout << mVal << endl; // color I.setPixel(x, y, RGBPixel(rAvg, gAvg, bAvg) * mVal); } } }
void RGBMatrixEditor::slotOffsetSpinChanged() { if (m_matrix->algorithm() != NULL && m_matrix->algorithm()->type() == RGBAlgorithm::Text) { RGBText* algo = static_cast<RGBText*> (m_matrix->algorithm()); Q_ASSERT(algo != NULL); algo->setXOffset(m_xOffsetSpin->value()); algo->setYOffset(m_yOffsetSpin->value()); slotRestartTest(); } if (m_matrix->algorithm() != NULL && m_matrix->algorithm()->type() == RGBAlgorithm::Image) { RGBImage* algo = static_cast<RGBImage*> (m_matrix->algorithm()); Q_ASSERT(algo != NULL); algo->setXOffset(m_xOffsetSpin->value()); algo->setYOffset(m_yOffsetSpin->value()); slotRestartTest(); } }
void RGBMatrixEditor::slotImageButtonClicked() { if (m_matrix->algorithm() != NULL && m_matrix->algorithm()->type() == RGBAlgorithm::Image) { RGBImage* algo = static_cast<RGBImage*> (m_matrix->algorithm()); Q_ASSERT(algo != NULL); QString path = algo->filename(); path = QFileDialog::getOpenFileName(this, tr("Select image"), path, "Images (*.jpg *.xpm *.png *.gif)"); if (path.isEmpty() == false) { algo->setFilename(path); m_imageEdit->setText(path); slotRestartTest(); } } }
void HereBeDragons::HeIsContentedThyPoorDrudgeToBe(const cv::Mat &source, RGBImage &dst) { int type = source.type(); if (type != CV_8UC3) { // throw std::exception("OpenCV Mat source image not of type CV_8UC3!"); throw std::exception{}; } dst.set(source.cols, source.rows); for (int x = 0; x < source.cols; x++) { for (int y = 0; y < source.rows; y++) { cv::Vec3b raw = source.at<cv::Vec3b>(y, x); dst.setPixel(x, y, RGB{ raw[2], raw[1], raw[0] }); } } //RGBImage * image = new RGBImage(source.cols, source.rows); //int dataLenght = source.cols * source.rows * source.elemSize(); //std::copy(source.data, source.data + dataLenght, (unsigned char *)image->data); }
// ============================================================================= // OpenGL Display and Mouse Processing Functions. // // You can read up on OpenGL and modify these functions, as well as the commands // in main(), to perform more sophisticated display or GUI behavior. This code // will service the bare minimum display needs for most assignments. // ============================================================================= static void windowResize(int w, int h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0,(w/2),0,(h/2),0,1); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); img.resize(w, h); setPixels( option ); }
void fillMandelbrot( const RGB& c1, const RGB& c2, const RGB& c3, int iters ) { int centerX = img.width() / 2; int centerY = img.height() / 2; const int THRES = 1e6; for(int y = 0; y < img.height() ; y++) { for(int x = 0; x < img.width(); x++) { complex<float> z((x-centerX)/(float)img.width() * 3.0 - 0.25, (y-centerY)/(float)img.height() * 3.0); complex<float> c = z; float val = std::abs(z); int i=0; while( i++ < iters && val < THRES ) { z = z * z + c; val = std::abs(z); } float t = i / (float)iters; t = powf(t, 0.25); RGB color; if( t >= 1.0 ) color = c1; else color = interpolate(c2, c3, t); img.setPixel(x, y, color); } } }
RGBImage halftone(const RGBImage& img, int blockSize) { int w = img.width(), h = img.height(); RGBImage I(w, h); int yBlocks = ceil(h / (float)blockSize); int xBlocks = ceil(w / (float)blockSize); cout << "blocks: " << yBlocks << "x" << xBlocks << endl; for(int i=0;i<yBlocks;i++) { int y0 = i * blockSize; int y1 = Utils::clamp((i+1) * blockSize, 0, h); for(int j=0;j<xBlocks;j++) { int x0 = j * blockSize; int x1 = Utils::clamp((j+1) * blockSize, 0, w); fillBlock(x0, y0, x1, y1, img, I); } } return I; }
void LuminosityAlgorithm::doAlgorithm(const RGBImage& input, IntensityImage& output) { // Check image size if (input.getWidth() != output.getWidth() || input.getHeight() != output.getHeight()) { output.set(input.getWidth(), input.getHeight()); } // Lunimosity Algorithm defined as // Gray = (Red * 0.2126 + Green * 0.7152 + Blue * 0.0722) for (int i = 0; i < input.getWidth()*input.getHeight(); i++) { RGB pixel = input.getPixel(i); output.setPixel(i, pixel.r * 0.2126 + pixel.g * 0.7152 + pixel.b * 0.0722); } }
void HereBeDragons::ButRisingAtThyNameDothPointOutThee(RGBImage &image, const Point2D<double> start, const Point2D<double> end, const RGB color) { //Bresenham's line algorithm //http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#C.2B.2B double x1 = std::round(start.x); double y1 = std::round(start.y); double x2 = std::round(end.x); double y2 = std::round(end.y); const bool steep = (fabs(y2 - y1) > fabs(x2 - x1)); if (steep) { std::swap(x1, y1); std::swap(x2, y2); } if (x1 > x2) { std::swap(x1, x2); std::swap(y1, y2); } const double dx = x2 - x1; const double dy = fabs(y2 - y1); double error = dx / 2.0f; const int ystep = (y1 < y2) ? 1 : -1; int y = (int)y1; const int maxX = (int)x2; for (int x = (int)x1; x<maxX; x++) { if (steep) { if (y > -1 && y < image.getWidth() && x > -1 && x < image.getHeight()) { image.setPixel(y, x, color); } } else { if (x > -1 && x < image.getWidth() && y > -1 && y < image.getHeight()) { image.setPixel(x, y, color); } } error -= dy; if (error < 0) { y += ystep; error += dx; } } }
void AppCanvas::readColorPixels(int x, int y, int w, int h, RGBImage& oImage) const { float *rgb = new float[3 * w * h]; memset(rgb, 0, sizeof(float) * 3 * w * h); int xsch = width(); int ysch = height(); if (_pass_diffuse.buf) { int xmin = border().getMin().x(); int ymin = border().getMin().y(); int xmax = border().getMax().x(); int ymax = border().getMax().y(); int rectx = _pass_z.width; int recty = _pass_z.height; float xfac = ((float)rectx) / ((float)(xmax - xmin)); float yfac = ((float)recty) / ((float)(ymax - ymin)); #if 0 if (G.debug & G_DEBUG_FREESTYLE) { printf("readColorPixels %d x %d @ (%d, %d) in %d x %d [%d x %d] -- %d x %d @ %d%%\n", w, h, x, y, xsch, ysch, xmax - xmin, ymax - ymin, rectx, recty, (int)(xfac * 100.0f)); } #endif int ii, jj; for (int j = 0; j < h; j++) { jj = (int)((y - ymin + j) * yfac); if (jj < 0 || jj >= recty) continue; for (int i = 0; i < w; i++) { ii = (int)((x - xmin + i) * xfac); if (ii < 0 || ii >= rectx) continue; memcpy(rgb + (w * j + i) * 3, _pass_diffuse.buf + (rectx * jj + ii) * 3, sizeof(float) * 3); } } } oImage.setArray(rgb, xsch, ysch, w, h, x, y, false); }
// ============================================================================= // main() Program Entry // ============================================================================= int main(int argc, char *argv[]) { if( argc < 2 ) option = "invalid"; else { option = argv[1]; } int width, height; //initialize the global variables width = 640, height = 480; img.resize(width, height); // set the pixels based on the input option setPixels( option ); // OpenGL Commands: // Once "glutMainLoop" is executed, the program loops indefinitely to all // glut functions. glutInit(&argc, argv); glutInitWindowPosition(100, 100); // Where the window will display on-screen. glutInitWindowSize(width, height); glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE); glutCreateWindow("Homework Zero"); init(); glutReshapeFunc(windowResize); glutDisplayFunc(windowDisplay); glutMouseFunc(processMouse); glutKeyboardFunc(processKeyboard); glutMainLoop(); return 0; //This line never gets reached. We use it because "main" is type int. }