Printing an array with a symbol next to it - c++

I would like to print a '>' sign next to an array which is able to move up and down when different things are inputted.
I am able to print a 12x12 array of 0s but when I try to print a '>' in line 7, the '>' is inputted into the array and the row itself is shifted over instead of the array being kept uniform (i.e. the '>' must be independent to the array).
I have tried creating two arrays of the '>' and the 0s but I am not able to print two things horizontally next to each other.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int row = 0;
while (row < 12){
int col = 0;
while (col < 12){
printf(" 0");
col = col + 1;
}
printf("\n");
row = row + 1;
if (row == 7) {
printf(">");
}
}
return 0;
}
output:
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
> 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
intended output:
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
> 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
thank you !

To display a ">" sign before your matrix you can use a conditional statement specifically for the required line number.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
for (int i = 0; i < 12; i++)
{
if(i == 7) // conditional statement
printf("> "); // for ">" followed by
else // a space to maintain
printf(" "); // the structure of matrix
for (int j = 0; j < 12; j++)
{
printf("0 ");
}
printf("\n");
}
return 0;
}
Hope it helps!!!

Related

QImage: how to convert a gray image to a RGB heatmap

I am trying to convert a Format_Grayscale8 image to Format_ARGB32 (or any other 8bpp RGB format). I tried to use QImage::convertToFormat and a color table but I can't make it work. The input is a small 8bit gray image (PGM format text string).
QImage img;
img.loadFromData(gray_map.toLatin1());
QImage heatmap = img.convertToFormat(QImage::Format_Indexed8, color_gradient.getColorMap());
Doing it manually works fine (pixel per pixel):
// gray_img format is QImage::Format_Grayscale8
QImage createHeatMap(QImage gray_img)
{
QImage heatmap(gray_img.width(), gray_img.height(), QImage::Format_ARGB32);
// custom heatmap, size() is 256
const QVector<QRgb> color_map = color_gradient.getColorMap();
// Default color
heatmap.fill(color_map[0]);
for (int y = 0; y < gray_img.height(); y++)
{
for (int x = 0; x < gray_img.width(); x++)
{
const uchar* img_ptr = gray_img.bits();
int offset = x + y*gray_img.bytesPerLine();
uchar gray_pix = *(img_ptr + offset);
// just in case
if (gray_pix < color_map.size())
heatmap.setPixel(x, y, color_map[gray_pix]);
}
}
return heatmap;
}
QImage img;
img.loadFromData(doppler_map.toLatin1());
QImage heatmap = createHeatMap(img);
I am interested in a simpler and more efficient solution. Thanks!
EDIT
Here is the code to generate the color gradient:
// Heatmap color lookup table generator, inspired from:
// http://www.andrewnoske.com/wiki/Code_-_heatmaps_and_color_gradients
#include <QColor>
class ColorGradient
{
private:
struct ColorPoint
{
float r, g, b;
float val; // Position of our color along the gradient (between 0 and 1).
ColorPoint(float red, float green, float blue, float value)
: r(red), g(green), b(blue), val(value)
{
}
};
std::vector<ColorPoint> mColors; // An array of color points in ascending value.
uint mTableSize = 0;
QVector<QRgb> mColorMap;
// hidden
ColorGradient();
public:
inline auto getColorMap()
{
return mColorMap;
}
ColorGradient(uint table_size)
{
createDefaultHeatMapGradient(table_size);
}
//-- Places a 6 color heapmap gradient into the "color" vector
#define CF64(val) ((float)(val) / 64.0)
#define CF256(val) ((float)(val) / 256.0)
void createDefaultHeatMapGradient(uint table_size)
{
mTableSize = table_size;
mColorMap.clear();
mColors.clear();
// ascending order
mColors.push_back(ColorPoint(0, 0, CF256(96), CF64(00))); // Dark Blue
mColors.push_back(ColorPoint(0, 0, 1, CF64(06))); // Blue
mColors.push_back(ColorPoint(0, 1, 1, CF64(22))); // Cyan
mColors.push_back(ColorPoint(1, 1, 0, CF64(39))); // Yellow
mColors.push_back(ColorPoint(1, 0, 0, CF64(55))); // Red
mColors.push_back(ColorPoint(CF256(159),0, 0, CF64(63))); // Dark Red
QColor color;
// Generate the color table
for (uint n = 0; n < table_size; n++)
{
float value = (float)n / (float)table_size;
bool found = false;
for (int i = 0; i < mColors.size(); i++)
{
ColorPoint &currC = mColors[i];
if (value < currC.val)
{
ColorPoint &prevC = mColors[std::max(0, i - 1)];
float valueDiff = (prevC.val - currC.val);
float fractBetween = (valueDiff == 0) ? 0 : (value - currC.val) / valueDiff;
float r = (prevC.r - currC.r)*fractBetween + currC.r;
float g = (prevC.g - currC.g)*fractBetween + currC.g;
float b = (prevC.b - currC.b)*fractBetween + currC.b;
color.setRgbF(r, g, b);
mColorMap.append(color.rgb());
found = true;
break;
}
}
if (!found)
{
color.setRgbF(mColors.back().r, mColors.back().g, mColors.back().b);
mColorMap.append(color.rgb());
}
}
}
};
And a sample text image file:
P2
40 17
64
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0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age::Format_Indexed8 without any color map, and set it in a separate call:
QImage heatmap = img.convertToFormat(QImage::Format_Indexed8);
heatmap.setColorTable(color_gradient.getColorMap());
After this, you can make a second conversion to QImage::Format_RGB888 or whatever you need.

