// // Software License for AMDiS // // Copyright (c) 2010 Dresden University of Technology // All rights reserved. // Authors: Simon Vey, Thomas Witkowski et al. // // This file is part of AMDiS // // See also license.opensource.txt in the distribution. #include "PngReader.h" #include "png.h" namespace AMDiS { /** \brief * Copies the values of a value file to a DOF vector. */ void PngReader::readValues(std::string filename, DOFVector *vec) { FUNCNAME("ValueReader::readValue()"); TEST_EXIT(filename != "")("Filename not specified!\n"); TEST_EXIT(vec)("no DOF vector specified\n"); png_structp png_ptr; png_infop info_ptr; FILE *fp; unsigned int sig_read = 0; int row, col, nVertices, nElements; int bytesPerPixel = 0; // Open files and create the png data structures. if ((fp = fopen(filename.c_str(), "rb")) == NULL) TEST_EXIT(0)("ERROR: file can not be opened\n"); png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); if (png_ptr == NULL) TEST_EXIT(0)("ERROR in png_create_read_struct\n"); info_ptr = png_create_info_struct(png_ptr); if (info_ptr == NULL) TEST_EXIT(0)("ERROR in png_create_info_struct\n"); if (setjmp(png_jmpbuf(png_ptr))) TEST_EXIT(0)("ERROR in png_jmpbuf\n"); png_init_io(png_ptr, fp); png_set_sig_bytes(png_ptr, sig_read); // Read the whole png at once to the pointer info_ptr. png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL); bytesPerPixel = info_ptr->rowbytes / info_ptr->width; cout << "Read image: " << filename << endl; cout << "Size: " << info_ptr->width << " x " << info_ptr->height << " pixel" << endl; cout << "Bytes per pixel: " << bytesPerPixel << endl; double value = 0; const DOFAdmin* admin = vec->getFeSpace()->getAdmin(); int n0 = admin->getNumberOfPreDofs(VERTEX); //offset zum globale DOF-managment int dim = vec->getFeSpace()->getMesh()->getDim(); int dow = Global::getGeo(WORLD); const BasisFunction *basFcts = vec->getFeSpace()->getBasisFcts(); int numBasFcts = basFcts->getNumber(); DegreeOfFreedom *localIndices = new DegreeOfFreedom[numBasFcts]; double xMin = 0.0, xMax = 1.0, yMin = 0.0, yMax = 1.0; getMeshDimension(vec->getFeSpace()->getMesh(), xMin, xMax, yMin, yMax); TraverseStack stack; ElInfo *elInfo = stack.traverseFirst(vec->getFeSpace()->getMesh(), -1, Mesh::CALL_LEAF_EL | Mesh::FILL_COORDS); while (elInfo) { const DegreeOfFreedom **dof = elInfo->getElement()->getDof(); Element *el = elInfo->getElement(); basFcts->getLocalIndices(el, vec->getFeSpace()->getAdmin(), localIndices); for (int i = 0; i < numBasFcts; i++) { double lambdaX = ((elInfo->getCoords())[i][0] - xMin)/(xMax - xMin); double lambdaY = ((elInfo->getCoords())[i][1] - yMin)/(yMax - yMin); col = static_cast(lambdaX*(info_ptr->width-1)); row = static_cast((1.0-lambdaY)*(info_ptr->height-1)); switch (bytesPerPixel) { case 1: value = static_cast(info_ptr->row_pointers[row][col]); break; case 3: value = 1.0/3.0/255.0 * (static_cast(info_ptr->row_pointers[row][3*col]) + static_cast(info_ptr->row_pointers[row][col*3 + 1]) + static_cast(info_ptr->row_pointers[row][col*3 + 2])); break; default: TEST_EXIT(false)("ERROR: bytesPerPixel=%d is unknown case!\n",bytesPerPixel) } (*vec)[localIndices[i]] = value; } elInfo = stack.traverseNext(elInfo); } delete [] localIndices; png_destroy_read_struct(&png_ptr, &info_ptr, NULL); fclose(fp); return(0); } }