1 | /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
---|
2 | /* |
---|
3 | * Copyright (C) 2015 HUBzero Foundation, LLC |
---|
4 | * |
---|
5 | * Author: Leif Delgass <ldelgass@purdue.edu> |
---|
6 | */ |
---|
7 | #include "ColorMap.h" |
---|
8 | |
---|
9 | using namespace GeoVis; |
---|
10 | |
---|
11 | void |
---|
12 | ColorMap::renderColorMap(osg::TransferFunction1D *map, int width, int height, |
---|
13 | osg::Image *imgData, |
---|
14 | bool opaque, float bgColor[3], |
---|
15 | bool bgr, |
---|
16 | int bytesPerPixel) |
---|
17 | { |
---|
18 | int size = bytesPerPixel * width * height; |
---|
19 | if (imgData->getTotalSizeInBytes() != (unsigned int)size) { |
---|
20 | GLenum pixelFormat; |
---|
21 | if (bytesPerPixel == 3) { |
---|
22 | pixelFormat = bgr ? GL_BGR : GL_RGB; |
---|
23 | } else { |
---|
24 | pixelFormat = bgr ? GL_BGRA: GL_RGBA; |
---|
25 | }; |
---|
26 | imgData->allocateImage(width, height, 1, pixelFormat, GL_UNSIGNED_BYTE, 1); |
---|
27 | } |
---|
28 | unsigned char *dst = imgData->data(); |
---|
29 | //unsigned char *src = map->data(); |
---|
30 | float min = map->getMinimum(); |
---|
31 | float max = map->getMaximum(); |
---|
32 | if (height > width) { |
---|
33 | for (int i = 0; i < height; i++) { |
---|
34 | float x = (float)i/(height-1); |
---|
35 | float val = min + (max - min) * x; |
---|
36 | osg::Vec4f rgba = map->getColor(val); |
---|
37 | float opacity = rgba[3]; |
---|
38 | unsigned char color[3]; |
---|
39 | if (opaque) { |
---|
40 | color[0] = (unsigned char)(255. * (bgr ? rgba[2] : rgba[0])); |
---|
41 | color[1] = (unsigned char)(255. * rgba[1]); |
---|
42 | color[2] = (unsigned char)(255. * (bgr ? rgba[0] : rgba[2])); |
---|
43 | } else { |
---|
44 | color[0] = (unsigned char)(255. * (bgColor[0] * (1.0 - opacity) + (bgr ? rgba[2] : rgba[0]) * opacity)); |
---|
45 | color[1] = (unsigned char)(255. * (bgColor[1] * (1.0 - opacity) + rgba[1] * opacity)); |
---|
46 | color[2] = (unsigned char)(255. * (bgColor[2] * (1.0 - opacity) + (bgr ? rgba[0] : rgba[2]) * opacity)); |
---|
47 | } |
---|
48 | for (int j = 0; j < width; j++) { |
---|
49 | memcpy(dst, color, 3); |
---|
50 | dst += 3; |
---|
51 | if (bytesPerPixel == 4) { |
---|
52 | *(dst++) = (unsigned char)(255. * (opaque ? 1.0 : opacity)); |
---|
53 | } |
---|
54 | } |
---|
55 | } |
---|
56 | } else { |
---|
57 | for (int i = 0; i < height; i++) { |
---|
58 | for (int j = 0; j < width; j++) { |
---|
59 | float x = (float)j/(width-1); |
---|
60 | float val = min + (max - min) * x; |
---|
61 | osg::Vec4f rgb = map->getColor(val); |
---|
62 | double opacity = rgb[3]; |
---|
63 | unsigned char color[3]; |
---|
64 | if (opaque) { |
---|
65 | color[0] = (unsigned char)(255. * (bgr ? rgb[2] : rgb[0])); |
---|
66 | color[1] = (unsigned char)(255. * rgb[1]); |
---|
67 | color[2] = (unsigned char)(255. * (bgr ? rgb[0] : rgb[2])); |
---|
68 | } else { |
---|
69 | color[0] = (unsigned char)(255. * (bgColor[0] * (1.0 - opacity) + (bgr ? rgb[2] : rgb[0]) * opacity)); |
---|
70 | color[1] = (unsigned char)(255. * (bgColor[1] * (1.0 - opacity) + rgb[1] * opacity)); |
---|
71 | color[2] = (unsigned char)(255. * (bgColor[2] * (1.0 - opacity) + (bgr ? rgb[0] : rgb[2]) * opacity)); |
---|
72 | } |
---|
73 | memcpy(dst, color, 3); |
---|
74 | dst += 3; |
---|
75 | if (bytesPerPixel == 4) { |
---|
76 | *(dst++) = (unsigned char)(255. * (opaque ? 1.0 : opacity)); |
---|
77 | } |
---|
78 | } |
---|
79 | } |
---|
80 | } |
---|
81 | } |
---|