Garbage values being filled in my 2d array

Output is as follows. Im stumped. its numRows+2 because i am adding a border
int** mirrorFramedAry = new int *[numRows+2];
for(int i=0; i<numRows+2; ++i){
mirrorFramedAry[i] = new int[numCols+2];
}
for(int i = 0; i < numRows+2; ++i)
for(int j = 0; j < numCols+2; ++j)
std::cout << mirrorFramedAry[i][j] << " ";
for(int row = 1; row<= numRows; row++){
for(int col = 1; col<= numCols; col++){
int value;
input >> value;
mirrorFramedAry[row][col] = value;
//if(row == 1) mirrorFramedAry[0][col] = value; //mirrors top row
//if(row == numRows) mirrorFramedAry[row+1][col] = value; //mirrors bottom row
}
}
1533360 0 1507664 0 1986348124 1886405421 1852394844 875976519 1852400220 1547322171 1735357008 544039282 1701603654 2015895667 1546204728 762733892 1550872643 1198418253 1546925655 997091682 1348221507 1919381362 1176530273 1936026729 947398688 1146890550 1127052901 1297903728 1464299113 1650209846 1127968361 1917869114 1634887535 1766203501 544433516 909670440 1698978857 1883450742 1766677616 911689582 1768053812 977484654 1533360 0 1507664 0 1886405421 1852394844 875976519 1852400220 1547322171 1735357008 544039282 1701603654 2015895667 1546204728 762733892 1550872643 1198418253 1546925655 997091682 1348221507 1919381362 1176530273 1936026729 947398688 1146890550 1127052901 1297903728 1464299113 1650209846 1127968361 1917869114 1634887535 1766203501 544433516 909670440 1698978857 1883450742 1766677616 911689582 1768053812 977484654 1869762652 1533360 0 1507664 0 1852394844 875976519 1852400220 1547322171 1735357008 544039282 1701603654 2015895667 1546204728 762733892 1550872643 1198418253 1546925655 997091682 1348221507 1919381362 1176530273 1936026729 947398688 1146890550 1127052901 1297903728 1464299113 1650209846 1127968361 1917869114 1634887535 1766203501 544433516 909670440 1698978857 1883450742 1766677616 911689582 1768053812 1342192494 1162368065 775771224 1533360 0 1507664 0 1161975342 1397370427 1397370427 1462647621 775636563 994595671 1129532718 1111707648 1145979218 1986089021 1869180009 1817182318 1868985441 1295871346 1342194755 1162039122 1380930387 1129464159 1163151688 1381323843 1296121157 3421764 1129271888 1330860869 1145659218 1230261829 1380272454 1145913661 1176515638 1818848609 825368697 1685015840 824208485 1951604793 1768976485 824207214 1967202348 1852139636 1533360 0 1507664 0 1380974641 1397048143 1599229779 1230390610 1313818963 808661309 1917845553 1634887535 1952531565 977485153 1869762652 1835102823 1635017028 1869762560 1835102823 1701603654 977485171 1869762652 1835102823 1818838560 1342206821 1919381362 1766223201 678651244 691419256 1547322173 1735357008 544039282 1701603654 2015895667 2700856 1735357008 1466786162 842216502 1547322173 1735357008 544039282 1701603654 1533360 0 1507664 0 2037603411 1835365491 1465659955 1868852841 1867543415 1400006007 1819043176 774993500 1867340848 1701606756 1127963763 1917869114 1634887535 1766203501 544433516 909670440 1766677545 1936683619 544499311 541872467 1987208531 828142181 1415327795 1936486255 2003783772 1750299237 1550609509 1969516365 1551066476 1547322171 1735357008 544039282 1701603654 1767332979 2003788910 2003783795 1750299237 1550609509 1533360 0 1507664 0 2015895667 1546204728 1919117645 1718580079 1146298484 1096577867 1701999994 2003783772 1750299237 1550609509 1869833554 1701016181 1634623821 1551000935 1920301633 1936020069 1668445551 1851870565 1919248225 977484636 1869762652 1835102823 1818838560 673215333 691419256 1667845468 1869836146 1394635878 1551059780 1920301633 1867537509 1400006007 1819043176 1919243100 1701013878 1634623821 1701668199 995914862 1533360 0 1507664 0 1869767529 1952870259 1262768928 2051103859 1550152309 1702326096 1701335922 1398565996 1634889588 6055271 1279415632 1128088393 1934974010 1551069797 1818391888 1358979945 1447119444 977485121 1869762652 1835102823 1818838560 673215333 691419256 1769296220 1767140195 1365009773 1937331028 1550673268 1632261201 2049859958 1392537705 1230197573 1095650895 1128088909 1869835887 1392534892 1702130553 1769096301 1533360 0 1507664 0 5461839 1347241300 1547322173 1919251285 1398103155 1095651909 1096561022 1631875184 1281122676 1818321775 1835357276 1297350768 977485136 1702057308 1432122226 1314014547 1546747457 1148219457 1549890657 1633906508 1700027500 1426092141 1146242387 1229016399 1346911566 1163089152 1297040466 1598966081 1296125778 1346850377 1229344594 1211974988 1398079568 1095651909 1430078797 544367987 1701667150 1163089152 1533360 0 1507664 0 6647137 1481589334 1230196063 1397639519 1280065876 1413566559 977485128 1869762652 1835102823 1818838560 1331458917 1818452338 1767267429 1635087474 2020557420 1398145116 1127232561 1414417743 1397509967 1547322173 1735357008 544039282 1701603654 2015895667 1546204728 1919117645 1718580079 1767252084 1818326387 1970557728 544172388 808334385 1836008284 929984365 1869567068 6058860 1684957559 1128100457 1507664 0 1507664 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1507664 0 1507664 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1507664 0 1507664 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1507664 0 1507664 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1507664 0 1507664 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1507664 0 1507664 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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 exited with return value 3221225477
Press any key to continue . . .
You filled up the data starting from the second row and second column when you used 1 as the starting index for rows and columns in the following loop:
for(int row = 1; row<=numRows; row++){
for(int col = 1; col<=numCols; col++){
int value;
input >> value;
//cout <<value<<" ";
mirrorFramedAry[row][col] = value;
}
}
But then, you are trying to display the data starting from the first row and first column when you use 0 as the starting index for rows and columns in the following loop:
for(int i = 0; i < numRows; ++i)
for(int j = 0; j < numCols; ++j)
std::cout << mirrorFramedAry[i][j] << " ";
If you see garbage, it's because you the contents of the first row and the first column were not properly initialized.
You can fix the problem by:
Not using elements of the array that were not initialized, or
By initializing those all the elements of the array.
Your initialization loops start at index 1 instead of index 0, and go to numRows / numCols instead of numRows-1 / numCols-1
for(int row = 1; row<=numRows; row++){
for(int col = 1; col<=numCols; col++){
int value;
input >> value;
//cout <<value<<" ";
mirrorFramedAry[row][col] = value;
}
}
should be
for(int row = 0; row<numRows; row++){
for(int col = 0; col<numCols; col++){
int value;
input >> value;
//cout <<value<<" ";
mirrorFramedAry[row][col] = value;
}
}

