4 Replies - 343 Views - Last Post: 03 February 2013 - 09:24 AM Rate Topic: -----

#1 gwnabaprmr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 08-August 08

openmp

Posted 01 February 2013 - 10:06 AM

Hello I have made a code for serial conversion of a tga picture from rgb to colour and blackwhite
I would like to ask what to do to make this with threads and openmp
Just some hints offcourse not the code
Here is the code
Thanks in advance!

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "tga_library.h"

void Display_Header (TGA_HEADER header){

   printf("ID length:         %d\n",header.idlength);
   printf("Colourmap type:    %d\n",header.colourmaptype);
   printf("Image type:        %d\n",header.datatypecode);
   printf("Colour map offset: %d\n",header.colourmaporigin);
   printf("Colour map length: %d\n",header.colourmaplength); 
   printf("Colour map depth:  %d\n",header.colourmapdepth);
   printf("X origin:          %d\n",header.x_origin);
   printf("Y origin:          %d\n",header.y_origin);
   printf("Width:             %d\n",header.width);
   printf("Height:            %d\n",header.height);
   printf("Bits per pixel:    %d\n",header.bitsperpixel);
   printf("Descriptor:        %d\n",header.imagedescriptor);

}

TGA_HEADER Get_Header (FILE *fptr){

	TGA_HEADER header;

	/* Retrieve the header fields */
	header.idlength = fgetc(fptr);
	header.colourmaptype = fgetc(fptr);  
	header.datatypecode = fgetc(fptr);
	fread(&header.colourmaporigin,2,1,fptr);
	fread(&header.colourmaplength,2,1,fptr);
	header.colourmapdepth = fgetc(fptr);
	fread(&header.x_origin,2,1,fptr);
	fread(&header.y_origin,2,1,fptr);
	fread(&header.width,2,1,fptr);
	fread(&header.height,2,1,fptr);
	header.bitsperpixel = fgetc(fptr);
	header.imagedescriptor = fgetc(fptr);

	return header;
}

void Set_Header (FILE *fptr, TGA_HEADER header){

	putc(header.idlength, fptr);
	//header.idlength = fgetc(fptr);
	putc(header.colourmaptype, fptr);
	//header.colourmaptype = fgetc(fptr);  
	putc(header.datatypecode, fptr);
	//header.datatypecode = fgetc(fptr);
	putc((header.colourmaporigin & 0x00FF), fptr);
	putc((header.colourmaporigin & 0xFF00)/256, fptr);
	//fread(&header.colourmaporigin,2,1,fptr);
	putc((header.colourmaplength & 0x00FF), fptr);
	putc((header.colourmaplength & 0xFF00)/256, fptr);
	//fread(&header.colourmaplength,2,1,fptr);
	putc(header.colourmapdepth, fptr);
	//header.colourmapdepth = fgetc(fptr);
	putc((header.x_origin & 0x00FF), fptr);
	putc((header.x_origin & 0xFF00)/256, fptr);
	//fread(&header.x_origin,2,1,fptr);
	putc((header.y_origin & 0x00FF), fptr);
	putc((header.y_origin & 0xFF00)/256, fptr);
	//fread(&header.y_origin,2,1,fptr);
	putc((header.width & 0x00FF), fptr);
	putc((header.width & 0xFF00)/256, fptr);
	//fread(&header.width,2,1,fptr);
	putc((header.height & 0x00FF), fptr);
	putc((header.height & 0xFF00)/256, fptr);
	//fread(&header.height,2,1,fptr);
	putc(header.bitsperpixel, fptr);
	//header.bitsperpixel = fgetc(fptr);
	putc(header.imagedescriptor, fptr);
	//header.imagedescriptor = fgetc(fptr);
}

PIXEL Get_Pixel(int x, int y, FILE* fptr, TGA_HEADER header) {

	int offset;
	PIXEL pixel;

	offset = 18*sizeof(char);
	offset += y*header.width*3*sizeof(char);
	offset += x*3*sizeof(char);
	fseek(fptr, offset, SEEK_SET);

	pixel.b = fgetc(fptr);
	pixel.g = fgetc(fptr);
	pixel.r = fgetc(fptr);

	return pixel;
}

