Why are my array values disappearing? [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
My main objective is to populate an array of indices. These indices will be retrieved from multiple Wall objects, each of which contains its own array of 6 indices.
These Wall objects are stored in two arrays, one for rows and one for columns.
Here is the Wall class:
#ifndef WALL_H
#define WALL_H
#include "Angel.h"
#include <vector>
using namespace std;
class Wall
{
public:
Wall();
Wall(int index, int orientation);
~Wall();
Wall(const Wall& other);
Wall& operator=(const Wall& other);
int * getIndices();
int indices[6];
void testWall();
protected:
private:
void makeRow(int index);
void makeColumn(int index);
};
#endif // WALL_H
#include "Wall.h"
Wall::Wall()
{
//ctor
}
//This function checks orientation and call the appropriate method
Wall::Wall(int index, int orientation)
{
if(orientation == 0)
{
makeRow(index);
}
else
{
makeColumn(index);
}
}
//this function prints the contents of the array of indices
void Wall::testWall()
{
for (int i = 0; i < 6; ++i)
cout << indices[i] << " ";
cout << "\n";
}
//This function returns the array
int * Wall::getIndices()
{
return indices;
}
//This function takes an index as an argument and populates the array of indices
//for a row wall based on that index.
void Wall::makeRow(int index)
{
int indexAbove = index + 121;
indices[0] = (index);
indices[1] = (indexAbove);
indices[2] = (indexAbove - 1);
indices[3] = (indexAbove - 1);
indices[4] = (index -1);
indices[5] = (index);
testWall();
}
//This function takes an index as an argument and populates the array of indices
//for a column wall based on that index. It then tests the wall
void Wall::makeColumn(int index)
{
int indexAbove = index + 121;
indices[0] = (index);
indices[1] = (index - 11);
indices[2] = (indexAbove - 11);
indices[3] = (indexAbove - 11);
indices[4] = (indexAbove);
indices[5] = (index);
testWall();
}
Wall::Wall(const Wall& other)
{
//copy ctor
}
Wall::~Wall()
{
}
Wall& Wall::operator=(const Wall& rhs)
{
if (this == &rhs) return *this; // handle self assignment
//assignment operator
return *this;
}
When I populate the arrays of Walls I test the values to verify correct input and print it out.
//This function populates 2 arrays of Wall objects. One for rows and one for columns.
void makeGrid()
{
//Access to lower level vertices
for(int i = 0; i < 11; ++i)
for( int j = 0; j < 11; ++j){
int index = (i * 11) + j;
printf("for index %d, these are the related indices\n", index );
if(j != 0){
rows[index] = Wall(index, 0);
}
if(i != 0){
columns[index] = Wall(index, 1) ;
}
}
}
However when I later retrieve them, the values all change to zero, as if my arrays are all uninitialized or creating a new object. I have no idea where i'm going wrong!
//This function tests the indices array contained within each wall of the rows array.
void getIndices()
{
int index = 0;
int * verts = NULL;
for (int i = 0; i < numRows; ++i)
rows[i].testWall();
/*
verts = rows[1].getIndices();
for(int j = 0; j < 6; j++){
indices[index] = rows[i].indices[j];
//cout << indices[index] << " ";
index++;
}
}
for (int i = 0; i < numColumns; i++){
verts = columns[i].getIndices();
for(int j = 0; j < 6; j++){
indices[index] = verts[j];
cout << verts[j] << " ";
index++;
}
}
*/
}
Edit:
These are my declarations of the arrays i'm working with
const int numVertices = 242, numColors = 242, numRows = 110, numColumns = 110;
const int numIndices = (numRows + numColumns) * 6;
vec4 vertices[numVertices];
vec4 colors[numColors];
Wall rows[numRows];
Wall columns[numColumns];
GLint indices[numIndices];
57 178 177 177 56 57
57 46 167 167 178 57
for index 58, these are the related indices
58 179 178 178 57 58
58 47 168 168 179 58
for index 59, these are the related indices
59 180 179 179 58 59
59 48 169 169 180 59
for index 60, these are the related indices
60 181 180 180 59 60
60 49 170 170 181 60
for index 61, these are the related indices
61 182 181 181 60 61
61 50 171 171 182 61
for index 62, these are the related indices
62 183 182 182 61 62
62 51 172 172 183 62
for index 63, these are the related indices
63 184 183 183 62 63
63 52 173 173 184 63
for index 64, these are the related indices
64 185 184 184 63 64
64 53 174 174 185 64
for index 65, these are the related indices
65 186 185 185 64 65
65 54 175 175 186 65
for index 66, these are the related indices
66 55 176 176 187 66
for index 67, these are the related indices
67 188 187 187 66 67
67 56 177 177 188 67
for index 68, these are the related indices
68 189 188 188 67 68
68 57 178 178 189 68
for index 69, these are the related indices
69 190 189 189 68 69
69 58 179 179 190 69
for index 70, these are the related indices
70 191 190 190 69 70
70 59 180 180 191 70
for index 71, these are the related indices
71 192 191 191 70 71
71 60 181 181 192 71
for index 72, these are the related indices
72 193 192 192 71 72
72 61 182 182 193 72
for index 73, these are the related indices
73 194 193 193 72 73
73 62 183 183 194 73
for index 74, these are the related indices
74 195 194 194 73 74
74 63 184 184 195 74
for index 75, these are the related indices
75 196 195 195 74 75
75 64 185 185 196 75
for index 76, these are the related indices
76 197 196 196 75 76
76 65 186 186 197 76
for index 77, these are the related indices
77 66 187 187 198 77
for index 78, these are the related indices
78 199 198 198 77 78
78 67 188 188 199 78
for index 79, these are the related indices
79 200 199 199 78 79
79 68 189 189 200 79
for index 80, these are the related indices
80 201 200 200 79 80
80 69 190 190 201 80
for index 81, these are the related indices
81 202 201 201 80 81
81 70 191 191 202 81
for index 82, these are the related indices
82 203 202 202 81 82
82 71 192 192 203 82
for index 83, these are the related indices
83 204 203 203 82 83
83 72 193 193 204 83
for index 84, these are the related indices
84 205 204 204 83 84
84 73 194 194 205 84
for index 85, these are the related indices
85 206 205 205 84 85
85 74 195 195 206 85
for index 86, these are the related indices
86 207 206 206 85 86
86 75 196 196 207 86
for index 87, these are the related indices
87 208 207 207 86 87
87 76 197 197 208 87
for index 88, these are the related indices
88 77 198 198 209 88
for index 89, these are the related indices
89 210 209 209 88 89
89 78 199 199 210 89
for index 90, these are the related indices
90 211 210 210 89 90
90 79 200 200 211 90
for index 91, these are the related indices
91 212 211 211 90 91
91 80 201 201 212 91
for index 92, these are the related indices
92 213 212 212 91 92
92 81 202 202 213 92
for index 93, these are the related indices
93 214 213 213 92 93
93 82 203 203 214 93
for index 94, these are the related indices
94 215 214 214 93 94
94 83 204 204 215 94
for index 95, these are the related indices
95 216 215 215 94 95
95 84 205 205 216 95
for index 96, these are the related indices
96 217 216 216 95 96
96 85 206 206 217 96
for index 97, these are the related indices
97 218 217 217 96 97
97 86 207 207 218 97
for index 98, these are the related indices
98 219 218 218 97 98
98 87 208 208 219 98
for index 99, these are the related indices
99 88 209 209 220 99
for index 100, these are the related indices
100 221 220 220 99 100
100 89 210 210 221 100
for index 101, these are the related indices
101 222 221 221 100 101
101 90 211 211 222 101
for index 102, these are the related indices
102 223 222 222 101 102
102 91 212 212 223 102
for index 103, these are the related indices
103 224 223 223 102 103
103 92 213 213 224 103
for index 104, these are the related indices
104 225 224 224 103 104
104 93 214 214 225 104
for index 105, these are the related indices
105 226 225 225 104 105
105 94 215 215 226 105
for index 106, these are the related indices
106 227 226 226 105 106
106 95 216 216 227 106
for index 107, these are the related indices
107 228 227 227 106 107
107 96 217 217 228 107
for index 108, these are the related indices
108 229 228 228 107 108
108 97 218 218 229 108
for index 109, these are the related indices
109 230 229 229 108 109
109 98 219 219 230 109
for index 110, these are the related indices
110 99 220 220 231 110
for index 111, these are the related indices
111 232 231 231 110 111
111 100 221 221 232 111
for index 112, these are the related indices
112 233 232 232 111 112
112 101 222 222 233 112
for index 113, these are the related indices
113 234 233 233 112 113
113 102 223 223 234 113
for index 114, these are the related indices
114 235 234 234 113 114
114 103 224 224 235 114
for index 115, these are the related indices
115 236 235 235 114 115
115 104 225 225 236 115
for index 116, these are the related indices
116 237 236 236 115 116
116 105 226 226 237 116
for index 117, these are the related indices
117 238 237 237 116 117
117 106 227 227 238 117
for index 118, these are the related indices
118 239 238 238 117 118
118 107 228 228 239 118
for index 119, these are the related indices
119 240 239 239 118 119
119 108 229 229 240 119
for index 120, these are the related indices
120 241 240 240 119 120
120 109 230 230 241 120
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
Process returned 0 (0x0) execution time : 2.445 s
Press any key to continue.
Please help me! Thanks!

If your copy constructor has nothing more than:
Wall::Wall(const Wall& other)
{
//copy ctor
}
that's the source of the problem. Either let the compiler generate it for you or implement it properly.
Wall::Wall(const Wall& other)
{
for ( int i = 0; i < 6; ++i )
{
indices[i] = other.indices[i];
}
}
The assignment operator suffers from the same problem (Thanks #πάνταῥεῖ).

Related

How to correctly compress a vector using ZSTD simple API?

I'm new to C++ and I wanted to compress a vector via ZSTD compression library. I used ZSTD simple API ZSTD_compress and ZSTD_decompress in the same way as the example. But I found a wired issue that when I compressed and decompressed a vector, the decompressed vector was not the same as the original vector. I'm not sure which part of my operation went wrong.
I looked at ZSTD's GitHub homepage and didn't find an answer. Please help or try to give some ideas how to solve it.
Example C code: https://github.com/facebook/zstd/blob/dev/examples/simple_compression.c
//Initialize a vector
vector<int> NumToCompress ;
NumToCompress.resize(10000);
for(int i = 0; i < 10000; i++)
{
NumToCompress[i] = rand()% 255;
}
//compress
int* com_ptr = NULL;
size_t NumSize = NumToCompress.size();
size_t Boundsize = ZSTD_compressBound(NumSize);
com_ptr =(int*) malloc(Boundsize);
size_t ComSize;
ComSize = ZSTD_compress(com_ptr,Boundsize,NumToCompress.data(),NumToCompress.size(),ZSTD_fast);
//decompress
int* decom_ptr = NULL;
unsigned long long decom_Boundsize;
decom_Boundsize = ZSTD_getFrameContentSize(com_ptr, ComSize);
decom_ptr = (int*)malloc(decom_Boundsize);
size_t DecomSize;
DecomSize = ZSTD_decompress(decom_ptr, decom_Boundsize, com_ptr, ComSize);
vector<int> NumAfterDecompress(decom_ptr,decom_ptr+DecomSize);
//check if two vectors are same
if(NumToCompress == NumAfterDecompress)
{
cout << "Two vectors are same" << endl;
}else
{cout << "Two vectors are insame" << endl;}
free(com_ptr);
free(decom_ptr);
case 1: If zstd can compress std::vector directly?
case 2: How to properly compress vectors with zstd if zstd can compress std::vector directly ?
Two vectors are insame
Original vector:
163 151 162 85 83 190 241 252 249 121 107 82 20 19 233 226 45 81 142 31 86 8 87 39 167 5 212 208 82 130 119 117 27 153 74 237 88 61 106 82 54 213 36 74 104 142 173 149 95 60 53 181 196 140 221 108 17 50 61 226 180 180 89 207 206 35 61 39 223 167 249 150 252 30 224 102 44 14 123 140 202 48 66 143 188 159 123 206 209 184 177 135 236 138 214 187 46 21 99 14
Decompressed vector:
163 151 162 85 83 190 241 252 249 121 107 82 20 19 233 226 45 81 142 31 86 8 87 39 167 0 417 0551929248 21916 551935408 21916 551933352 21916 551939512 21916 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Seems likely that your API expects the size to be in bytes but you give the size as the number of elements. So you need to multiply the number of elements by the size of each element. Like this
ComSize = ZSTD_compress(com_ptr, Boundsize, NumToCompress.data(),
NumToCompress.size()*sizeof(int), ZSTD_fast);
and similarly when you decompress you need to divide by the element size
DecomSize = ZSTD_decompress(decom_ptr, decom_Boundsize, com_ptr, ComSize);
vector<int> NumAfterDecompress(decom_ptr, decom_ptr+DecomSize/sizeof(int));

Printing out an array as an evenly spaced rectangle

Ok so I have this code as part of a project to print an array:
#include <iostream>
#include <string>
#define ROWS 12
#define COLS 10
using namespace std;
void image_print(unsigned char image[ROWS][COLS]) {
for (int r = 0; r < ROWS; r++) {
for (int c = 0; c < COLS; c++) {
cout << +image[r][c] << " ";
}
cout << endl;
}
}
The output looks like this:
0 0 0 0 0 0 0 0 0 0
0 64 10 55 127 235 203 241 163 0
0 207 115 62 37 77 43 186 1 0
0 1 119 234 125 172 90 206 103 0
0 113 193 181 158 69 31 85 212 0
0 161 84 27 145 121 211 192 123 0
0 114 4 29 214 80 231 64 71 0
0 173 251 191 221 241 94 76 81 0
0 126 53 138 141 43 78 32 108 0
0 244 4 11 193 240 99 56 154 0
0 131 141 82 198 212 92 217 148 0
0 0 0 0 0 0 0 0 0 0
But I want it to look evenly spaced like this:
0 0 0 0 0 0 0 0 0 0
0 64 10 55 127 235 203 241 163 0
0 207 115 62 37 77 43 186 1 0
0 1 119 234 125 172 90 206 103 0
0 113 193 181 158 69 31 85 212 0
0 161 84 27 145 121 211 192 123 0
0 114 4 29 214 80 231 64 71 0
0 173 251 191 221 241 94 76 81 0
0 126 53 138 141 43 78 32 108 0
0 244 4 11 193 240 99 56 154 0
0 131 141 82 198 212 92 217 148 0
0 0 0 0 0 0 0 0 0 0
Are there any clever ways I can do that?
#include <iomanip>
cout << setw(3) << +image[r][c] << " ";
setw is an I/O manipulator which sets the field width for the next item to be output.
SetW is the function you are looking for.
It sets the field width to be used on output.
Here is the updated code:
#include <iostream>
#include <string>
#include <iomanip>
#define ROWS 12
#define COLS 10
using namespace std;
void image_print(unsigned char image[ROWS][COLS]) {
for (int r = 0; r < ROWS; r++) {
for (int c = 0; c < COLS; c++) {
cout << std::setw(5)<<image[r][c] << " ";//setW added for settingwidth in output
}
cout << endl;
}
}

Big inputs in command prompt

I want to give a big input in command prompt i.e.
100 100
437 1
68 0
319 0
565 0
307 1
512 0
493 0
30 0
557 0
367 0
547 1
263 0
481 0
78 0
492 1
56 1
81 0
154 0
503 1
375 0
152 0
401 0
226 0
482 0
264 0
52 0
9 0
145 0
72 0
293 0
15 0
42 1
305 0
34 0
509 0
156 0
321 0
437 0
510 1
449 0
79 0
492 0
191 0
354 1
529 0
315 0
384 0
371 0
100 0
480 0
408 0
221 0
286 0
551 0
106 0
123 0
549 0
183 1
428 0
435 0
370 0
46 0
289 0
246 0
414 1
159 0
442 0
286 0
21 0
119 0
263 0
572 0
334 0
96 0
307 0
323 0
554 1
487 0
455 1
399 0
559 0
276 0
357 1
586 0
346 0
240 1
492 0
63 0
262 0
489 0
124 1
526 0
350 1
243 0
35 0
292 0
418 0
364 1
41 1
519 0
This is the part of my code that takes the input :
for (int i = 0; i < n; i++)
{
cin >> a >> b;
A.insert(std::pair<unsigned int, bool>(a, b));
total += a;
if (b == 1)
totalI++;
}
I can obviously just type it all but there must be a shorter way and I want to know what it is. Or is there some other tool / IDE I can use. I am using Visual Studio 2015 (Community)
Just redirect input to the program.
C:\> my_litte_program <mydata.txt
To run the program that way from within Visual Studio 2015, just configure it in the project's debug options:
This works also for running without debugging, e.g. via Ctrl+F5.
Stop using cin; instead, use a stream declared by you. Say, myin.
Put all that "big inputs" of yours in a text file, say, myBigInputs.txt
Make the main() function of your program accept an argument, which is going to be a filename. Use that filename to open myin with. Edit your launch configuration and make it so that when you launch your program from within your IDE, the argument myBigInputs.txt is passed to it.
You can also make the argument to your program optional. If you detect that no argument was passed, then just do myin = cin; in which case your program will work exactly as previously. Then, you will also be able to run your program as follows: myprogram < myBigInput.txt
Open Visual Studio. Load the project. Go to Debug ... Properties ... Configuration Properties ... Debugging
That gives you all the options for setting up the command line. Add "< data.txt" as "command argument"

Histogram image maching using opencv

I need to perform a matching between an image and an histogram I receive as a text.
I do the cdf for both of them:
//Calculating cumulative histogram of src
double total = src.rows*src.cols;
double probSrc[255];
int newValuesSrc[255];
double cuml = 0;
for(int j = 0; j < 256; j++)
{
probSrc[j] = imageHistogram[j]/total; // Probability of each value in image
cuml = cuml + probSrc[j]; // Cumulative probability of current and all previous values
double cdfmax = cuml * 255; // Cumulative probability * max value
newValuesSrc[j] = (int) round(cdfmax);
cout << imageHistogram[j] << " "<< probSrc[j] << " " << newValuesSrc[j] << endl;
}
readHistogramFromFile();
//Calculating cumulative histogram from file
double probDst[255];
int newValuesDst[255];
cuml = 0;
for(int j = 0; j < 256; j++)
{
probDst[j] = receivedHistogram[j]/total; // Probability of each value in image
cuml = cuml + probDst[j]; // Cumulative probability of current and all previous values
double cdfmax = cuml * 255; // Cumulative probability * max value
newValuesDst[j] = (int) round(cdfmax);
cout << receivedHistogram[j] << " "<< probDst[j] << " " << newValuesDst[j] << endl;
}
and I get this values:
For the src image:
207677 0.0901376 23
37615 0.016326 27
19098 0.00828906 29
11955 0.0051888 31
8744 0.00379514 32
7386 0.00320573 32
6546 0.00284115 33
6178 0.00268142 34
5967 0.00258984 34
5437 0.00235981 35
5280 0.00229167 36
5127 0.00222526 36
5002 0.00217101 37
4839 0.00210026 37
4754 0.00206337 38
4676 0.00202951 38
4547 0.00197352 39
4517 0.0019605 39
4484 0.00194618 40
4290 0.00186198 40
4197 0.00182161 41
4188 0.00181771 41
4265 0.00185113 42
4229 0.0018355 42
4233 0.00183724 43
4245 0.00184245 43
4358 0.00189149 44
4330 0.00187934 44
4400 0.00190972 45
4474 0.00194184 45
4519 0.00196137 46
4415 0.00191623 46
4477 0.00194314 47
4468 0.00193924 47
4580 0.00198785 48
4416 0.00191667 48
4558 0.0019783 49
4674 0.00202865 49
4705 0.0020421 50
4998 0.00216927 50
4848 0.00210417 51
4782 0.00207552 51
4883 0.00211936 52
4989 0.00216536 52
4957 0.00215148 53
4987 0.0021645 53
5133 0.00222786 54
4967 0.00215582 54
5217 0.00226432 55
5185 0.00225043 56
5140 0.0022309 56
5236 0.00227257 57
5291 0.00229644 57
5458 0.00236892 58
5473 0.00237543 59
5464 0.00237153 59
5495 0.00238498 60
5439 0.00236068 60
5458 0.00236892 61
5557 0.00241189 62
5881 0.00255252 62
5900 0.00256076 63
5935 0.00257595 64
5902 0.00256163 64
6040 0.00262153 65
6203 0.00269227 66
6146 0.00266753 66
6140 0.00266493 67
6075 0.00263672 68
6054 0.0026276 68
6238 0.00270747 69
6060 0.00263021 70
6153 0.00267057 70
6303 0.00273568 71
6231 0.00270443 72
6278 0.00272483 72
6360 0.00276042 73
6359 0.00275998 74
6368 0.00276389 75
6438 0.00279427 75
6329 0.00274696 76
6408 0.00278125 77
6360 0.00276042 77
6378 0.00276823 78
6329 0.00274696 79
6394 0.00277517 79
6517 0.00282856 80
6521 0.0028303 81
6707 0.00291102 82
6788 0.00294618 82
6761 0.00293446 83
6878 0.00298524 84
7004 0.00303993 85
6963 0.00302214 85
7050 0.0030599 86
6940 0.00301215 87
6875 0.00298394 88
7073 0.00306988 89
7035 0.00305339 89
7146 0.00310156 90
7007 0.00304123 91
7159 0.0031072 92
7089 0.00307682 92
7185 0.00311849 93
7410 0.00321615 94
7237 0.00314106 95
7334 0.00318316 96
7364 0.00319618 97
7452 0.00323437 97
7760 0.00336806 98
7839 0.00340234 99
7882 0.00342101 100
7885 0.00342231 101
8055 0.00349609 102
7923 0.0034388 103
8165 0.00354384 103
8306 0.00360503 104
8271 0.00358984 105
8275 0.00359158 106
8634 0.0037474 107
8684 0.0037691 108
8752 0.00379861 109
9080 0.00394097 110
8958 0.00388802 111
9094 0.00394705 112
9279 0.00402734 113
9234 0.00400781 114
9348 0.00405729 115
9440 0.00409722 116
9431 0.00409332 117
9662 0.00419358 118
9842 0.0042717 119
9816 0.00426042 121
9957 0.00432161 122
10353 0.00449349 123
10626 0.00461198 124
10764 0.00467187 125
10832 0.00470139 126
10767 0.00467318 128
11222 0.00487066 129
11469 0.00497786 130
11661 0.0050612 131
11731 0.00509158 133
12023 0.00521832 134
12086 0.00524566 135
12094 0.00524913 137
12362 0.00536545 138
12364 0.00536632 139
12659 0.00549436 141
12587 0.00546311 142
12776 0.00554514 144
13037 0.00565842 145
13252 0.00575174 147
13425 0.00582682 148
13595 0.00590061 150
13795 0.00598741 151
14308 0.00621007 153
14232 0.00617708 154
14657 0.00636155 156
14966 0.00649566 157
14867 0.00645269 159
15051 0.00653255 161
15510 0.00673177 162
15357 0.00666536 164
15326 0.00665191 166
15308 0.0066441 168
15316 0.00664757 169
15321 0.00664974 171
15298 0.00663976 173
15435 0.00669922 174
15496 0.00672569 176
15307 0.00664366 178
15343 0.00665929 179
15356 0.00666493 181
15315 0.00664714 183
15444 0.00670312 185
15346 0.00666059 186
15583 0.00676345 188
15429 0.00669661 190
15641 0.00678863 191
15661 0.00679731 193
15638 0.00678733 195
15689 0.00680946 197
15866 0.00688628 198
15552 0.00675 200
15150 0.00657552 202
15185 0.00659071 203
14941 0.00648481 205
14989 0.00650564 207
14585 0.0063303 208
14718 0.00638802 210
14553 0.00631641 212
14612 0.00634201 213
14520 0.00630208 215
14358 0.00623177 216
13931 0.00604644 218
13580 0.0058941 220
13370 0.00580295 221
13281 0.00576432 222
13053 0.00566536 224
12711 0.00551693 225
12556 0.00544965 227
12556 0.00544965 228
12125 0.00526259 229
12184 0.00528819 231
11975 0.00519748 232
12198 0.00529427 233
11919 0.00517318 235
11898 0.00516406 236
11589 0.00502995 237
11348 0.00492535 239
11011 0.00477908 240
10523 0.00456727 241
10388 0.00450868 242
9795 0.0042513 243
9251 0.00401519 244
9014 0.00391233 245
8436 0.00366146 246
8266 0.00358767 247
7851 0.00340755 248
7299 0.00316797 249
6996 0.00303646 250
6303 0.00273568 250
5625 0.00244141 251
5375 0.0023329 251
5102 0.00221441 252
4747 0.00206033 253
4313 0.00187196 253
3809 0.00165321 253
3307 0.00143533 254
2756 0.00119618 254
2276 0.000987847 254
1935 0.000839844 255
1617 0.000701823 255
1087 0.000471788 255
547 0.000237413 255
217 9.4184e-05 255
31 1.34549e-05 255
4 1.73611e-06 255
0 0 255
0 0 255
0 0 255
0 0 255
0 0 255
0 0 255
0 0 255
0 0 255
0 0 255
0 0 255
0 0 255
0 0 255
0 0 255
0 0 255
0 0 255
0 0 255
0 0 255
And for the histogram I receive:
10 4.34028e-06 0
11 4.77431e-06 0
12 5.20833e-06 0
13 5.64236e-06 0
14 6.07639e-06 0
15 6.51042e-06 0
16 6.94444e-06 0
17 7.37847e-06 0
18 7.8125e-06 0
19 8.24653e-06 0
20 8.68056e-06 0
22 9.54861e-06 0
24 1.04167e-05 0
26 1.12847e-05 0
28 1.21528e-05 0
30 1.30208e-05 0
34 1.47569e-05 0
38 1.64931e-05 0
42 1.82292e-05 0
50 2.17014e-05 0
60 2.60417e-05 0
70 3.03819e-05 0
80 3.47222e-05 0
90 3.90625e-05 0
100 4.34028e-05 0
120 5.20833e-05 0
140 6.07639e-05 0
160 6.94444e-05 0
160 6.94444e-05 0
150 6.51042e-05 0
140 6.07639e-05 0
130 5.64236e-05 0
120 5.20833e-05 0
110 4.77431e-05 0
100 4.34028e-05 0
90 3.90625e-05 0
80 3.47222e-05 0
70 3.03819e-05 0
60 2.60417e-05 0
50 2.17014e-05 0
40 1.73611e-05 0
30 1.30208e-05 0
20 8.68056e-06 0
10 4.34028e-06 0
10 4.34028e-06 0
10 4.34028e-06 0
10 4.34028e-06 0
11 4.77431e-06 0
12 5.20833e-06 0
13 5.64236e-06 0
14 6.07639e-06 0
15 6.51042e-06 0
16 6.94444e-06 0
17 7.37847e-06 0
18 7.8125e-06 0
19 8.24653e-06 0
20 8.68056e-06 0
22 9.54861e-06 0
24 1.04167e-05 0
26 1.12847e-05 0
28 1.21528e-05 0
30 1.30208e-05 0
34 1.47569e-05 0
38 1.64931e-05 0
42 1.82292e-05 0
50 2.17014e-05 0
60 2.60417e-05 0
70 3.03819e-05 0
80 3.47222e-05 0
90 3.90625e-05 0
100 4.34028e-05 0
120 5.20833e-05 0
140 6.07639e-05 0
160 6.94444e-05 0
160 6.94444e-05 0
150 6.51042e-05 0
140 6.07639e-05 0
130 5.64236e-05 1
120 5.20833e-05 1
110 4.77431e-05 1
100 4.34028e-05 1
90 3.90625e-05 1
80 3.47222e-05 1
70 3.03819e-05 1
60 2.60417e-05 1
50 2.17014e-05 1
40 1.73611e-05 1
30 1.30208e-05 1
20 8.68056e-06 1
10 4.34028e-06 1
10 4.34028e-06 1
11 4.77431e-06 1
12 5.20833e-06 1
13 5.64236e-06 1
14 6.07639e-06 1
15 6.51042e-06 1
16 6.94444e-06 1
17 7.37847e-06 1
18 7.8125e-06 1
19 8.24653e-06 1
20 8.68056e-06 1
22 9.54861e-06 1
24 1.04167e-05 1
26 1.12847e-05 1
28 1.21528e-05 1
30 1.30208e-05 1
34 1.47569e-05 1
38 1.64931e-05 1
42 1.82292e-05 1
50 2.17014e-05 1
60 2.60417e-05 1
70 3.03819e-05 1
80 3.47222e-05 1
90 3.90625e-05 1
100 4.34028e-05 1
120 5.20833e-05 1
140 6.07639e-05 1
160 6.94444e-05 1
160 6.94444e-05 1
150 6.51042e-05 1
140 6.07639e-05 1
130 5.64236e-05 1
120 5.20833e-05 1
110 4.77431e-05 1
100 4.34028e-05 1
90 3.90625e-05 1
80 3.47222e-05 1
70 3.03819e-05 1
60 2.60417e-05 1
50 2.17014e-05 1
40 1.73611e-05 1
30 1.30208e-05 1
20 8.68056e-06 1
10 4.34028e-06 1
10 4.34028e-06 1
10 4.34028e-06 1
10 4.34028e-06 1
11 4.77431e-06 1
12 5.20833e-06 1
13 5.64236e-06 1
14 6.07639e-06 1
15 6.51042e-06 1
16 6.94444e-06 1
17 7.37847e-06 1
18 7.8125e-06 1
19 8.24653e-06 1
20 8.68056e-06 1
22 9.54861e-06 1
24 1.04167e-05 1
26 1.12847e-05 1
28 1.21528e-05 1
30 1.30208e-05 1
34 1.47569e-05 1
38 1.64931e-05 1
42 1.82292e-05 1
50 2.17014e-05 1
60 2.60417e-05 1
70 3.03819e-05 1
80 3.47222e-05 1
90 3.90625e-05 1
100 4.34028e-05 1
120 5.20833e-05 1
140 6.07639e-05 1
160 6.94444e-05 1
160 6.94444e-05 1
150 6.51042e-05 1
140 6.07639e-05 1
130 5.64236e-05 1
120 5.20833e-05 1
110 4.77431e-05 1
100 4.34028e-05 1
90 3.90625e-05 1
80 3.47222e-05 1
70 3.03819e-05 1
60 2.60417e-05 1
50 2.17014e-05 1
40 1.73611e-05 1
30 1.30208e-05 1
20 8.68056e-06 1
10 4.34028e-06 1
10 4.34028e-06 1
10 4.34028e-06 1
20 8.68056e-06 1
30 1.30208e-05 1
40 1.73611e-05 1
50 2.17014e-05 1
60 2.60417e-05 1
70 3.03819e-05 1
80 3.47222e-05 1
90 3.90625e-05 1
100 4.34028e-05 1
120 5.20833e-05 1
140 6.07639e-05 1
160 6.94444e-05 1
160 6.94444e-05 1
150 6.51042e-05 1
140 6.07639e-05 1
130 5.64236e-05 1
120 5.20833e-05 1
110 4.77431e-05 1
100 4.34028e-05 1
90 3.90625e-05 1
80 3.47222e-05 1
70 3.03819e-05 1
60 2.60417e-05 1
50 2.17014e-05 1
40 1.73611e-05 1
30 1.30208e-05 1
20 8.68056e-06 1
10 4.34028e-06 1
10 4.34028e-06 1
10 4.34028e-06 1
20 8.68056e-06 1
30 1.30208e-05 1
40 1.73611e-05 1
40 1.73611e-05 1
50 2.17014e-05 1
55 2.38715e-05 1
60 2.60417e-05 1
65 2.82118e-05 1
70 3.03819e-05 1
75 3.25521e-05 1
80 3.47222e-05 1
85 3.68924e-05 2
90 3.90625e-05 2
95 4.12326e-05 2
90 3.90625e-05 2
80 3.47222e-05 2
70 3.03819e-05 2
60 2.60417e-05 2
50 2.17014e-05 2
40 1.73611e-05 2
30 1.30208e-05 2
20 8.68056e-06 2
10 4.34028e-06 2
10 4.34028e-06 2
10 4.34028e-06 2
20 8.68056e-06 2
30 1.30208e-05 2
40 1.73611e-05 2
40 1.73611e-05 2
50 2.17014e-05 2
55 2.38715e-05 2
60 2.60417e-05 2
65 2.82118e-05 2
70 3.03819e-05 2
75 3.25521e-05 2
80 3.47222e-05 2
85 3.68924e-05 2
90 3.90625e-05 2
95 4.12326e-05 2
100 4.34028e-05 2
105 4.55729e-05 2
110 4.77431e-05 2
115 4.99132e-05 2
120 5.20833e-05 2
As you can see, the src histogram are a lot more distributed than the histrogram I receive ([0-255] against [0-2]).
My question is, what do I do now? How do I match them?
Why don't you scale [0-2] histogram to [0-255]? oldValue * 255 / 2.

C++ Converting a float to an unsigned char?

I'm new to C++, and doing a bit of googling I thought sprintf would do the job, but I get an error upon compiling that I can't convert between an unsigned char and a char. I need an unsigned char because I am going to print to an image file (0-255 RGB).
unsigned char*** pixels = new unsigned char**[SIZE];
vector<float> pixelColors;
...
sprintf(pixels[i][j][k], "%.4g", pixelColors.at(k));
(pixelColors has size of 3 and 'k' refers to a 'for loop' variable)
I'll guess that the floats are in the range 0.0 ... 1.0, then you do it like this:
float redf = 0.5f;
unsigned char reduc = redf * 255;
The variable reduc is now 128.
EDIT: complete example, outputting image in Net PPM format.
// Usage
// program > file.ppm
#include <vector>
#include <iostream>
typedef struct
{ /* colors in range 0..1 anything else is out of gamut */
float red, green, blue;
} color;
using namespace std;
int main ( int argc, char **argv )
{
int width = 10, height = 10;
vector<color> bitmap; // This should maybe be called floatmap? ;)
// Make an image in memory as a float vector
for( int y = 0; y < height; y++ )
{
for( int x = 0; x < width; x++ )
{
color temp;
temp.red = ((float)x / width);
temp.green = 0;
temp.blue = ((float)y / height);
bitmap.push_back(temp);
}
}
// output image as an Netppm pixmap
cout << "P3" << endl << width << " " << height << endl << 255 << endl;
for( int y = 0; y < height; y++ )
{
for( int x = 0; x < width; x++ )
{
int red, green, blue;
red = (unsigned char)(bitmap[y*width+x].red * 255);
green = (unsigned char)(bitmap[y*width+x].green * 255);
blue = (unsigned char)(bitmap[y*width+x].blue * 255);
cout << red << " ";
cout << green << " ";
cout << blue << " ";
}
cout << endl;
}
return 0;
}
I hope this helps you. You can read about Netpbm format on wikipedia.
EDIT2: The image output is clear text.
Result is like this:
(Tiny, isn't it? edit line 16 to 512x512 or something)
And the actual output is this:
P3
10 10
255
0 0 0 25 0 0 51 0 0 76 0 0 102 0 0 127 0 0 153 0 0 178 0 0 204 0 0 229 0 0
0 0 25 25 0 25 51 0 25 76 0 25 102 0 25 127 0 25 153 0 25 178 0 25 204 0 25 229 0 25
0 0 51 25 0 51 51 0 51 76 0 51 102 0 51 127 0 51 153 0 51 178 0 51 204 0 51 229 0 51
0 0 76 25 0 76 51 0 76 76 0 76 102 0 76 127 0 76 153 0 76 178 0 76 204 0 76 229 0 76
0 0 102 25 0 102 51 0 102 76 0 102 102 0 102 127 0 102 153 0 102 178 0 102 204 0 102 229 0 102
0 0 127 25 0 127 51 0 127 76 0 127 102 0 127 127 0 127 153 0 127 178 0 127 204 0 127 229 0 127
0 0 153 25 0 153 51 0 153 76 0 153 102 0 153 127 0 153 153 0 153 178 0 153 204 0 153 229 0 153
0 0 178 25 0 178 51 0 178 76 0 178 102 0 178 127 0 178 153 0 178 178 0 178 204 0 178 229 0 178
0 0 204 25 0 204 51 0 204 76 0 204 102 0 204 127 0 204 153 0 204 178 0 204 204 0 204 229 0 204
0 0 229 25 0 229 51 0 229 76 0 229 102 0 229 127 0 229 153 0 229 178 0 229 204 0 229 229 0 229
Not sure, what is your exact requirement [ since you didnot paste a snippet of the code .. as Greg requested ], following example might resolve it :
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
float i=1;
unsigned char c;
c = static_cast<unsigned char>(i);
cout << c << endl;
getch();
return 0;
}