C++: problems reading a Bitmap image

I'm currently writing a function that will manipulate a 512x512 .BMP image and I'm struggling with reading in the pixel data. I'm including fstream in this to deal with reading the image data.
My function should work explicitly with 8-bit Grayscale .BMP images, so I really shouldn't need to worry about the file Headers. You can see in my code I just read the entire header into an array for later usage.
Here is my Constructor for my Image Class:
// Constructor
Image::Image(const char* inFileName){
// First we will initialize our Image Data Arrays:
ImageData = new int* [SIZE];
complexImageData = new complex<double>* [SIZE];
for(int i=0; i<SIZE; ++i){
ImageData[i] = new int [SIZE];
complexImageData[i] = new complex<double> [SIZE];
}
// Now we will initialize our I/O Files.
outFile = new ofstream;
inFile = new ifstream;
// And now we will fill our data arrays with the input file data.
inFile->open(inFileName,ios::in | ios::binary); // Reads file as binary
inFile->seekg(0,ios::beg); // sets filter position to beginning
inFile->read(header,HEADERSIZE); // Reads header
// Now to read in the image data:
for(int i=0; i<SIZE; ++i){
inFile->read(reinterpret_cast<char*>(ImageData[i]),SIZE);
for(int j=0; j<SIZE; ++j){
cout << ImageData[i][j] << ' ';
}
cout << endl;
}
inFile->close();
}
As you can see, it prints out the data as it is read in. The output of this constructor is strange to me. Here's the first 512 entries read into the ImageData array:
236854814 237117986 505286158 236854814 505290270 236854814 572662306 237112866 571346462 505290254 505288222 371072534 236328478 504763918 370546198 152442390 503911958 236330518 370546198 152439062 369690902 504763926 236852758 571347470 572662306 571351570 235802142 504237582 505290254 236330526 504241694 235802126 505290262 370546206 370548246 504765974 371070494 505288214 371072534 369694230 235806222 236854814 237117986 235802146 370548254 369690902 504763926 371072534 370546198 152442390 504760598 504763926 237117986 304226850 235802146 235802126 235807246 504241678 504761870 504237590 371072542 370548246 504763926 236854806 235802142 235802126 235802126 504242702 235802126 303178258 505286178 571351566 572662286 572658210 236065314 303178254 571609618 571351586 571613730 571351586 237112866 571351566 303702562 572658210 237112846 304222754 303700506 304226850 168434194 471079956 169088010 169088020 437914122 437918234 169085466 436865546 169482778 336204810 168430100 336857610 336862228 336204820 471077898 471866380 471079964 169090076 203168778 169088012 471077914 336862236 168430090 168434186 168434202 437918234 169482778 168430106 168430106 169482762 336206346 169085450 336206356 135796768 404228120 270014488 303698458 303174162 303174162 404752922 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
As you can see, approximately the second half of this data is all zeros. Another line is even stranger:
236852766 571346446 236850702 371072542 505291278 371070494 504763934 236330518 370548238 503911966 236852758 371072542 504763913 504237582 571350542 235802126 504242722 503911958 152966678 370542870 369694230 369690905 236330505 504237590 370548246 504763926 370546206 370546198 370546198 235806230 371073550 504763926 370548254 572658206 235807246 571346446 504237582 236854798 505553422 370548254 152442390 236852758 572662306 504765986 370546198 369694217 504763926 370548246 504241686 371072526 235804190 504765974 370542870 151587081 152635673 504763913 369690902 370546206 152443145 505290262 236852766 371072534 505284886 370546206 572397078 371072526 504237598 370548254 371070486 371073566 152439049 370542870 152442377 504763926 571350558 505286174 504765982 236854814 235806238 235802126 235807246 572399134 571351566 571613730 571611666 303698466 437916186 169478682 168430090 169089546 336862236 336860180 336204810 336857620 336202260 168432660 168430090 168430106 471075338 203431964 470551564 336202250 336860180 336860180 336862228 336204820 168430100 436865562 337384458 436868106 168430090 168430090 336204820 436865546 168430090 168432650 269487114 135796760 202905624 437918226 437918234 403705868 404232208 269490200 337252884 571613722 304222738 269228562 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 181497 53543152 0 53543152 0 57016416 0 57016416 0 57135104 0 925696 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
You can see that it also has the same zeros, but amongst those zeros are some unexpected non-zero entries. This is certainly not expected from the input image.
Any help would be greatly appreciated! I'm sorry if this is a duplicate but I tried so very hard to find an answer to my problem before posting this!
EDIT:
I noticed immediately that the numbers being printed (236854814, 237117986 etc) were not in the 8-bit range. I find this so confusing since I'm reading the data reinterpreted as a char, which is 8-bit on my system. Would it not be the case that the data being read into the integer array should be 8-bit? I even tried making my ImageData array of type __int8**, so that it could only store 8 bits per entry, but It still didn't work.
Your image is 8bpp grayscale but you read it into block int [SIZE] (supplying wrong buffer size in the process) potentially storing 4 pixel values in each int. If image is 8bpp you could've just used uint8_t buffer. You should also perform header validation (because right now your may read at wrong position) and check for actual amount of bytes read. Depending on image type / size you may also need to check padding for each line.

