CS50 pset4 filter reflection - cs50

#include "helpers.h"
#include <math.h>
// Convert image to grayscale
void grayscale(int height, int width, RGBTRIPLE image[height][width])
{
for ( int h=0 ; h<height ; h++)
{
for (int w=0 ; w<width ; w++)
{
int i = image[h][w].rgbtBlue + image[h][w].rgbtGreen + image[h][w].rgbtRed ;
float j = i/3 ;
int n = round(j) ;
image[h][w].rgbtBlue = n ;
image[h][w].rgbtGreen = n ;
image[h][w].rgbtRed = n ;
}
}
return;
}
// Convert image to sepia
void sepia(int height, int width, RGBTRIPLE image[height][width])
{
for ( int h=0 ; h<height ; h++)
{
for (int w=0 ; w<width ; w++)
{
float sr = .393 * image[h][w].rgbtRed + .769 * image[h][w].rgbtGreen + .189 * image[h][w].rgbtBlue ;
float sg = .349 * image[h][w].rgbtRed + .686 * image[h][w].rgbtGreen + .168 * image[h][w].rgbtBlue ;
float sb = .272 * image[h][w].rgbtRed + .534 * image[h][w].rgbtGreen + .131 * image[h][w].rgbtBlue ;
int SR = round(sr);
int SG = round(sg);
int SB = round(sb);
if ( SR>255)
{
SR = 255 ;
}
if (SG>255)
{
SG = 255 ;
}
if (SB>255)
{
SB = 255 ;
}
image[h][w].rgbtBlue = SB ;
image[h][w].rgbtGreen = SG ;
image[h][w].rgbtRed = SR ;
}
}
return;
}
// Reflect image horizontally
void swap (int *a , int *b) ;
void reflect(int height, int width, RGBTRIPLE image[height][width])
{
for (int h=0 ; h<height ; h++)
{
for (int w=0 ; w<width/2 ; w++)
{
for (int p=width-1 ; p>=round(width/2) ; p--)
{
int blue = image[h][w].rgbtBlue ;
int B = image[h][p].rgbtBlue ;
swap(&blue , &B);
int g = image[h][w].rgbtGreen ;
int G = image[h][p].rgbtGreen ;
swap(&g , &G);
int r = image[h][w].rgbtRed ;
int R = image[h][p].rgbtRed ;
swap(&r , &R );
}
}
}
return;
}
void swap (int *a , int *b)
{
int tmp = *a ;
*a = *b ;
*b = tmp ;
}
// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
return;
}
I have written this code for helper.c. It compiles ok and works for grayscale and sepia but fails to reflect the image.
I am stuck in it . Please tell me where have i made the mistake.

// Reflect image horizontally
void reflect(int height, int width, RGBTRIPLE image[height][width])
{
int tmp[3];
for (int h = 0; h < height; h++)
{
for (int w = 0; w < width / 2; w++)
{
// Place the first pixel in a temporary variable
tmp[0] = image[h][w].rgbtRed;
tmp[1] = image[h][w].rgbtGreen;
tmp[2] = image[h][w].rgbtBlue;
// Place the opposite pixel one into the first pixel
image[h][w].rgbtRed = image[h][width - w - 1].rgbtRed;
image[h][w].rgbtGreen = image[h][width - w - 1].rgbtGreen;
image[h][w].rgbtBlue = image[h][width - w - 1].rgbtBlue;
// From the temporary variable extract the first pixel and place it into
the opposite pixel
image[h][width - w - 1].rgbtRed = tmp[0];
image[h][width - w - 1].rgbtGreen = tmp[1];
image[h][width - w - 1].rgbtBlue = tmp[2];
}
}
return;
}

You don't need the third for loop
for (int p=width-1 ; p>=round(width/2) ; p--)
instead, you can assign the opposite pixel inside the second loop as follows:
for (int w=0 ; w<width/2 ; w++)
{
p = width - w - 1;
...
}

Related

How to use vector acsess element through function

