Am Montag, 13. Mai 2022, finden Wartungsarbeiten am Gitlab-Server (Update auf neue Version statt). Der Dienst wird daher am Montag für einige Zeit nicht verfügbar sein.
On Monday, May 13th 2022, the Gitlab server will be updated. The service will therefore not be accessible for some time on Monday.

PngReader.cc 3.34 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
//
// 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.
   */
Praetorius, Simon's avatar
Praetorius, Simon committed
20
  void PngReader::readValues(std::string filename,
Praetorius, Simon's avatar
Praetorius, Simon committed
21
22
23
24
25
26
27
28
29
30
31
			      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;
Praetorius, Simon's avatar
Praetorius, Simon committed
32
    int row, col;
Praetorius, Simon's avatar
Praetorius, Simon committed
33
34
35
    int bytesPerPixel = 0;

    // Open files and create the png data structures.
Praetorius, Simon's avatar
Praetorius, Simon committed
36
37
38
    if ((fp = fopen(filename.c_str(), "rb")) == NULL) {
      TEST_EXIT(0)("ERROR: file can not >be opened\n");
    }
Praetorius, Simon's avatar
Praetorius, Simon committed
39
40

    png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
Praetorius, Simon's avatar
Praetorius, Simon committed
41
    if (png_ptr == NULL) {
Praetorius, Simon's avatar
Praetorius, Simon committed
42
      TEST_EXIT(0)("ERROR in png_create_read_struct\n");
Praetorius, Simon's avatar
Praetorius, Simon committed
43
    }
Praetorius, Simon's avatar
Praetorius, Simon committed
44
45

    info_ptr = png_create_info_struct(png_ptr);
Praetorius, Simon's avatar
Praetorius, Simon committed
46
    if (info_ptr == NULL) {
Praetorius, Simon's avatar
Praetorius, Simon committed
47
      TEST_EXIT(0)("ERROR in png_create_info_struct\n");
Praetorius, Simon's avatar
Praetorius, Simon committed
48
    }
Praetorius, Simon's avatar
Praetorius, Simon committed
49

Praetorius, Simon's avatar
Praetorius, Simon committed
50
    if (setjmp(png_jmpbuf(png_ptr))) {
Praetorius, Simon's avatar
Praetorius, Simon committed
51
      TEST_EXIT(0)("ERROR in png_jmpbuf\n");
Praetorius, Simon's avatar
Praetorius, Simon committed
52
    }
Praetorius, Simon's avatar
Praetorius, Simon committed
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

    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 BasisFunction *basFcts = vec->getFeSpace()->getBasisFcts();
    int numBasFcts = basFcts->getNumber();
    DegreeOfFreedom *localIndices = new DegreeOfFreedom[numBasFcts];
Praetorius, Simon's avatar
Praetorius, Simon committed
70
71
72

    double xMin = 0.0, xMax = 1.0, yMin = 0.0, yMax = 1.0;
    getMeshDimension(vec->getFeSpace()->getMesh(), xMin, xMax, yMin, yMax);
Praetorius, Simon's avatar
Praetorius, Simon committed
73
74
75
76
77
78
79
80
81
82
    
    TraverseStack stack;
    ElInfo *elInfo = stack.traverseFirst(vec->getFeSpace()->getMesh(), -1,
					 Mesh::CALL_LEAF_EL |
					 Mesh::FILL_COORDS);
    while (elInfo) {
      Element *el = elInfo->getElement();
      basFcts->getLocalIndices(el, vec->getFeSpace()->getAdmin(), localIndices);
      
      for (int i = 0; i < numBasFcts; i++) {
Praetorius, Simon's avatar
Praetorius, Simon committed
83
84
85
86
	double lambdaX = ((elInfo->getCoords())[i][0] - xMin)/(xMax - xMin);
	double lambdaY = ((elInfo->getCoords())[i][1] - yMin)/(yMax - yMin);
	col = static_cast<int>(lambdaX*(info_ptr->width-1));
	row = static_cast<int>((1.0-lambdaY)*(info_ptr->height-1));
Praetorius, Simon's avatar
Praetorius, Simon committed
87
88
89
90
91
92
93
94
95
96
97
	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:
Praetorius, Simon's avatar
Praetorius, Simon committed
98
	    TEST_EXIT(false)("ERROR: bytesPerPixel=%d is unknown case!\n",bytesPerPixel);
Praetorius, Simon's avatar
Praetorius, Simon committed
99
100
101
102
103
104
105
106
107
108
	}
	(*vec)[localIndices[i]] = value;
      }
      elInfo = stack.traverseNext(elInfo);
    }

    delete [] localIndices;

    png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
    fclose(fp);
Praetorius, Simon's avatar
Praetorius, Simon committed
109
110
111
  };
  
};