HM encoder 16.4 predMode is 2

I want to determine if a CU has been intra or inter encoded with the HEVC reference software HM 16.4 encoder.
In TEncSlice.cpp, after a CTU has been compressed (m_pcCuEncoder->compressCtu( pCtu );), I should be able to tell if a CU is intra or inter encoded.
Therefore, I check the m_pePredMode array of TComDataCU* pCtu:
Char* predMode = pCtu->getPredictionMode();
for (int i = 0; i < pCtu->getTotalNumPart(); i++)
{
std::cout << (int)predMode[i] << " ";
}
std::cout << std::endl;
PredMode is defined in TypeDef.h as:
enum PredMode
{
MODE_INTER = 0, ///< inter-prediction mode
MODE_INTRA = 1, ///< intra-prediction mode
NUMBER_OF_PREDICTION_MODES = 2,
};
The output of my code first gives me the expected result. For the first intra frame, I first only get ones (MODE_INTRA). But for certain CTUs, I also get "twos", which doesn't really make sense to me, as 2 is not supposed to describe a prediction mode.
Similarly, in the inter frames (P frames), I mostly get zeros (MODE_INTER), a few ones (MODE_INTRA), which is ok. But then I also get some "twos". A part of an example output looks like this (each row corresponds to a 64x64 CTU):
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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2 2 2 2 2 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
Does anyone know what these mean? I checked if this would correspond to skip mode, but there was no correlation between the skip flag and the 2s.
Well, I got the answer to this question:
If a CU has predMode 2, this means that the CU is not coded. This happens in this case because the CUs are outside the boundaries of the picture.
The sequence that was coded had a resolution of 416x240 and the CTU size was 64x64. So there are 6.5 CTUs in a row.
This explain why every 7 CTUs, I get 2s as predMode.

