Reverse Grayscale Image Pixel by Pixel - c

I'm trying to write a C program that takes a .raw grayscale image and reverses it, pixel by pixel.
I store each pixel in a 1D char array, and then write each pixel to an output file, just in opposite order.
My program works if I try to reproduce the image, but if I try modifying the indices of my array, I produce an unreadable file.
Reproduce Original
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fin, *fout;
char ipath[64], opath[64], *rev, px;
int i, row, read, width, height;
printf("Name of input file: ");
scanf("%s", ipath);
printf("Name of output file: ");
scanf("%s", opath);
printf("Width of image: ");
scanf("%d", &width);
printf("Height of image: ");
scanf("%d", &height);
fin = fopen(ipath, "rb");
fout = fopen(opath, "wb");
rev = (char *)malloc(width * sizeof(char));
row = 0;
while(row < height)
{
for(i = 0; i < width; i++)
{
read = fread(&px, sizeof(char), 1, fin);
rev[i] = px;
}
for(i = 0; i < width; i++)
if(i < width && row < height)
fwrite(&rev[i], sizeof(char), 1, fout);
row++;
}
fclose(fout);
fclose(fin);
return 0;
}
Reverse Image
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fin, *fout;
char ipath[64], opath[64], *rev, px;
int i, row, read, width, height;
printf("Name of input file: ");
scanf("%s", ipath);
printf("Name of output file: ");
scanf("%s", opath);
printf("Width of image: ");
scanf("%d", &width);
printf("Height of image: ");
scanf("%d", &height);
fin = fopen(ipath, "rb");
fout = fopen(opath, "wb");
rev = (char *)malloc(width * sizeof(char));
row = 0;
while(row < height)
{
for(i = 0; i < width; i++)
{
read = fread(&px, sizeof(char), 1, fin);
rev[width - 1 - i] = px;
}
for(i = 0; i < width; i++)
if(i < width && row < height)
fwrite(&rev[i], sizeof(char), 1, fout);
row++;
}
fclose(fout);
fclose(fin);
return 0;
}
For testing purposes, I've just used scanf() though I'm aware of its issues.

Related

Saving integers from a file into an array

I was hoping to get a bit of help, I am implementing an inversion counter algorithm to take in 50,000 intergers and display the inversions and time it took to run the algorithm, I am having a hard time allocating and saving the integers from the file into an array. My code complies and runs but nothing happens
here is what I have:
int main(int argc, char** argv)
{
int n, i;
int inversions=0;
int *A;
FILE *file;
char filename[100];
clock_t start, end;
double totalTime;
printf("Enter filename: ");
scanf("%s", filename);
file = fopen(filename, "r");
if(file == NULL)
{
printf("Error opening file!\n");
return 0;
}
fscanf(file, "%d", &n);
A = (int*) malloc(n * sizeof(int));
for(i = 0; i < n; i++) {
fscanf(file, "%d", &A[i]);
}
start = clock();
inversions = countInversionsBruteForce(A, n);
end = clock();
totalTime = (double) (end - start) / CLOCKS_PER_SEC;
printf("Brute Force Algorithm\n");
printf("Number of inversions: %d\n", inversions);
printf("Execution time: %f\n", totalTime);
I think I have noth allocated array size and saved it properly
Your program is incomplete so I was not able to compile it. Minimized the problem to just loading the data into your array:
Formatted code for readability.
Generated a suitable input file. Most likely this is your problem but you have not shared your input sample with us.
Added missing include files.
Remove argc, argv as you not using them.
Minimize scope of variables. Use size_t instead of int for unsigned values.
Max string size on obtaining file name
Check return value for scanf(), fopen(), fscanf().
Printing out the data read to demonstrate it's working.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
printf("Enter filename: ");
char filename[100];
if(scanf("%99s", filename) != 1) {
printf("scanf failed\n");
return 1;
}
FILE *file = fopen(filename, "r");
if(!file) {
printf("Error opening file!\n");
return 1;
}
size_t n;
fscanf(file, "%zu", &n);
if(!n) {
printf("n must be positive");
return 1;
}
int *A = malloc(n * sizeof(*A));
for(size_t i = 0; i < n; i++)
if(fscanf(file, "%d", &A[i]) != 1) {
printf("fscanf() failed\n");
return 1;
}
printf("n = %zu\n", n);
printf("A = ");
for(size_t i = 0; i < n; i++)
printf("%d%s", A[i], i + 1 < n ? ", " : "\n");
}
with 1.txt as:
4
1
2
3
4
a sample session looks like this:
Enter filename: 1.txt
n = 4
A = 1, 2, 3, 4

Rotate bmp image in C

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?

C does not go to the next line while reading .ppm

