Skip to content

Commit 0ab2baa

Browse files
author
Solarian
committed
initial commit
0 parents  commit 0ab2baa

10 files changed

+617
-0
lines changed

README

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Here you could find the code for "C++11 thread tutorial", for more informations visit the project webpage:
2+
3+
http://solarianprogrammer.com/2011/12/16/cpp-11-thread-tutorial/
4+
5+
In order to use the image_processing code you will need a picture in ppm format, you can easily find examples
6+
on the Internet, or you could use Gimp to save your own picture in ppm format.
7+
8+
You could use this program under the terms of GPL v3, for more details see:
9+
10+
http://www.gnu.org/copyleft/gpl.html
11+
12+
Copyright 2011 Sol from www.solarianprogrammer.com

cpp_thread_00.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//Create a C++11 thread from the main program
2+
3+
4+
#include <iostream>
5+
#include <thread>
6+
7+
using namespace std;
8+
9+
//This function will be called from a thread
10+
void call_from_thread(){
11+
cout<<"Launched by thread"<<endl;
12+
}
13+
14+
15+
int main(){
16+
//Launch a thread
17+
thread t1 = thread(call_from_thread);
18+
19+
//Join the thread with the main thread
20+
t1.join();
21+
return(0);
22+
}

cpp_thread_01.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//Create a group of C++11 threads from the main program
2+
3+
#include <iostream>
4+
#include <thread>
5+
6+
using namespace std;
7+
8+
#define NUM_THREADS 10
9+
10+
//This function will be called from a thread
11+
void call_from_thread(){
12+
cout<<"Launched by thread\n";
13+
}
14+
15+
int main(){
16+
thread *t = new thread[NUM_THREADS];
17+
18+
//Launch a group of threads
19+
for(int i=0;i<NUM_THREADS;i++){
20+
t[i] = thread(call_from_thread);
21+
}
22+
23+
cout<<"Launched from the main\n";
24+
25+
//Join the threads with the main thread
26+
for(int i=0;i<NUM_THREADS;i++){
27+
t[i].join();
28+
}
29+
30+
delete [] t;
31+
return(0);
32+
}

cpp_thread_02.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//Create a group of C++11 threads from the main program
2+
3+
#include <iostream>
4+
#include <thread>
5+
6+
using namespace std;
7+
8+
#define NUM_THREADS 10
9+
10+
//This function will be called from a thread
11+
void call_from_thread(int tid){
12+
cout<<"Launched by thread "<<tid<<endl;
13+
}
14+
15+
int main(){
16+
thread *t = new thread[NUM_THREADS];
17+
18+
//Launch a group of threads
19+
for(int i=0;i<NUM_THREADS;i++){
20+
t[i] = thread(call_from_thread,i);
21+
}
22+
23+
cout<<"Launched from the main\n";
24+
25+
//Join the threads with the main thread
26+
for(int i=0;i<NUM_THREADS;i++){
27+
t[i].join();
28+
}
29+
30+
delete [] t;
31+
return(0);
32+
}