void Set_Pixel(int x, int y, PIXEL pixel, FILE* fptr, TGA_HEADER header) {
	
	int offset;

	offset = 18*sizeof(char);
	offset += y*header.width*3*sizeof(char);
	offset += x*3*sizeof(char);
	fseek(fptr, offset, SEEK_SET);

	putc(pixel.b, fptr);
	putc(pixel.g, fptr);
	putc(pixel.r, fptr);
}

unsigned char Get_Grey_Pixel(int x, int y, FILE* fptr, TGA_HEADER header) {

	int offset;
	unsigned char pixel;

	offset = 18*sizeof(char);
	offset += y*header.width*sizeof(char);
	offset += x*sizeof(char);
	fseek(fptr, offset, SEEK_SET);

	pixel = fgetc(fptr);

	return pixel;
}

void Set_Grey_Pixel(int x, int y, unsigned char pixel, FILE* fptr, TGA_HEADER header) {
	
	int offset;

	offset = 18*sizeof(char);
	offset += y*header.width*sizeof(char);
	offset += x*sizeof(char);
	fseek(fptr, offset, SEEK_SET);

	putc(pixel, fptr);
}



 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "tga_library.h"

void main (int argc, char *argv[]){
	TGA_HEADER header;	
	PIXEL pixel;
	unsigned char oldpixel, newpixel, temppixel;
	int quant_error;
	int x,y, name_length;
	FILE *fptr, *gs_fptr, *bw_fptr;
	char gs_fileName[100], bw_fileName[100];
	unsigned char grey_pixel;
	TGA_HEADER gs_header, bw_header;
	
	if (argc < 2) {
		fprintf(stderr,"Usage: %s tgafile\n",argv[0]);
		exit(-1);
	}

	/* Open the original file */
	if ((fptr = fopen(argv[1],"r")) == NULL) {
		fprintf(stderr,"File open failed\n");
		exit(-1);
	}

	// Create name of gray-scale image
	name_length = strlen(argv[1]);
	argv[1][name_length-4]='\0';
	sprintf(gs_fileName, "%s-grey.tga", argv[1]);
	// Open grey-scale image file
	if ((gs_fptr = fopen(gs_fileName,"w")) == NULL) {
		fprintf(stderr,"File open failed\n");
		exit(-1);
	}
	// Create name of b&w image (dithered)
	sprintf(bw_fileName, "%s-bw.tga", argv[1]);
	// Open blackwhite image file
	if ((bw_fptr = fopen(bw_fileName,"w+")) == NULL) {
		fprintf(stderr,"File open failed\n");
		exit(-1);
	}

	// Get header of original file
	header = Get_Header(fptr);

	// Create header for grey-scale image
	gs_header = header;
	gs_header.datatypecode = 3;
	gs_header.bitsperpixel = 8;
	// Write grey-scale header to grey-scale file
	Set_Header(gs_fptr, gs_header);

	// Create header for b&w image
	bw_header = header;
	bw_header.datatypecode = 3;
	bw_header.bitsperpixel = 8;
	// Write b&w header to b&w file
	Set_Header(bw_fptr, bw_header);

	// Create grey-scale image
	for(y=0; y<header.height; y++) {
		for (x=0; x<header.width; x++) {
			//Gray = 0.299 * Red + 0.587 * Green + 0.114 * Blue
			pixel = Get_Pixel(x, y, fptr, header);	
			grey_pixel = 0.299 * pixel.r + 0.587 * pixel.g + 0.114 * pixel.b; 

			//printf("%d/%d\n", y*header.width+x, header.width*header.height);
	//		printf("%d\n",grey_pixel);
			Set_Grey_Pixel(x, y, grey_pixel, gs_fptr, gs_header);
			Set_Grey_Pixel(x, y, grey_pixel, bw_fptr, bw_header);
		}
	}
	
	// Create black-white image
	for (y=0;y<header.height;y++) {
		for(x=0;x<header.width;x++) {
			oldpixel = Get_Grey_Pixel(x, y, bw_fptr, bw_header);
			
			if ( oldpixel < 128 ){
    			newpixel = 0;
			}
			else{
				newpixel = 255;
			}
			Set_Grey_Pixel(x, y, newpixel, bw_fptr, bw_header);
			quant_error = oldpixel - newpixel;
			printf("%d - %d = %d\n",oldpixel, newpixel, quant_error);
			//printf("%d\n",oldpixel);
			
			if(x<header.width){
			temppixel = Get_Grey_Pixel(x+1, y, bw_fptr, bw_header); 
			temppixel = temppixel + 7/16*quant_error;
			Set_Grey_Pixel(x+1, y, temppixel, bw_fptr, bw_header);		
			}
			if(y<header.height){
			temppixel = Get_Grey_Pixel(x-1, y+1, bw_fptr, bw_header); 
			temppixel = temppixel + 3/16*quant_error;
			Set_Grey_Pixel(x-1, y+1, temppixel, bw_fptr, bw_header);		
			}
			if(y<header.height){
			temppixel = Get_Grey_Pixel(x, y+1, bw_fptr, bw_header); 
			temppixel = temppixel + 5/16*quant_error;
			Set_Grey_Pixel(x, y+1, temppixel, bw_fptr, bw_header);		
			}
			if((x+1<header.width)&&(y+1<header.height)){
			temppixel = Get_Grey_Pixel(x+1, y+1, bw_fptr, bw_header); 
			temppixel = temppixel + 1/16*quant_error;
			Set_Grey_Pixel(x+1, y+1, temppixel, bw_fptr, bw_header);		
			}
		}
		
	}

	fclose(fptr);
	fclose(gs_fptr);
	fclose(bw_fptr);
}



