Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

openmp

$
0
0
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);


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>