image_processing/ppm.cpp

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
#include <iostream>
2+
#include <fstream>
3+
#include <string>
4+
#include <sstream>
5+
#include <exception>
6+
7+
#include "ppm.h"
8+
9+
//init with default values
10+
void ppm::init(){
11+
flag_alloc = false;
12+
width=0;
13+
height=0;
14+
max_col_val=255;
15+
}
16+
17+
//create a PPM object
18+
ppm::ppm(){
19+
init();
20+
}
21+
22+
//create a PPM object and fill it with data stored in fname
23+
ppm::ppm(const std::string &fname){
24+
init();
25+
read(fname);
26+
}
27+
28+
//create an "epmty" PPM image with a given width and height;the R,G,B arrays are filled with zeros
29+
ppm::ppm(const unsigned int _width, const unsigned int _height){
30+
init();
31+
width = _width;
32+
height = _height;
33+
nr_lines = height;
34+
nr_columns = width;
35+
size = width*height;
36+
37+
r = new unsigned char[size];
38+
g = new unsigned char[size];
39+
b = new unsigned char[size];
40+
flag_alloc = true;
41+
42+
for(unsigned int i = 0; i < size; i++){
43+
r[i] = 0;
44+
g[i] = 0;
45+
b[i] = 0;
46+
}
47+
}
48+
49+
//free the memory used by the R,G,B vectors when the object is destroyed
50+
ppm::~ppm(){
51+
if(flag_alloc){
52+
delete [] r;
53+
delete [] g;
54+
delete [] b;
55+
}
56+
}
57+
58+
//read the PPM image from fname
59+
void ppm::read(const std::string &fname){
60+
std::ifstream inp(fname.c_str(), std::ios::in|std::ios::binary);
61+
if(inp.is_open()){
62+
std::string line;
63+
std::getline(inp,line);
64+
if(line != "P6"){
65+
std::cout<<"Error. Unrecognized file format."<<std::endl;
66+
return;
67+
}
68+
std::getline(inp,line);
69+
while(line[0]=='#'){
70+
std::getline(inp,line);
71+
}
72+
std::stringstream dimensions(line);
73+
74+
try{
75+
dimensions>>width;
76+
dimensions>>height;
77+
nr_lines = height;
78+
nr_columns = width;
79+
}
80+
catch(std::exception &e){
81+
std::cout<<"Header file format error. "<<e.what()<<std::endl;
82+
return;
83+
}
84+
85+
std::getline(inp,line);
86+
std::stringstream max_val(line);
87+
try{
88+
max_val>>max_col_val;
89+
}
90+
catch(std::exception &e){
91+
std::cout<<"Header file format error. "<<e.what()<<std::endl;
92+
return;
93+
}
94+
95+
size = width*height;
96+
97+
r = new unsigned char[size];
98+
g = new unsigned char[size];
99+
b = new unsigned char[size];
100+
flag_alloc = true;
101+
102+
char aux;
103+
for(unsigned int i = 0; i < size; i++){
104+
inp.read(&aux,1);
105+
r[i] = (unsigned char) aux;
106+
inp.read(&aux,1);
107+
g[i] = (unsigned char) aux;
108+
inp.read(&aux,1);
109+
b[i] = (unsigned char) aux;
110+
}
111+
}
112+
else{
113+
std::cout<<"Error. Unable to open "<<fname<<std::endl;
114+
}
115+
inp.close();
116+
}
117+
118+
//write the PPM image in fname
119+
void ppm::write(const std::string &fname){
120+
std::ofstream inp(fname.c_str(), std::ios::out|std::ios::binary);
121+
if(inp.is_open()){
122+
123+
inp<<"P6\n";
124+
inp<<width;
125+
inp<<" ";
126+
inp<<height<<"\n";
127+
inp<<max_col_val<<"\n";
128+
129+
char aux;
130+
for(unsigned int i = 0; i < size; i++){
131+
aux = (char)r[i];
132+
inp.write(&aux,1);
133+
aux = (char)g[i];
134+
inp.write(&aux,1);
135+
aux = (char)b[i];
136+
inp.write(&aux,1);
137+
}
138+
}
139+
else{
140+
std::cout<<"Error. Unable to open "<<fname<<std::endl;
141+
}
142+
inp.close();
143+
}
144+

image_processing/ppm.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//Process a binary PPM file
2+
class ppm{
3+
bool flag_alloc;
4+
void init();
5+
//info about the PPM file (height and width)
6+
unsigned int nr_lines;
7+
unsigned int nr_columns;
8+
9+
public:
10+
//arrays for storing the R,G,B values
11+
unsigned char *r;
12+
unsigned char *g;
13+
unsigned char *b;
14+
//
15+
unsigned int height;
16+
unsigned int width;
17+
unsigned int max_col_val;
18+
//total number of elements (pixels)
19+
unsigned int size;
20+
21+
ppm();
22+
//create a PPM object and fill it with data stored in fname
23+
ppm(const std::string &fname);
24+
//create an "epmty" PPM image with a given width and height;the R,G,B arrays are filled with zeros
25+
ppm(const unsigned int _width, const unsigned int _height);
26+
//free the memory used by the R,G,B vectors when the object is destroyed
27+
~ppm();
28+
//read the PPM image from fname
29+
void read(const std::string &fname);
30+
//write the PPM image in fname
31+
void write(const std::string &fname);
32+
};

0 commit comments

Comments
 (0)