Question

In: Computer Science

C Programming Run Length Encoder (Compression). I am writing a program that takes an image (from...

C Programming Run Length Encoder (Compression). I am writing a program that takes an image (from a text file) and looks for patterns of 2 or more duplicate values.

The program should replace these values with the following pattern:  2 such characters followed by an Integer ( which represents number of occurrences of each character), followed by an asterisk.

For example say the input of the non-compressed image was: ,,,,)H 7. i.e. it consists of four commas, one closed bracket, the letter H, three spaces, the number seven and a full stop.

The latter would be compressed into another text file as: ,,4*)H 3*7.

Here is the code I have so far which opens a image text file and displays ASCII image to the console. I am stuck on how I go about compressing this image into another file.

Any help that can help me get a little closer to writing this program will be much appreciated. Any clarifications feel free to comment. Thanks in advanced.

#include
void print_image(FILE *fptr);

int main(void)
{
    char *filename = "image.txt";
    FILE *fptr = NULL;

    if((fptr = fopen(filename,"r")) == NULL)
    {
        fprintf(stderr,"error opening %s\n",filename);
        return 1;
    }

    print_image(fptr);

    fclose(fptr);

    return 0;
}

void print_image(FILE *fptr)
{
    char read_string[128];

    while(fgets(read_string,sizeof(read_string),fptr) != NULL)
{
        printf("%s",read_string);
}
}

Solutions

Expert Solution

/*
Method compress_file() accepts the file pointer for image.txt.
Steps followed in method
1. Create string array with contents.
2. Parse the array to find pattern.
3. Replace the pattern as per requirement.
4. Write updated string array in new file
*/

void compress_file(FILE *fptr);

void compress_file(FILE *fptr)
{
    fseek(fptr, 0, SEEK_END);    //move pointer to end of file
    int len = ftell(fptr);        //get total length of file
    char read_string[len];        //create string array of size len
    while(fgets(read_string), sizeof(read_string), fptr != NULL){}    //copy file contents in read_string
    int i=0, count=0;
    while (string[i] != '\0') {    //iterate array
        count = 1;    //to track pointer if there are no same characters
        if(string[i] ==string[i+1]){    //check if 2 or more characters are same
            count = 2;
            for(int j= i+1; string[j]!= '\0'; j++){    //check further in array if there are similar characters
                if(string[j]!=string[j+1]){
                    break;
                }
                count++;    //increament count if characters are same
            }
            string[i+count-1]=count + '0'; //convert int count to char and add in array
        }
        i = i + count; //increament i
    }
    
    FILE *targetfptr = fopen("compressed_file.txt","w");    //open file in write mode
    if (targetfptr != NULL) {
        for (i = 0; i < len; i++) {    ////iterate array
            fputc (string[i], targetfptr);    //add character in file
        }
        fclose(targetfptr);    //close file
   }
}


Related Solutions

I am writing a shell program in C++, to run this program I would run it...
I am writing a shell program in C++, to run this program I would run it in terminal like the following: ./a.out "command1" "command2" using the execv function how to execute command 1 and 2 if they are stored in argv[1] and argv[2] of the main function?
WITHOUT USING POINTERS. This is C programming. I am writing a program which should calculate standard...
WITHOUT USING POINTERS. This is C programming. I am writing a program which should calculate standard deviation of an array of exam scores and then add the standard deviation to each array element. It should print the original array, the standard deviation, and the new rounded adjusted array. I included my question as a comment in the line of code that is giving me an issue. (Removing the a from E-x-a-m as Chegg doesn't allow the word.) #include <stdio.h> #include...
I am Writing a C-Program to read and write files. but none of my code is...
I am Writing a C-Program to read and write files. but none of my code is working like it should be. Please fix all code and supply output response. Please try to use existing code and code in comments. But if needed change any code that needs to be changed. Thank you in advance //agelink.c //maintains list of agents //uses linked list #include <stdio.h> #include <stdlib.h> #define TRUE 1 void listall(void); void newname(void); void rfile(void); void wfile(void); struct personnel {...
Hi, (C programming) I am looking to write a program that will encrypt a text file...
Hi, (C programming) I am looking to write a program that will encrypt a text file automatically once program has opened, and have the option to decrypt it in a menu in my program. I have multiple text files that I need to encrypt, and would like if the program could encrypt all of them at once. I would also only like to decrypt the a text file once the name has been entered into a scanf function.
I want to create an image compression program with matlab use PCA. I have the code...
I want to create an image compression program with matlab use PCA. I have the code listed below. But this code is fail, the image is colorless, but still gray. Can you help me to fix my code. clc clear all picture = im2double(imread('picture1.jpg')); Red = picture(:,:,1); premean = mean(Red(:)); premax = max(Red(:)); premin = min(Red(:)); x = size(Red,1); y = size(Red,2); Z = ones(x,y)*premean; A = (Red - Z)*(1/premax - premin); B = cov(A); [veceig,eig,C] = pcacov(B); NewRed =...
Write a program in C that takes the length and the integers to be stored in...
Write a program in C that takes the length and the integers to be stored in an array and shifts array by N positions. Example: Input the number of elements to store in the array (max 10) : 5 Input 5 integers to be stored : Index - 0 : 12 Index - 1 : 29 Index - 2 : 68 Index - 3 : 32 Index - 4 : 97 Input number of shifts : 2 Expected Output :...
introduction: C PROGRAMMING For this assignment you will write an encoder and a decoder for a...
introduction: C PROGRAMMING For this assignment you will write an encoder and a decoder for a modified "book cipher." A book cipher uses a document or book as the cipher key, and the cipher itself uses numbers that reference the words within the text. For example, one of the Beale ciphers used an edition of The Declaration of Independence as the cipher key. The cipher you will write will use a pair of numbers corresponding to each letter in the...
In C programming, I am trying to search for the names of people that in this...
In C programming, I am trying to search for the names of people that in this DOISigned.txt file, however I am having trouble getting the first and last names of the multiple people named john, my current code only searches for John once and then it terminates,here is my current code #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> #include <stdlib.h> #define BUF_SIZE 0x3000 char buf[BUF_SIZE]; int main() {    char* inputFile = "DOISigners.txt";    FILE* fp;    fp = fopen(inputFile, "r");...
I am writing a jave program. I have seen the topic before but I want to...
I am writing a jave program. I have seen the topic before but I want to do it this way. And I got an error says: BonusAndDayOffRew.java:14: error: variable monthlySales might not have been initialized getSales(monthlySales); ^ Here is the description:A retail company assigns a $5000 store bonus if monthly sales are $100,000 or more. Additionally, if their sales exceed 125% or more of their monthly goal of $90,000, then all employees will receive a message stating that they will...
This is in Python I am getting an error when I run this program, also I...
This is in Python I am getting an error when I run this program, also I cannot get any output. Please help! #Input Section def main(): name=input("Please enter the customer's name:") age=int(input("Enter age of the customer: ")) number_of_traffic_violations=int(input("Enter the number of traffic violations: ")) if age <=15 and age >= 105: print('Invalid Entry') if number_of_traffic_violations <0: print('Invalid Entry') #Poccessing Section def Insurance(): if age < 25 and number_of_tickets >= 4 and riskCode == 1: insurancePrice = 480 elif age >=...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT