PngReader.cc 3.4 KB
Newer Older
Praetorius, Simon's avatar
Praetorius, Simon committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//
// 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::readValue(std::string filename,
			      DOFVector<double> *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];
    
    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++) {
	col = static_cast<int>(((elInfo->getCoords())[i][0])*(info_ptr->width-1));
	row = static_cast<int>((1.0-(elInfo->getCoords())[i][1])*(info_ptr->height-1));
	switch (bytesPerPixel) {
	  case 1:
	    value = static_cast<double>(info_ptr->row_pointers[row][col]);
	    break;
	  case 3:
	    value =
	      1.0/3.0/255.0 * (static_cast<double>(info_ptr->row_pointers[row][3*col]) +
		static_cast<double>(info_ptr->row_pointers[row][col*3 + 1]) +
		static_cast<double>(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);
  }
}