void Blank::execute() { TargaImage * image = TargaImage::blankImage(width,height); //blankImage fills each pixel with (0,0,0,0) but we need (0,0,0,255) image->fillAlpha(255); SymbolTable::setSymbol(varName,image); }
void Example::LoadTextures(const string& filename, TargaImage &Texture, GLuint* ID) { if (!Texture.load(filename)) std::cerr << "Could not load the grass texture" << std::endl; glGenTextures(1, ID); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, *ID); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, Texture.getWidth(), Texture.getHeight(), 0, GL_RGB, GL_UNSIGNED_BYTE, Texture.getImageData()); }
void FormImageBase::saveImageToDir(const QString &dir,QImage& image){ QString fullFileName = dir + "/" + imageName + PostfixNames::getPostfix(imageProp.imageType) + PostfixNames::outputFormat; qDebug() << "<FormImageProp> save image:" << fullFileName; QFileInfo fileInfo(fullFileName); (*recentDir).setPath(fileInfo.absolutePath()); if( PostfixNames::outputFormat.compare(".tga") == 0){ TargaImage tgaImage; tgaImage.write(image,fullFileName); }else image.save(fullFileName); }
bool FormImageBase::saveFile(const QString &fileName){ qDebug() << Q_FUNC_INFO << "image:" << fileName; QFileInfo fileInfo(fileName); (*recentDir).setPath(fileInfo.absolutePath()); image = imageProp.getImage(); if( PostfixNames::outputFormat.compare(".tga") == 0 || fileInfo.completeSuffix().compare("tga") == 0 ){ TargaImage tgaImage; tgaImage.write(image,fileName); }else{ image.save(fileName); } return true; }
bool FormImageProp::loadFile(const QString &fileName) { QFileInfo fileInfo(fileName); QImage _image; // Targa support added if(fileInfo.completeSuffix().compare("tga") == 0){ TargaImage tgaImage; _image = tgaImage.read(fileName); }else{ QImageReader loadedImage(fileName); _image = loadedImage.read(); } if (_image.isNull()) { QMessageBox::information(this, QGuiApplication::applicationDisplayName(), tr("Cannot load %1.").arg(QDir::toNativeSeparators(fileName))); return false; } if(bOpenNormalMapMixer){ qDebug() << "<FormImageProp> Open normal mixer image:" << fileName; imageProp.glWidget_ptr->makeCurrent(); if(glIsTexture(imageProp.normalMixerInputTexId)) imageProp.glWidget_ptr->deleteTexture(imageProp.normalMixerInputTexId); imageProp.normalMixerInputTexId = imageProp.glWidget_ptr->bindTexture(_image,GL_TEXTURE_2D); ui->labelNormalMixerInfo->setText("Current image:"+ fileInfo.baseName()); emit imageChanged(); }else{ qDebug() << "<FormImageProp> Open image:" << fileName; imageName = fileInfo.baseName(); (*recentDir).setPath(fileName); image = _image; imageProp.init(image); //emit imageChanged(); emit imageLoaded(image.width(),image.height()); } return true; }
/* saveTargaColorMatrix() * ---------------------- * Save the specified ColorMatrix as the specified targa file * @param targaFilename - name of the targa file to save as (including .tga) * @return - true on successful save, false on failure */ bool ImageProcessing::saveTargaColorMatrix(const string& targaFilename, const ColorMatrix& source) { // Try to load a blank targa for the output TargaImage* dest = TargaImage::blankImage(source.cols(), source.rows()); if( dest == nullptr ) { cout << "Error: unable to create blank image for output." << endl; return false; } // Setup constants for the output image const unsigned int rows = dest->height(); const unsigned int cols = dest->width(); unsigned char* pixels = dest->pixels(); // Write the source ColorMatrix to the dest TargaImage for(unsigned int row = 0, i = 0; row < rows; ++row) for(unsigned int col = 0; col < cols; ++col, ++i) { const Color color(source(row,col)); pixels[i*4 + 0] = color.r(); pixels[i*4 + 1] = color.g(); pixels[i*4 + 2] = color.b(); pixels[i*4 + 3] = color.a(); } // Save the new dest TargaImage bool success = true; if( dest->write(targaFilename.c_str()) != 1 ) { cout << "Error: unable to save " << targaFilename << ", " << tga_error_string(tga_get_last_error()) << endl; success = false; } // Cleanup the new TargaImage delete dest; return success; }
void glcTexture2D::init() { TargaImage textureImg; if(!textureImg.load(m_TextureFilename)) { std::cerr << "Could not load the texture" << std::endl; b_Initialized = false; return; } glGenTextures(1, &m_ID); glBindTexture(GL_TEXTURE_2D, m_ID); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, textureImg.getWidth(), textureImg.getHeight(), 0, GL_RGB, GL_UNSIGNED_BYTE, textureImg.getImageData()); b_Initialized = true; }
bool FormMaterialIndicesManager::loadFile(const QString &fileName) { QFileInfo fileInfo(fileName); QImage _image; // Targa support added if(fileInfo.completeSuffix().compare("tga") == 0){ TargaImage tgaImage; _image = tgaImage.read(fileName); }else{ QImageReader loadedImage(fileName); _image = loadedImage.read(); } if (_image.isNull()) { QMessageBox::information(this, QGuiApplication::applicationDisplayName(), tr("Cannot load material image %1.").arg(QDir::toNativeSeparators(fileName))); return false; } qDebug() << "<FormImageProp> Open material image:" << fileName; (*FormImageProp::recentDir).setPath(fileName); int mIndex = FBOImageProporties::currentMaterialIndeks; if(updateMaterials(_image)){ image = _image; imageProp.init(image); emit materialChanged(); FBOImageProporties::currentMaterialIndeks = mIndex; emit imageLoaded(image.width(),image.height()); // repaint all materials if(FBOImageProporties::currentMaterialIndeks != MATERIALS_DISABLED){ toggleMaterials(true); } } return true; }
/* loadTargaColorMatrix() * ---------------------- * Load the specified targa file into a ColorMatrix object * @param targaFilename - name of the targa file to open (including .tga) * @return - pixel data from the targa file in a ColorMatrix object */ ColorMatrix ImageProcessing::loadTargaColorMatrix(const string& targaFilename) { // Try to load the targa image TargaImage* image = TargaImage::readImage(targaFilename.c_str()); if( image == nullptr ) { stringstream ss; ss << "Error: unable to open " << targaFilename << endl; throw FailedToLoad(ss.str()); } // Setup constants from the input image const unsigned int rows = image->height(); const unsigned int cols = image->width(); const unsigned char* pixels = image->pixels(); if( pixels == nullptr ) { stringstream ss; ss << "Error: invalid pixel data in " << targaFilename << endl; throw BadPixelData(ss.str()); } // Generate the color matrix based on the image pixels ColorMatrix colors(rows, cols); for(unsigned int row = 0, i = 0; row < rows; ++row) for(unsigned int col = 0; col < cols; ++col, ++i) { colors(row,col) = Color(pixels[i*4 + 0], // red pixels[i*4 + 1], // green pixels[i*4 + 2], // blue pixels[i*4 + 3]); // alpha } // Cleanup the TargaImage delete image; return colors; }
/////////////////////////////////////////////////////////////////////////////// // // Run simplified version of Hertzmann's painterly image filter. // You probably will want to use the Draw_Stroke funciton and the // Stroke class to help. // Return success of operation. // /////////////////////////////////////////////////////////////////////////////// bool TargaImage::NPR_Paint() { TargaImage ref_image = TargaImage(*this); TargaImage canvas = TargaImage(width, height); //Constant color that will get painted on int x, y, i, j, g, m; int r0, g0, b0, r1, g1, b1; typedef tuple<int, int, double> error_point; double d, area_error; int T = 25; //Error threshold static const unsigned int arr[] = {7, 3, 1}; //Brush sizes vector<int> rs (arr, arr + sizeof(arr) / sizeof(arr[0]) ); int rad; //***** //Paint //***** for(size_t b=0; b < rs.size(); b++) { //For each brush size ref_image.Filter_Gaussian_N(2*rs[b]+1); //referenceImage g = 2*rs[b]+1; //Could also be 2*rs[b]+1 rad = 3*rs[b]; //***Paint a layer*** vector <Stroke> strokes; //Find all of the grid block centers vector < tuple<int, int> > centers; centers.reserve( (width/g + 1)* (height/g + 1) ); for( x = g/2; x < g*(width/g); x+=g ) { for( y = g/2; y < g*(height/g); y+=g ) { centers.push_back( make_tuple(x, y) ); } } //Construct overlapping blocks around right and bottom edge, Possibly Unnecessary if (width % g) { //Hold x constant equal to width-g/2-1, loop down the column increasing y x = width - g/2 - 1; for( y = g/2; y < g*(height/g); y+=g ) { centers.push_back( make_tuple(x, y) ); } } if (height % g) { y = height - g/2 - 1; //Hold y constant equal to height-g/2-1, loop across the row increasing x for( x = g/2; x < g*(width/g); x+=g ) { centers.push_back( make_tuple(x, y) ); } } //if both x_extra and y_extra then need final bottom right corner block, probably unnecessary for( size_t c=0; c < centers.size(); c++ ) { //For each grid center tie (x, y) = centers[c]; //Find the error in the area/region/block vector < error_point > errors; errors.reserve(g*g); area_error = 0; for (j = -g/2; j<=g/2; ++j) { for (i = -g/2; i<=g/2; ++i) { m = (width*(y+j) + (x+i))*4; //Location of current pixel tie (r0, g0, b0) = make_tuple(ref_image.data[m], ref_image.data[m+1], ref_image.data[m+2]); tie (r1, g1, b1) = make_tuple(canvas.data[m], canvas.data[m+1], canvas.data[m+2]); //Calculate euclidean distance d = sqrt( (double) (r1-r0)*(r1-r0) + (g1-g0)*(g1-g0) + (b1-b0)*(b1-b0) ); //Save the coordinate and its error errors.push_back(error_point(x+i, y+j, d)); area_error += d; }//i }//j if( area_error > T ) { //Find largest error point sort(errors.begin(), errors.end(), cmp_errors); tie (i, j, d) = errors.front(); //Build a Stroke at loacation x,y with radius and value of r,g,b,alpha m = (width*j + i)*4; //Location of current pixel Stroke s = Stroke(rad, i, j, ref_image.data[m], ref_image.data[m+1], ref_image.data[m+2], ref_image.data[m+3]); strokes.push_back(s); } }//c //******* //Paint all strokes in random order onto canvas //******* random_shuffle ( strokes.begin(), strokes.end() ); vector <Stroke>::iterator it; for (it = strokes.begin(); it != strokes.end(); it++) { canvas.Paint_Stroke(*it); } //Reset the reference image to the unchanged image data for the next smaller pass memcpy(ref_image.data, data, sizeof(unsigned char) * width * height * 4); }//b //Write the canvas data to the displayed image memcpy(data, canvas.data, sizeof(unsigned char) * width * height * 4); //Overwrite alpha values for(i=3; i<width*height*4; i+=4) { data[i] = 255; } return true; }