I'm rotating a bmp image file in C.
I successfully tested copying image(rotate 0 degree). But, It must be a problem in rotating image. Here is my code
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#pragma warning (disable:4996)
#define FILENAMELENGTH 30
#define MAXROW 512
#define MAXCOL 512
//location of pixels
typedef struct
{
int row;
int col;
}COORDI;
//function declarations
void RotatePixel(float Theta, COORDI org, COORDI before, COORDI* after);
void ReadImage(char* original_image);
void WriteImage(char* rotate_image);
char source[MAXROW][MAXCOL];
char dest[MAXROW][MAXCOL];
COORDI org;
float Theta;
int main(void)
{
char original_image[FILENAMELENGTH];
char rotate_image[FILENAMELENGTH];
printf("image name : ");
scanf("%s", original_image);
//rotate angle(radian)
printf("angle : ");
scanf("%f", &Theta);
printf("after rotate file name : ");
scanf("%s", rotate_image);
org.row = MAXROW / 2;
org.col = MAXCOL / 2;
dest[MAXROW][MAXCOL] = { 0, };
ReadImage(original_image);
WriteImage(rotate_image);
printf("rotate completed!\n");
return 0;
}
void ReadImage(char* original_image)
{
FILE* fp;
int i, j;
fp = fopen(original_image, "rb");
if (fp == NULL)
{
printf("File cannot open\n");
exit(-1);
}
for (i = 0; i < MAXROW; i++)
{
for (j = 0; j < MAXCOL; j++)
fread(&source[i][j], sizeof(unsigned char), 1, fp);
}
printf("image read completed !\n");
fclose(fp);
return;
}
//writing a pixel to file
void WriteImage(char* rotate_image)
{
FILE* fp;
int i, j;
COORDI locn;
COORDI* after = (COORDI*)malloc(sizeof(COORDI));
fp = fopen(rotate_image, "wb");
if (fp == NULL)
{
printf("File cannot open\n");
exit(-1);
}
for (i = 0; i < MAXROW; i++)
{
for (j = 0; j < MAXCOL; j++)
{
locn.row = i;
locn.col = j;
RotatePixel(Theta, org, locn, after);
if (after->col < 0) continue;
if (after->row < 0) continue;
if (after->col >= MAXCOL) continue;
if (after->row >= MAXROW) continue;
dest[after->row][after->col] = source[i][j];
putc(dest[after->row][after->col], fp);
}
}
printf("image write completed !\n");
fclose(fp);
return;
}
//rotate pixels
void RotatePixel(float Theta, COORDI org, COORDI before, COORDI* after)
{
int x, y;
x = before.col - org.col;
y = before.row - org.row;
after->col = (int)(x* cos(Theta) - y*sin(Theta)) + org.col; // x
after->row = (int)(x* sin(Theta) + y*cos(Theta)) + org.row; // y
}
I think writing a pixel to file is a problem. where is the missing point?
Related
I believe I have a problem with my structure pointer (head), but I'm not sure where I'm doing wrong. It reads the data from the file fine for the headercheck function so I have no clue if that's it or not. I would rather have the structure pointer global, because this is part of a very big program with switch case statements and all that, but it doesn't like that.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define maxheight 1080
#define maxwidth 1920
#define RGB_COMPONENT_COLOUR 255
#define pgmtype "P2"
#define ppmtype "P3"
typedef struct {
int x, y;
}
PPMImage;
typedef struct {
int rgb_comp_colour;
char filetype[2];
int height;
int width;
}
PPMHead;
/*PPMHead head[3];
{
head[3].filetype;
head[3].height;
head[3].width;
}
PPMHead *ptr_head = NULL;
ptr_head = head;*/
PPMPixel * data; //Defines pointer to PPMPixel
int ** Array; //Double pointer defines as a pointer pointing to a pointer that is pointing to an integer
PPMPixel ** RGBArray; //Double pointer defines as a pointer pointing to a pointer that is pointing to the PPMPixel structure
FILE * fp;
int r, g, b;
void headercheck() {
PPMHead * ptr_head, head;
ptr_head = & head;
fscanf(fp, "%s %d %d %d", head.filetype, & head.width, & head.height, & head.rgb_comp_colour);
printf("%s %d %d %d", head.filetype, head.width, head.height, head.rgb_comp_colour);
if (head.width > maxwidth || head.height > maxheight) {
printf("\tInvalid image size. The maximum value of image is 1920x1080.\n");
printf("\tImage size is %d x %d\n", head.width, head.height);
} else {
printf("\tImage size is valid\n");
printf("\tImage size is %d x %d\n", head.width, head.height);
}
if ((strcmp(head.filetype, pgmtype) != 0) && (strcmp(head.filetype, ppmtype) != 0)) {
printf("\tInvalid filetype\n");
} else {
if (strcmp(head.filetype, pgmtype) == 0) {
printf("\t File is PGM type image\n");
} else {
if (strcmp(head.filetype, ppmtype) == 0) {
printf("\t File is PPM type image\n");
}
}
}
if ((head.rgb_comp_colour == RGB_COMPONENT_COLOUR)) {
printf("\t Image is 8 bit\n");
} else {
if (head.rgb_comp_colour > RGB_COMPONENT_COLOUR) {
printf("Maximum bit-depth is 8 bits\n");
} else {
printf("\tImage is not 8 bit\n");
}
}
}
int main(void) {
PPMHead * ptr_head, head;
ptr_head = & head;
char fname[100];
printf("Enter file name: ");
scanf("%s", fname);
fseek(stdin, 0, SEEK_END);
fp = fopen(fname, "r");
if (fp == NULL) {
printf("\tError while opening the file\n");
} else {
printf("\tReading in %s\n", fname);
}
//Checking for comments
//Skip whitespace and comments
headercheck();
if (strcmp(head.filetype, pgmtype) == 0) {
printf("\tReading in PGM image...\n");
* Array = malloc(head.width * sizeof(int * )); //Points to malloc
for (int i = 0; i < head.width; i++) {
Array[i] = (int * ) malloc(head.height * sizeof(int));
if ((Array = NULL)) {
printf("Error allocating memory to the array");
} else {
printf("Memory allocated to the PGM array sucessfully");
}
}
for (int j = 0; j < head.height; j++) {
for (int i = 0; i < head.width; i++) {
fscanf(fp, "%3d ", & Array[i][j]); //Scans the address of Array[i][j]
}
}
}
fclose(fp);
//Save PPM Array Into New PPM File
FILE * pf;
int i, j;
char fname2[100];
printf("Enter file name: ");
scanf("%s", fname2);
fseek(stdin, 0, SEEK_END);
pf = fopen(fname2, "w");
if (pf == NULL) {
printf("\tError while opening the file\n");
} else {
printf("\tWriting in %s\n", fname2);
}
if (strcmp(head.filetype, pgmtype) == 0) {
for (j = 0; j < head.height; j++) {
fprintf(pf, "\n");
for (i = 0; i < head.width; i++) {
fprintf(pf, "%3d ", Array[i][j]);
//fprintf(pf, "%3d ", **(Array+j*head[0].width + i));
}
}
}
fclose(pf);
for (int i = 0; i < head.width; i++) {
free(Array[i]);
Array[i] = NULL;
}
free(Array);
Array = NULL;
return 0;
}
I'm trying to Heapsort (sort by Alphabetical Order the name of the phone) a structure of char array read from a txt file. My algorithm works for integer but when I change to 'char' type it don't show any result nor any error
Therefore I really dont know what's wrong with my code. Please help, I'm new to this.
My txt file
iPhone_XR 128 6.1 599
Galaxy_s20 256 5.8 599
oppo_find_x 128 4.7 429
iPhone_SE 128 4.0 349
My code
#include <stdio.h>
struct phone
{
char a[100];
char b[100];
char c[100];
char d[100];
};
struct phone array[100];
void swap(char *e, char *f)
{
char temp = *e;
*e = *f;
*f = temp;
}
void heapify(char arr[], int n, int i)
{
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < n && arr[left] > arr[largest])
largest = left;
if (right < n && arr[right] > arr[largest])
largest = right;
if (largest != i)
{
swap(&arr[i], &arr[largest]);
heapify(arr, n, largest);
}
}
void heapSort(char arr[], int size)
{
for (int i = size / 2 - 1; i >= 0; i--)
heapify(arr, size, i);
for (int i = size - 1; i >= 0; i--) {
swap(&arr[0], &arr[i]);
heapify(arr, i, 0);
}
}
int main(void)
{
int size;
char ch;
int count = 0;
char A[1000];
FILE *myfile = fopen("phonedb.txt", "r");
if (myfile == NULL) {
printf("Cannot open file.\n");
return 1;
}
else {
do //count lines
{
ch = fgetc(myfile);
if (ch == '\n') count++;
} while (ch != EOF);
rewind(myfile);
// scan all the line inside the text
int i;
for (i = 0; i < count; i++) {
fscanf(myfile, "%s %s %s %s\n", array[i].a, array[i].b, array[i].c, array[i].d);
printf("%s %s %s %s\n", array[i].a, array[i].b, array[i].c, array[i].d);
}
}
heapSort(A, count);
printf("\nYour sorted list\n");
for (int i=0; i<size; i++)
{
printf("%s\n", array[i].a);
}
return 0;
}
Your program has lot of errors and some unnecessary use of char arrays.
But since you are sorting your data using phone names which is a string,
direct comparison won't work as it's not a primitive data type. You need to use library function strcmp() from <string.h> library.
Here's my working code.
#include<stdio.h>
#include<string.h>
struct phone
{
char phoneName[100];
int second,fourth;
float third;
};
struct phone array[100];
void swap(struct phone *e, struct phone *f)
{
struct phone temp = *e;
*e = *f;
*f = temp;
}
void heapify(struct phone arr[], int n, int i)
{
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < n && strcmp(arr[left].phoneName, arr[largest].phoneName)>0)
largest = left;
if (right < n && strcmp(arr[right].phoneName, arr[largest].phoneName)>0)
largest = right;
if (largest != i)
{
swap(&arr[i], &arr[largest]);
heapify(arr, n, largest);
}
}
void heapSort(struct phone arr[], int size)
{
for (int i = size / 2 - 1; i >= 0; i--)
heapify(arr, size, i);
for (int i = size - 1; i >= 0; i--) {
swap(&arr[0], &arr[i]);
heapify(arr, i, 0);
}
}
int main()
{
int size;
char ch;
int count = 0;
FILE *myfile = fopen("phonedb.txt", "r");
if (myfile == NULL) {
printf("Cannot open file.\n");
return 1;
}
else {
do //count lines
{
ch = fgetc(myfile);
if (ch == '\n') count++;
} while (ch != EOF);
rewind(myfile);
// scan all the line inside the text
int i;
for (i = 0; i < count; i++) { // using floating point with 2 precision.
fscanf(myfile, "%s %d %f %d\n", array[i].phoneName, &array[i].second, &array[i].third, &array[i].fourth);
printf("%s %d %0.2f %d\n", array[i].phoneName, array[i].second, array[i].third, array[i].fourth);
}
}
heapSort(array, count);
printf("\nYour sorted list\n");
for (int i=0; i<count; i++)
{
printf("%s %d %0.3f %d\n", array[i].phoneName,array[i].second,array[i].third,array[i].fourth);
}
return 0;
}
And Here's the output:
iPhone_XR 128 6.10 599
Galaxy_s20 256 5.80 599
oppo_find_x 128 4.70 429
iPhone_SE 128 4.00 349
Your sorted list
Galaxy_s20 256 5.80 599
iPhone_SE 128 4.00 349
iPhone_XR 128 6.10 599
oppo_find_x 128 4.70 429
I have an Instance File from which I need to store the NUM_PT and all the respective co-ordinates in the form of a 2D array system (personal choice so I can access them easily). I am able to retrieve the NUM_PT but I am stuck at reading the successive cordinates into my array.
HERE IS WHAT I HAVE DONE
/* Assignment 2 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#define MAXS 256
int main(int argc, char *argv[])
{
int num_pt;
int inputfile = 0, outputfile = 0, i;
for (i = 1; i < argc; i++)
{
if (strcmp (argv[i], "-i") == 0)
inputfile = i+1;
if (strcmp (argv[i], "-o") == 0)
outputfile = i+1;
}
if (inputfile == 0)
{
/* invalid command line options */
printf("\nIncorrect command-line...\n");
printf("> %s [-i inputfile [-o outputfile]]\n\n", argv[0]);
exit(0);
}
FILE *fp;
fp = fopen(argv[inputfile], "r");
int count = 0;
if (fp == 0)
{
printf("\nCould not find %s\n", argv[inputfile]);
exit(0);
}
char line[MAXS];
while (fgets(line, sizeof line, fp) != NULL)
{
if (count == 4)
{
fscanf(fp, "%d", &num_pt);
break;
}
else
count++;
}
int arr[num_pt][1];
while (fgets(line, sizeof line, fp) != NULL)
{
if (count == 5)
{
int k, j, cord;
for (k = 0; k < num_pt; k++)
{
for (j = 0; j < num_pt; j++)
{
while (fscanf(fp, "%d%d", &cord) > 0)
{
arr[k][j] = cord;
j++;
}
}
}
}
}
fclose(fp)
return 0;
}
After retrieving NUM_PT i tried reinitializing the count to 5 because the cordinates start from **LINE 6* in the file.
ERROR FROM COMPILER
Language: c99 ; Compiler: gcc
sample for "Storing numbers as (x, y) cordinates from a file" (It is better not to fix the reading position)
#include <stdio.h>
typedef struct point {
int x, y;
} Point;
int readPoint(FILE *fp, Point *p);
int readInt(FILE *fp, int *n);
int main(void){
FILE *fp = fopen("instance10_001.txt", "r");
Point p;
int MAX_X, MAX_Y;
readPoint(fp, &p);
MAX_X = p.x;
MAX_Y = p.y;
printf("MAX_X:%d, MAX_Y:%d\n", MAX_X, MAX_Y);
int NUM_PT;
readInt(fp, &NUM_PT);
printf("NUM_PT:%d\n", NUM_PT);
Point arr[NUM_PT];
for(int i = 0; i < NUM_PT; ++i){
readPoint(fp, &arr[i]);
printf("Point(%d, %d)\n", arr[i].x, arr[i].y);
}
fclose(fp);
}
int readLine(FILE *fp, char *buff, int buff_size){
while(fgets(buff, buff_size, fp)){
if(*buff == '#' || *buff == '\n')
continue;
return 1;
}
return 0;
}
#define LINE_MAX 128
int readPoint(FILE *fp, Point *p){
char buff[LINE_MAX];
if(readLine(fp, buff, sizeof buff)){
return 2 == sscanf(buff, "%d %d", &p->x, &p->y);
}
return 0;
}
int readInt(FILE *fp, int *n){
char buff[LINE_MAX];
if(readLine(fp, buff, sizeof buff)){
return 1 == sscanf(buff, "%d", n);
}
return 0;
}
This is the code I am working on and in the function trace* readTrace(char* fileName) I have to read a file (that fills structure) and then return the address of trace structure. Also the time and value of the structure are pointers but I don't know how to do it.
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <math.h>
#define TMAX 1000
#define NBPTS 2000
#define DT 0.5
typedef struct
{
char comment[40];
int nbpts;
float time[4096];
float value[4096];
} trace;
void simuTrace(int tmax, float dt, float params[], trace *uneTrace)
{
printf("Shkruani emrin e eksperimentit : \n");
scanf("%s", &uneTrace->comment);
int i = 0;
float v = 0, w = 0, dv = 0, dw = 0, t = 0;
float a = params[0], d = params[1], e = params[2];
while (t <= tmax)
{
dv = (a - v) * (v - 1) * v - w;
dw = e * (0.5 * v - w - d);
v += dv * dt;
w += dw * dt;
uneTrace->time[i] = t;
uneTrace->value[i] = v;
i++;
t += dt;
}
uneTrace->nbpts = i;
}
void printTrace(trace uneTrace)
{
printf("%s\n", uneTrace.comment);
printf("\t%d\n", uneTrace.nbpts);
int i;
for (i = 0; i <= NBPTS; i++) {
printf(" t= %.1f \tv= %.4f \n ", uneTrace.time[i],uneTrace.value[i]);
}
}
void saveTraceBin(char *fileTrace, trace uneTrace)
{
FILE *file;
file = fopen(fileTrace, "w");
if (fopen(fileTrace, "w") == NULL) {
printf("\n Gabim! \n");
} else {
fprintf(file, "%s\n", uneTrace.comment);
fprintf(file, "%d", uneTrace.nbpts);
int i;
for (i = 0; i <= NBPTS; i++) {
fprintf(file, "\n %.1f %.4f",uneTrace.time[i],uneTrace.value[i]);
}
fclose(file);
}
}
void readTrace(char *fileName, trace *uneTrace)
{
FILE*file;
file = fopen(fileName, "r");
if (file != NULL) {
fscanf(file, "%s", uneTrace->comment);
fscanf(file, "%d", &uneTrace->nbpts);
int i;
for (i = 0; i <= NBPTS; i++) {
fscanf(file, "%f", &(uneTrace->time[i]));
fscanf(file, "%f", &(uneTrace->value[i]));
}
printf("\n Leximi perfundoi me sukses!\n");
} else {
printf("\n Gabim! \n");
}
fclose(file);
}
trace* readTrace(char* fileName) {
FILE*file;
file = fopen(fileName, "r");
if (file != NULL) {
fscanf(file, "%s", uneTrace->comment);
fscanf(file, "%d", &uneTrace->nbpts);
int i;
for (i = 0; i <= NBPTS; i++) {
fscanf(file, "%f", &(uneTrace->time[i]));
fscanf(file, "%f", &(uneTrace->value[i]));
}
printf("\n Leximi perfundoi me sukses!\n");
} else {
printf("\n Gabim! \n");
}
fclose(file);
}
float errorTrace(trace uneTrace1, trace uneTrace2)
{
float sum = 0;
int i;
for (i = 0; i <= NBPTS; i++)
{
sum += (uneTrace1.value[i] - uneTrace2.value[i]);
}
sum /= NBPTS;
return sqrt(fabs(sum));
}
int main()
{
float Pa[] = { 0.5, 0.01, 0.05 };
float Pb[] = { 0.75, 0.3, 0.1 };
float e1, e2;
trace tracePa, tracePb, traceCell;
simuTrace(NBPTS, DT, Pa, &tracePa);
printTrace(tracePa);
saveTraceBin("myfile1.txt", tracePa);
simuTrace(NBPTS, DT, Pb, &tracePb);
printTrace(tracePb);
saveTraceBin("myfile2.txt", tracePb);
readTrace("cell.txt", &traceCell);
e1 = errorTrace(traceCell, tracePa);
e2 = errorTrace(traceCell, tracePb);
printf("\n Gabimi nga llogaritja e Pa : %f ", e1);
printf("\n Gabimi nga llogaritja e Pb : %f ", e2);
if (e1 < e2)
printf("\n\n Rasti Pa eshte me i mire se rasti Pb \n");
else
printf("\n\n Rasti Pb eshte me i mire se rasti Pa \n");
return 0;
}
You can either return a trace* that is allocated in readTrace,
trace* readTrace(char* fileName) {
trace *tp = malloc(sizeof *tp);
if (!tp) return NULL;
// fill up tp from file
....
}
// call this in main
trace *t = readTrace("cell.txt");
free(t); // free it when done
Or you could supply trace to the function, like
void readTrace(char* filename, trace *tp) {
if (!tp) return;
// fill up tp from file
....
}
// call this in main
trace t; // define trace object
readTrace("cell.txt", &t);
As for fill up time and value, read the array from the file:
int i;
fscanf(file, "%d", &t->nbpts);
for (i = 0; i < t->nbpts; i++) {
fscanf(file, "%f", &(t->time[i]));
fscanf(file, "%f", &(t->value[i]));
}
The function structure should look like this
trace* readTrace(char* fileName)
{
trace* traceptr;
int no_of_elements;
// Read the no of trace elements stored in file
// Generally this is avaliable in a location in the start of file,
// If not, then you have to guess and resize if it falls short.
traceptr = malloc(sizeof(trace) * no_of_elements);
// Read the trace elements from file
return (traceptr);
}
To call it in main()
int main(void)
{
trace *traceptr;
// Other stuff
traceptr = readTrace(filename);
simuTrace(NBPTS, DT, Pa, traceptr);
}
You need to modify your printTrace function to take a pointer to struct as input instead of an entire structure. Then it can take traceptr as it's input. Similarly for the saveTraceBin function.
I am trying to do duplicate elimination from clients.txt (which has 7 names and surnames, some of them are repeated). In the end of file it writes the output to output.dat file. I did not get any error during the compiling but when i try to run it, it gives "003.exe stopped working" error. (003.c is C project name)
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct names
{
char name[25];
char surname[25];
};
int main()
{
int i, j;
char a[1] = {""};
struct names name[200];
FILE *file;
FILE *file2;
file = fopen("clients.txt", "r");
if (ferror(file))
{
printf ("File could not be opened");
}
while (fscanf(file, "%s", a) == 2)
{
i = 0;
fscanf(file, "%s %s", name[i].name, name[i].surname);
i++;
}
for (i = 0; i < 200; i++)
{
for (j = 0; j < 200; j++)
{
if (i != j && strcmp(name[i].name, name[j].name) == 0 && strcmp(name[i].surname, name[j].surname) == 0 )
{
strcpy(name[j].name, a);
strcpy(name[j].surname, a);
}
}
}
fclose(file);
file2 = fopen("output.dat", "w");
{
for (i = 0; i < 200; i++)
{
if ( strcmp(name[i].name, "") == 1 )
{
fprintf(file, "%s %s\n", name[i].name, name[i].surname);
}
}
}
fclose(file2);
system("pause");
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct names {
char name[25];
char surname[25];
};
int isEqual(struct names *a, struct names *b){
return strcmp(a->name, b->name) == 0 && strcmp(a->surname, b->surname)==0;
}
int main(){
int i, j;
struct names name[200], a;
FILE *file;
file = fopen("clients.txt", "r");
if (!file){//ferror can't use to fopen
printf ("File could not be opened");
return -1;
}
i=0;
while (fscanf(file, "%24s %24s", a.name, a.surname) == 2){
int dup = 0;
for(j=0; j < i ;++j){
if(dup=isEqual(&a, &name[j]))
break;
}
if(!dup)//!dup && i<200
name[i++] = a;
}
fclose(file);
file = fopen("output.dat", "w");
for (j = 0; j < i; ++j){
fprintf(file, "%s %s\n", name[j].name, name[j].surname);
}
fclose(file);
system("pause");
return 0;
}