This code is made for the multiplication of two arrays given by the user
typedef vector<vector<int> > arr ;
void multiply (arr &arr1 ,arr &arr2 )
{
arr res ;
unsigned new_row = arr1.size() ;
unsigned new_col = arr2.at(0).size();
for(int i = 0 ; i < new_row ; i++)
{
vector <int> vec ;
res.push_back(vec ) ;
for(int j = 0 ; j<new_col ;j++)
{
int x = 0 ;
res.at(i).push_back(x);
for(unsigned k =0 ; k <arr2.size();k++)
{
res.at(i).at(j) += arr1.at(i).at(k)*arr2.at(k).at(j);
}
cout<< res.at(i).at(j) ;
}
}
}
int main()
{
unsigned rows_number1 = 0 , columns_number1 = 0 ;
arr arr1 ;
cout<<"MATRIX A "<<endl<<endl ;
cout << "The Rows : " ;
cin >> rows_number1 ;
cout << "The Columns :" ;
cin>> columns_number1 ;
for(int i = 0 ; i<rows_number1;i++)
{
vector<int> newr ;
arr1.push_back(newr);
for(int j = 0; j<columns_number1 ;j++)
{
int x ;
cout<<"The Member ("<<i+1<<","<<j+1 <<") :" ;
cin>>x ;
arr1.at(i).push_back(x);
}
}
unsigned rows_number2 = 0 , columns_number2 = 0 ;
arr arr2 ;
cout<<"MATRIX B "<<endl<<endl ;
cout << "The Rows : " ;
cin >> rows_number2 ;
cout << "The Columns :" ;
cin>> columns_number2 ;
for(int i = 0 ; i<rows_number2;i++)
{
vector<int> newr ;
arr1.push_back(newr);
for(int j = 0; j<columns_number2 ;j++)
{
int x ;
cout<<"The Member ("<<i+1<<","<<j+1 <<") :" ;
cin>>x ;
arr1.at(i).push_back(x);
}
}
system("cls");
if(columns_number1!=rows_number2)
{
cout<<"Error Multiplication Dimensions" <<endl ;
}
else
{
cout << "A * B ="<<endl;
multiply(arr1,arr2);
}
}
why there is an error and what is the other way ??
how i can improve the code to multiplicate two arrays
Edited : i tried with two 2*2 arrays using console input and output and this is my full code
The problem is out_of_range exception but i don't know why
When you mulitply to matrices, there are strict constraints on the input.
The number of columns of the first matrix must be the same as the number of rows of the second matrix.
When your matrix is represented by a std::vector<std::vector<int>>, you'll have to make sure that ALL the nested std::vectors are of the same size.
Also, don't assume that arr2 is non-empty. When it is empty, arr2.at(0) will thrown an exception.
Here's a more robust version of your function.
void multiply (arr &arr1 ,arr &arr2 )
{
arr res;
unsigned num_rows1 = arr1.size();
if ( num_rows1 == 0 )
{
// Can't do much.
// Return.
return res;
}
unsigned num_cols1 = arr1[0].size();
if ( num_cols1 == 0 )
{
// Can't do much.
// Return.
return res;
}
// Inner vector size check of arr1.
for(unsigned int i = 1 ; i < num_rows1 ; i++)
{
if ( num_cols1 != arr1[i].size() )
{
throw std::runtime_error("Bad input");
}
}
// Make sure the number of columns in arr1 is the same as
// number of rows in arr2.
unsigned num_rows2 = arr2.size();
if ( num_cols1 != num_rows2 )
{
throw std::runtime_error("Bad input");
}
unsigned num_cols2 = arr2[0].size();
if ( num_cols2 == 0 )
{
// Can't do much.
// Return.
return res;
}
// Inner vector size check of arr2.
for(unsigned int i = 1 ; i < num_rows2 ; i++)
{
if ( num_cols2 != arr2[i].size() )
{
throw std::runtime_error("Bad input");
}
}
// All inputs appear to be valid.
// Now, do the multiplication.
unsigned new_row = num_rows1;
unsigned new_col = num_cols2;
res.resize(num_rows1);
for(unsigned i = 0 ; i < new_row ; i++)
{
for(unsigned j = 0 ; j<new_col ;j++)
{
int x = 0 ;
res.at(i).push_back(x);
for(unsigned k =0 ; k < num_cols1; k++)
{
res.at(i).at(j) += arr1.at(i).at(k)*arr2.at(k).at(j);
}
cout<< res.at(i).at(j) ;
}
}
}