So I have a .ppm file and the goal is to read each pixel to corresponding r[] g[] and b[] elements. The code reads the first line (idk correct or not), but it does not go any further. I'm unsure if I need these getc(fp); in order to skip spaces. Reading each line and parsing it to int is not an option. Thanks for any help.
int main(int argc, char** argv)
{
int height;
int width;
int max;
FILE *fp;
fp = fopen("vit_small.ppm", "r");
fscanf(fp, "%*[^\n]\n", NULL);
fscanf(fp, "%d %d", &width, &height);
printf("Width is %d height is %d \n", width, height);
fscanf(fp, "%d", &max);
printf("Maximum value %d \n", max);
int r [height][width];
int g [height][width];
int b [height][width];
int hist [5];
int w = 0;
int h = 0;
char buffer [1000];
for (;w<height;w++)
{
printf("Row number %d \n", w);
for (;h<width;h++)
{
fread(&r[w][h], 1, 1, fp);
printf("%d ", r[w][h]);
getc(fp);
fread(&g[w][h], 1, 1, fp);
printf("%d ", g[w][h]);
getc(fp);
fread(&b[w][h], 1, 1, fp);
printf("%d ", b[w][h]);
getc(fp);
}
getc(fp);
printf("\n");
}
int i = 0;
int j = 0;
for (;i<height; i++)
{
for (;j<width; j++)
{
printf("%d %d %d ", r[i][j], g[i][j], b[i][j]);
}
printf("\n");
}
fclose(fp);
FILE * res;
res = fopen ("Image_output.ppm", "w");
fprintf (res, "P6\n");
fprintf(res, "%d\n", width);
fprintf(res, "%d\n", height);
fprintf(res, "%d\n", max);
i = 0;
j = 0;
for(; i < height; i++)
{
for(; j < width; j++)
{
fprintf(res, "%d %d %d", r[i][j], g[i][j], b[i][j]);
}
fprintf(res,"\n");
}
return (EXIT_SUCCESS);
}
The P6 format of PPM stores each primary as a byte and there are no rows and no spaces. So if an image is 10 by 6, it will have 180 bytes (10x6x3) following the 255 and newline character. See
Wikipedia entry on PPM.

Reproduce RAW Image - C

I'm trying to reproduce a .raw image file using a small program I've written in C, but every time I run the program my output file becomes unreadable.
All my program does is take every pixel and write it to a corresponding output file.
Also note, this program is written to support only grayscale images.
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fin, *fout;
char path_in[64], path_out[64], *rev, px;
int width, height, read, row, i;
printf("Input file name: ");
scanf("%s", path_in);
printf("Output file name: ");
scanf("%s", path_out);
printf("Width of image (in pixels): ");
scanf("%d", &width);
printf("Height of image (in pixels): ");
scanf("%d", &height);
fin = fopen(path_in, "r");
fout = fopen(path_out, "w");
row = 0;
rev = (char *)malloc(width * sizeof(char));
while(row < height)
{
for(i = 0; i < width; i++)
{
read = fread(&px, sizeof(char), 1, fin);
rev[i] = (unsigned int)px;
}
for(i = 0; i < width; i++)
if(i < width && row < height)
fwrite(&rev[i], sizeof(char), 1, fout);
row++;
}
fclose(fout);
fclose(fin);
return 0;
}
There might be some other problems with it, but you've told fopen to open the files in text mode:
fin = fopen(path_in, "r");
fout = fopen(path_out, "w");
instead of binary mode:
fin = fopen(path_in, "rb");
fout = fopen(path_out, "wb");
which is what's appropriate for image data.
You're also doing odd casting that might be munging your data:
char ..., *rev, px;
...
rev = (char *)malloc(width * sizeof(char));
...
rev[i] = (unsigned int)px;
The (unsigned int) cast is unnecessary at best.

How to read two strings from file and store them in two separate arrays

Looking forward to help...I need to read two strings from text files and store them into two separate arrays. I've searched, and got many codes, one of which worked, so I tried to modify it to read two strings. Here is my code:
int main(){
int i = 0;
int BUFSIZE = 1000;
char* words[20];
char* words2[20];
FILE *fp = fopen("input1.txt", "r");
FILE *fp2 = fopen("input2.txt", "r");
if (fp == 0){
fprintf(stderr, "Error while opening");
return 0;
}
words[i] = (char*)malloc(BUFSIZE);
words2[i] = (char*)malloc(BUFSIZE);
while (fgets(words[i], BUFSIZE, fp)) {
i++;
words[i] = (char*)malloc(BUFSIZE);
}
while (fgets(words2[i], BUFSIZE, fp2)) {
i++;
words2[i] = (char*)malloc(BUFSIZE);
}
printf("Output: \n");
srand(time(NULL));
int j = rand()%i;
int k = (j+1)%i;
fflush(stdout);
printf("%d - %s %d -%s", j, words[j], k, words[k]);
int x;
for(x = 0; x<i; x++)
free(words[x]);
free(words2[x]);
scanf("%d", x);
fclose(fp);
fclose(fp2);
return 0;
}
But it won't work.Anyone knows why? Thank you!
Reset i = 0 before second loop and enbrace your free(x) code in curly braces
int main()
{
int i = 0;
int BUFSIZE = 1000;
char* words[20];
char* words2[20];
FILE *fp = fopen("input1.txt", "r");
FILE *fp2 = fopen("input2.txt", "r");
if (fp == 0){
fprintf(stderr, "Error while opening");
return 0;
}
words[i] = (char*)malloc(BUFSIZE);
words2[i] = (char*)malloc(BUFSIZE);
while (fgets(words[i], BUFSIZE, fp)) {
i++;
words[i] = (char*)malloc(BUFSIZE);
}
// reset i back to zero
i = 0;
while (fgets(words2[i], BUFSIZE, fp2)) {
i++;
words2[i] = (char*)malloc(BUFSIZE);
}
printf("Output: \n");
srand(time(NULL));
int j = rand()%i;
int k = (j+1)%i;
fflush(stdout);
printf("%d - %s %d -%s", j, words[j], k, words[k]);
int x;
for(x = 0; x<i; x++){
free(words[x]);
free(words2[x]);
}
scanf("%d", x);
fclose(fp);
fclose(fp2);
return 0;
}
More information is needed as to what is happening. Do you get a compilation error, runtime message? Did you check that the files exist?
Also, a few things should be fixed:
Your first line should include stdio.h
Add braces around the body of your for() to envelop both calls to free.
Does your compiler accept your declaration of x in the middle of the function like that? If not, move it to the top.

Resources