Bad_alloc not thrown when I expect it to

Consider this simple program:
#include <exception>
#include <iostream>
int main(void)
{
const std::size_t size = 1<<31;
int *a = NULL;
try
{
a = new int[size];
}
catch (std::exception &e)
{
std::cerr << "caught some bad guy" << std::endl;
return 1;
}
if (a == NULL)
{
std::cerr << "it's null, can't touch this" << std::endl;
return 1;
}
std::cerr << "looks like 'a' is allocated alright!" << std::endl;
for (size_t i = 0; i < size; i ++)
std::cout << a[i] << " ";
return 0;
}
Commentary
I try to allocate some ridiculous amount of memory: (1<<31) * sizeof(int) == 8GB
I add safety checks
Catching std::exception, which should catch std::bad_alloc among other exceptions...
Check if it's not null (even though for this check to actually make sense, I'd need a = new (std::nothrow) int[size] - but regardless of how I allocate memory, it doesn't work)
Environment
RAM installed: 2GB
Operating system: Debian
Architecture: 32-bit
Problem
The problem is that the program, instead of early exit, does something like this:
rr-#burza:~$ g++ test.cpp -o test && ./test
looks like 'a' is allocated alright!
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
(...many other zeros here...)
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0Segmentation fault
The number of zeros printed is exactly 33790, which tells me exactly... nothing.
How can I make my program segfault-proof?
This seems to be a bug in your environment, which causes integer overflow in implementation of new[]. In effect, you are allocating 0 bytes. It might be this bug. C++03 standard is not clear about what should happen, in C++11 std::bad_array_new_length should be thrown.
If you need to support this system you can check if there is chance for overflow before allocating, for example:
size_t size_t_max = -1;
if (size > size_t_max / sizeof(int))
throw ...;
This bug might still affect you however if libraries you use don't have such checks (for example implementation of std::vector).