Reaction-Diffusion algorithm on Processing + Multithreading

I have made an implementation of the Reaction-Diffusion algorithm on Processing 3.1.1, following a video tutorial. I have made some adaptations on my code, like implementing it on a torus space, instead of a bounded box, like the video.
However, I ran into this annoying issue, that the code runs really slow, proportional to the canvas size (larger, slower). With that, I tried optmizing the code, according to my (limited) knowledge. The main thing I did was to reduce the number of loops running.
Even then, my code still ran quite slow.
Since I have noticed that with a canvas of 50 x 50 in size, the algorithm ran at a good speed, I tried making it multithreaded, in such a way that the canvas would be divided between the threads, and each thread would run the algorithm for a small region of the canvas.
All threads read from the current state of the canvas, and all write to the future state of the canvas. The canvas is then updated using Processing's pixel array.
However, even with multithreading, I didn't see any performance improvement. By the contrary, I saw it getting worse. Now sometimes the canvas flicker between a rendered state and completely white, and in some cases, it doesn't even render.
I'm quite sure that I'm doing something wrong, or I may be taking the wrong approach to optimizing this algorithm. And now, I'm asking for help to understand what I'm doing wrong, and how I could fix or improve my code.
Edit: Implementing ahead of time calculation and rendering using a buffer of PImage objects has removed flickering, but the calculation step on the background doesn't run fast enough to fill the buffer.
My Processing Sketch is below, and thanks in advance.
ArrayList<PImage> buffer = new ArrayList<PImage>();
Thread t;
Buffer b;
PImage currentImage;
Point[][] grid; //current state
Point[][] next; //future state
//Reaction-Diffusion algorithm parameters
final float dA = 1.0;
final float dB = 0.5;
//default: f = 0.055; k = 0.062
//mitosis: f = 0.0367; k = 0.0649
float feed = 0.055;
float kill = 0.062;
float dt = 1.0;
//multi-threading parameters to divide canvas
int threadSizeX = 50;
int threadSizeY = 50;
//red shading colors
color red = color(255, 0, 0);
color white = color(255, 255, 255);
color black = color(0, 0, 0);
//if redShader is false, rendering will use a simple grayscale mode
boolean redShader = true;
//simple class to hold chemicals A and B amounts
class Point
{
float a;
float b;
Point(float a, float b)
{
this.a = a;
this.b = b;
}
}
void setup()
{
size(300, 300);
//initialize matrices with A = 1 and B = 0
grid = new Point[width][];
next = new Point[width][];
for (int x = 0; x < width; x++)
{
grid[x] = new Point[height];
next[x] = new Point[height];
for (int y = 0; y < height; y++)
{
grid[x][y] = new Point(1.0, 0.0);
next[x][y] = new Point(1.0, 0.0);
}
}
int a = (int) random(1, 20); //seed some areas with B = 1.0
for (int amount = 0; amount < a; amount++)
{
int siz = 2;
int x = (int)random(width);
int y = (int)random(height);
for (int i = x - siz/2; i < x + siz/2; i++)
{
for (int j = y - siz/2; j < y + siz/2; j++)
{
int i2 = i;
int j2 = j;
if (i < 0)
{
i2 = width + i;
} else if (i >= width)
{
i2 = i - width;
}
if (j < 0)
{
j2 = height + j;
} else if (j >= height)
{
j2 = j - height;
}
grid[i2][j2].b = 1.0;
}
}
}
initializeThreads();
}
/**
* Divide canvas between threads
*/
void initializeThreads()
{
ArrayList<Reaction> reactions = new ArrayList<Reaction>();
for (int x1 = 0; x1 < width; x1 += threadSizeX)
{
for (int y1 = 0; y1 < height; y1 += threadSizeY)
{
int x2 = x1 + threadSizeX;
int y2 = y1 + threadSizeY;
if (x2 > width - 1)
{
x2 = width - 1;
}
if (y2 > height - 1)
{
y2 = height - 1;
}
Reaction r = new Reaction(x1, y1, x2, y2);
reactions.add(r);
}
}
b = new Buffer(reactions);
t = new Thread(b);
t.start();
}
void draw()
{
if (buffer.size() == 0)
{
return;
}
PImage i = buffer.get(0);
image(i, 0, 0);
buffer.remove(i);
//println(frameRate);
println(buffer.size());
//saveFrame("output/######.png");
}
/**
* Faster than calling built in pow() function
*/
float pow5(float x)
{
return x * x * x * x * x;
}
class Buffer implements Runnable
{
ArrayList<Reaction> reactions;
boolean calculating = false;
public Buffer(ArrayList<Reaction> reactions)
{
this.reactions = reactions;
}
public void run()
{
while (true)
{
if (buffer.size() < 1000)
{
calculate();
if (isDone())
{
buffer.add(currentImage);
Point[][] temp;
temp = grid;
grid = next;
next = temp;
calculating = false;
}
}
}
}
boolean isDone()
{
for (Reaction r : reactions)
{
if (!r.isDone())
{
return false;
}
}
return true;
}
void calculate()
{
if (calculating)
{
return;
}
currentImage = new PImage(width, height);
for (Reaction r : reactions)
{
r.calculate();
}
calculating = true;
}
}
class Reaction
{
int x1;
int x2;
int y1;
int y2;
Thread t;
public Reaction(int x1, int y1, int x2, int y2)
{
this.x1 = x1;
this.x2 = x2;
this.y1 = y1;
this.y2 = y2;
}
public void calculate()
{
Calculator c = new Calculator(x1, y1, x2, y2);
t = new Thread(c);
t.start();
}
public boolean isDone()
{
if (t.getState() == Thread.State.TERMINATED)
{
return true;
} else
{
return false;
}
}
}
class Calculator implements Runnable
{
int x1;
int x2;
int y1;
int y2;
//weights for calculating the Laplacian for A and B
final float[][] laplacianWeights = {{0.05, 0.2, 0.05},
{0.2, -1, 0.2},
{0.05, 0.2, 0.05}};
/**
* x1, x2, y1, y2 delimit a rectangle. The object will only work within it
*/
public Calculator(int x1, int y1, int x2, int y2)
{
this.x1 = x1;
this.x2 = x2;
this.y1 = y1;
this.y2 = y2;
//println("x1: " + x1 + ", y1: " + y1 + ", x2: " + x2 + ", y2: " + y2);
}
#Override
public void run()
{
reaction();
show();
}
public void reaction()
{
for (int x = x1; x <= x2; x++)
{
for (int y = y1; y <= y2; y++)
{
float a = grid[x][y].a;
float b = grid[x][y].b;
float[] l = laplaceAB(x, y);
float a2 = reactionDiffusionA(a, b, l[0]);
float b2 = reactionDiffusionB(a, b, l[1]);
next[x][y].a = a2;
next[x][y].b = b2;
}
}
}
float reactionDiffusionA(float a, float b, float lA)
{
return a + ((dA * lA) - (a * b * b) + (feed * (1 - a))) * dt;
}
float reactionDiffusionB(float a, float b, float lB)
{
return b + ((dB * lB) + (a * b * b) - ((kill + feed) * b)) * dt;
}
/**
* Calculates Laplacian for both A and B at same time, to reduce amount of loops executed
*/
float[] laplaceAB(int x, int y)
{
float[] l = {0.0, 0.0};
for (int i = x - 1; i < x + 2; i++)
{
for (int j = y - 1; j < y + 2; j++)
{
int i2 = i;
int j2 = j;
if (i < 0)
{
i2 = width + i;
} else if (i >= width)
{
i2 = i - width;
}
if (j < 0)
{
j2 = height + j;
} else if (j >= height)
{
j2 = j - height;
}
int weightX = (i - x) + 1;
int weightY = (j - y) + 1;
l[0] += laplacianWeights[weightX][weightY] * grid[i2][j2].a;
l[1] += laplacianWeights[weightX][weightY] * grid[i2][j2].b;
}
}
return l;
}
public void show()
{
currentImage.loadPixels();
//renders the canvas using the pixel array
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
float a = next[x][y].a;
float b = next[x][y].b;
int pix = x + y * width;
float diff = (a - b);
color c;
if (redShader) //aply red shading
{
float thresh = 0.5;
if (diff < thresh)
{
float diff2 = map(pow5(diff), 0, pow5(thresh), 0, 1);
c = lerpColor(black, red, diff2);
} else
{
float diff2 = map(1 - pow5(-diff + 1), 1 - pow5(-thresh + 1), 1, 0, 1);
c = lerpColor(red, white, diff2);
}
} else //apply gray scale shading
{
c = color(diff * 255, diff * 255, diff * 255);
}
currentImage.pixels[pix] = c;
}
}
currentImage.updatePixels();
}
}
A programmer had a problem. He thought “I know, I’ll solve it with threads!”. has Now problems. two he
Processing uses a single rendering thread.
It does this for good reason, and most other renderers do the same thing. In fact, I don't know of any multi-threaded renderers.
You should only change what's on the screen from Processing's main rendering thread. In other words, you should only change stuff from Processing's functions, not your own thread. This is what's causing the flickering you're seeing. You're changing stuff as it's being drawn to the screen, which is a horrible idea. (And it's why Processing uses a single rendering thread in the first place.)
You could try to use your multiple threads to do the processing, not the rendering. But I highly doubt that's going to be worth it, and like you saw, it might even make things worse.
If you want to speed up your sketch, you might also consider doing the processing ahead of time instead of in real time. Do all your calculations at the beginning of the sketch, and then just reference the results of the calculations when it's time to draw the frame. Or you could draw to a PImage ahead of time, and then just draw those.