typedef struct {
   char  idlength;
   char  colourmaptype;
   char  datatypecode;
   short int colourmaporigin;
   short int colourmaplength;
   char  colourmapdepth;
   short int x_origin;
   short int y_origin;
   short width;
   short height;
   char  bitsperpixel;
   char  imagedescriptor;
} TGA_HEADER;


typedef struct {
   unsigned char r,g,b;
} PIXEL;

void Display_Header (TGA_HEADER header);
void Set_Header (FILE *fptr, TGA_HEADER header);
TGA_HEADER Get_Header (FILE *fptr);
PIXEL Get_Pixel(int x, int y, FILE* fptr, TGA_HEADER header);
void Set_Pixel(int x, int y, PIXEL pixel, FILE* fptr, TGA_HEADER header);
unsigned char Get_Grey_Pixel(int x, int y, FILE* fptr, TGA_HEADER header);
void Set_Grey_Pixel(int x, int y, unsigned char pixel, FILE* fptr, TGA_HEADER header);



Is This A Good Question/Topic? 0
  • +

Replies To: openmp

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3622
  • View blog
  • Posts: 11,290
  • Joined: 05-May 12

Re: openmp

Posted 01 February 2013 - 12:14 PM

Well, the dead simple approach is to run the b&w and grey scale in their own threads. That way you can technically say you are using threads. Now, to truly do parallel processing, within those threads, you can partition the image areas to be processed into smaller parts and do the conversion of those parts in parallel using openmp.
Was This Post Helpful? 1
  • +
  • -

#3 gwnabaprmr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 08-August 08

Re: openmp

Posted 03 February 2013 - 02:53 AM

Can you be little bit more specific
what I suppose to do for creating threads
for example how can I use pthread create
or how can I use openmp
Thanks!
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3622
  • View blog
  • Posts: 11,290
  • Joined: 05-May 12

Re: openmp

Posted 03 February 2013 - 08:35 AM

What tutorials our documentation about threads and OpenMP have you read? I don't mean to demean you, but most of the tutorials I read/scanned were a good enough basis for me to extrapolate how I would use the same concepts for other problem spaces.

It feels like you have not done your research, and are asking us to do your research and coding for you.

Sent from my T-Mobile G2 using Tapatalk 2
Was This Post Helpful? 0
  • +
  • -

#5 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 810
  • View blog
  • Posts: 2,407
  • Joined: 20-March 10

Re: openmp

Posted 03 February 2013 - 09:24 AM

Hi,


I think the simplest way to do this is to run a

omp parallel

then tun two omp singles

inside two loops

the two-for-loops.c example would seem adequate for what you want to do.

Snoopy.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1