Unexpected result in color conversion from LAB to RGB

My goal is to create high-resolution images of color charts in LAB.
I'm a beginner in programming and I use Processing because that's the best I know to do it. However, it works only in RGB or HSB so I have to convert LAB to RGB in order to display it.
I used the formulas found on the web (LAB to XYZ and XYZ to RGB)
I included them in my code, then I use a "for" loop to determine the color for each pixel.
I saw a few topics on color conversion, but I'm kind of stuck as I don't know where my problem is coming from ....
So here it is: for a fixed value of L = 100, everything is working perfectly, I'm getting this image as expected :
https://drive.google.com/file/d/0ByjuuWpChE01X3otSFRQNFUyVjA/edit?usp=sharing
But when I try to make another image for a fixed value of a = 0, I get a horizontal line in the bottom, as if there was a problem with lower values of L ... here it is :
https://drive.google.com/file/d/0ByjuuWpChE01RzJWUVZnR2U3VW8/edit?usp=sharing
Here is my code, I hope it will be clear, ask me if you need anything, and thank you very much for your help.
// parameters for the code execution
void setup() {
noLoop();
size(10,10,P2D);
nuancier = createGraphics(taille,taille);
}
// final image file and size
PGraphics nuancier ;
int taille = 1000 ;
// Arrays for color values
float[] colorLAB = new float[3];
float[] colorXYZ = new float[3];
float[] colorRGB = new float[3];
// colors
float X;
float Y;
float Z;
float L;
float a;
float b;
float R;
float G;
float B;
// pixels
int x ;
int y ;
// function to convert Lab to XYZ
float[] LABtoXYZ() {
L = colorLAB[0];
a = colorLAB[1];
b = colorLAB[2];
float ntY = ( L + 16 ) / 116 ;
float ntX = a / 500 + ntY ;
float ntZ = ntY - b / 200 ;
if ( (pow(ntY,3)) > 0.008856 ) {
ntY = (pow(ntY,3)) ;
} else { ntY = ( ntY - 16 / 116 ) / 7.787 ; }
if ( (pow(ntX,3)) > 0.008856 ) {
ntX = (pow(ntX,3)) ;
} else { ntX = ( ntX - 16 / 116 ) / 7.787 ; }
if ( (pow(ntZ,3)) > 0.008856 ) {
ntZ = (pow(ntZ,3)) ;
} else { ntZ = ( ntZ - 16 / 116 ) / 7.787 ; }
X = 95.047 * ntX ; //ref_X = 95.047 Observateur= 2°, Illuminant= D65
Y = 100 * ntY ; //ref_Y = 100.000
Z = 108.883 * ntZ ; //ref_Z = 108.883
colorXYZ[0] = X ;
colorXYZ[1] = Y ;
colorXYZ[2] = Z ;
return colorXYZ ;
}
// function to convert XYZ to RGB
float[] XYZtoRGB() {
X = colorXYZ[0];
Y = colorXYZ[1];
Z = colorXYZ[2];
float ntX = X / 100 ; //X compris entre 0 et 95.047 ( Observateur = 2°, Illuminant = D65 )
float ntY = Y / 100 ; //Y compris entre 0 et 100.000
float ntZ = Z / 100 ; //Z compris entre 0 et 108.883
float ntR = ntX * 3.2406 + ntY * (-1.5372) + ntZ * (-0.4986) ;
float ntG = ntX * (-0.9689) + ntY * 1.8758 + ntZ * 0.0415 ;
float ntB = ntX * 0.0557 + ntY * (-0.2040) + ntZ * 1.0570 ;
if ( ntR > 0.0031308 ) {
ntR = 1.055 * ( pow(ntR,( 1 / 2.4 )) ) - 0.055 ;
} else { ntR = 12.92 * ntR ; }
if ( ntG > 0.0031308 ) {
ntG = 1.055 * ( pow(ntG,( 1 / 2.4 )) ) - 0.055 ;
} else { ntG = 12.92 * ntG ; }
if ( ntB > 0.0031308 ) {
ntB = 1.055 * ( pow(ntB,( 1 / 2.4 )) ) - 0.055 ;
} else { ntB = 12.92 * ntB ; }
R = ntR * 255 ;
G = ntG * 255 ;
B = ntB * 255 ;
colorRGB[0] = R ;
colorRGB[1] = G ;
colorRGB[2] = B ;
return colorRGB ;
}
// I know that with RGB, not every visible color is possible
//so I just made this quick function, to bound RGB values between 0 and 255
float[] arrondirRGB () {
for (int i=0;i<3;i++) {
if (colorRGB[i]>255) {
colorRGB[i]=255 ;
}
if (colorRGB[i]<0) {
colorRGB[i]=0 ;
}
}
return colorRGB;
}
// operating section
void draw () {
nuancier.beginDraw();
nuancier.noSmooth();
nuancier.colorMode(RGB, 255);
nuancier.endDraw();
for (x=0;x<taille;x++) {
for (y=0;y<taille;y++) {
colorLAB[0] = (((taille-y)*100)/taille) ; // --------------------------------------------------------------- valeur 100 // formule ((x*100)/taille)
colorLAB[1] = 0 ; // ----------------------------------------------------------- valeur 0 // formule ((x*256)/taille)-127
colorLAB[2] = (((x)*256)/taille)-127 ; // -------------------------------------------------- valeur 0 // (((taille-y)*256)/taille)-127
println(colorLAB[0]);
LABtoXYZ () ;
XYZtoRGB () ;
arrondirRGB () ;
nuancier.beginDraw();
nuancier.stroke (colorRGB[0],colorRGB[1],colorRGB[2]);
nuancier.point (x,y);
nuancier.endDraw();
}
}
nuancier.save("nuancier.tiff");
println("done !");
}
Ok I found out !
The problem was dividing by integers.
I don't know if it works like that in other languages, but in processing if you write
x = 2/5
the result will be x = 0 instead of x = 0.4 ; it's because with the denominator being an integer, the result will always be an integer .... so
x = 2/5.0
will give x = 0.4 !
I had to put a ".0" after every integer dividing, and turn to float any integer data that would divide.
The result is perfect, no more problems !
https://github.com/processing/processing/wiki/Troubleshooting#Why_does_2_.2F_5_.3D_0_instead_of_0.4.3F

Not able to set processor affinity

I'm trying to implement this code on a 8 core cluster. It has 2 sockets each with 4 cores. I am trying to create 8 threads and set affinity using pthread_attr_setaffinity_np function. But when I look at my performance in VTunes , it shows me that 3969 odd threads are being created. I don't understand why and how! Above all, my performance is exactly the same as it was when no affinity was set (OS thread scheduling). Can someone please help me debug this problem? My code is running perfectly fine but I have no control over the threads! Thanks in advance.
--------------------------------------CODE-------------------------------------------
const int num_thrd=8;
bool RCTAlgorithmBackprojection(RabbitCtGlobalData* r)
{
float O_L = r->O_L;
float R_L = r->R_L;
double* A_n = r->A_n;
float* I_n = r->I_n;
float* f_L = r->f_L;*/
cpu_set_t cpu[num_thrd];
pthread_t thread[num_thrd];
pthread_attr_t attr[num_thrd];
for(int i =0; i< num_thrd; i++)
{
threadCopy[i].L = r->L;
threadCopy[i].O_L = r->O_L;
threadCopy[i].R_L = r->R_L;
threadCopy[i].A_n = r->A_n;
threadCopy[i].I_n = r->I_n;
threadCopy[i].f_L = r->f_L;
threadCopy[i].slice= i;
threadCopy[i].S_x = r->S_x;
threadCopy[i].S_y = r->S_y;
pthread_attr_init(&attr[i]);
CPU_ZERO(&cpu[i]);
CPU_SET(i, &cpu[i]);
pthread_attr_setaffinity_np(&attr[i], CPU_SETSIZE, &cpu[i]);
int rc=pthread_create(&thread[i], &attr[i], backProject, (void*)&threadCopy[i]);
if (rc!=0)
{
cout<<"Can't create thread\n"<<endl;
return -1;
}
// sleep(1);
}
for (int i = 0; i < num_thrd; i++) {
pthread_join(thread[i], NULL);
}
//s_rcgd = r;
return true;
}
void* backProject (void* parm)
{
copyStruct* s = (copyStruct*)parm; // retrive the slice info
unsigned int L = s->L;
float O_L = s->O_L;
float R_L = s->R_L;
double* A_n = s->A_n;
float* I_n = s->I_n;
float* f_L = s->f_L;
int slice1 = s->slice;
//cout<<"The size of volume is L= "<<L<<endl;
int from = (slice1 * L) / num_thrd; // note that this 'slicing' works fine
int to = ((slice1+1) * L) / num_thrd; // even if SIZE is not divisible by num_thrd
//cout<<"computing slice " << slice1<< " from row " << from<< " to " << to-1<<endl;
for (unsigned int k=from; k<to; k++)
{
double z = O_L + (double)k * R_L;
for (unsigned int j=0; j<L; j++)
{
double y = O_L + (double)j * R_L;
for (unsigned int i=0; i<L; i++)
{
double x = O_L + (double)i * R_L;
double w_n = A_n[2] * x + A_n[5] * y + A_n[8] * z + A_n[11];
double u_n = (A_n[0] * x + A_n[3] * y + A_n[6] * z + A_n[9] ) / w_n;
double v_n = (A_n[1] * x + A_n[4] * y + A_n[7] * z + A_n[10]) / w_n;
f_L[k * L * L + j * L + i] += (float)(1.0 / (w_n * w_n) * p_hat_n(u_n, v_n));
}
}
}
//cout<<" finished slice "<<slice1<<endl;
return NULL;
}
Alright, so I found out the reason was because of CPU_SETSIZE that I was using as an argument in pthread_attr_setaffinity_np. I replaced it with num_thrd . Apparently CPU_SETSIZE which will be declared inside #define __USE_GNU was not included in my file.!! Sorry if I bothered any of y'all who were trying to debug this thanks again!

j2me drawChar abnormal character spacing?

I write some code to draw a text on a j2me canvas without using drawString.
For some reasons I can't use drawString method.
So, when I run my program, I deal with abnormal character spacing.
Please help me to solve the problem. This is my code:
public void paint(Graphics g) {
...
String str = ... ;
int x0 = 10;
int y0 = getHeight() - 50;
Font f = g.getFont();
int charWidth = 0;
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
charWidth = f.charWidth(ch);
x0 += charWidth;
g.drawChar(ch, x0, y0, 0);
}
...
}
instead use this:
public void paint(Graphics g) {
...
String str = ... ;
int x0 = 10;
int y0 = getHeight() - 50;
Font f = g.getFont();
int lastWidth = 0;
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
g.drawChar(ch, x0 + lastWidth, y0, 0);
lastWidth += f.charWidth(ch);
}
...
}
In your drawChar method,you use 0(it is equal to Graphics.TOP|Graphics.LEFT) so you would increase "lastWidth" after draw current char,or use another anchor(for example Graphics.TOP|Graphics.RIGHT) for drawChar.

Resources