Finally got it to work! Instead of adding all the values to the imagecpy array, I added them into an independent set of floats for each value. Posting the edited correct code below!
//Iterate through all pixels
for (int i = 0; i < height; i++)
for (int j = 0; j < width; j++)
float avgred = 0.0;
float avggreen = 0.0;
There are a few problems to fix. First, there's this:
string filename = NULL;
This creates a string of length 0. When a string is created, its size (i.e., the physical memory allocated to it) cannot be changed. This initialization doesn't allocate any memory for it. Instead, the code should set the size for the filename. We already know how many chars ...
Kudos for making a good attempt, but you made a serious wrong turn. This logic is seriously flawed. The code effectively has two 512 byte buffers. It reads from the input file into the x buffer and then checks the bytes buffer for a signature. This is like looking in your front yard for a hammer that you lost in the basement, because the light is better. ...
debug50 may be a better tool at this point, so you can find where program is crashing.
Assuming the first block is not a jpg signature, where does program control go when this if (buffer == 0xff && buffer == 0xd8 && buffer == 0xff && (buffer & 0xf0) == 0xe0) is false?
If you look at the extended check50 results in the link at the end of the message, there are more clues to be found.
For the greyscale issue, the last pixel in the 3x3 image is off by 1, to the high direction. The problem lies here:
int average = (int) ceilf(avg);
The code should be using a rounding function, not a ceiling function. The ceiling function ...
"Ready, fire, aim!"
The problem lies here:
jpg = fopen(filename, "w");
sprintf(filename, "%03i.jpg", numfound);
The sprintf command builds the name of the file to be opened by the fopen call. It needs to be before the fopen call.
It also explains the files with wierd file names. To delete them, you'll need to ...
There are a few issues with the code, two of which are significant.
First and most important, the two final fclose statements are inside the while loop. They should be after the loop closes.
Next, the output file name should end in jpg, not jpeg. That extra letter will cause data corruption when the filename overwrites the first byte of the next item in ...
Are you sure that you are remaking the file after editing it, and that you're rebuilding the file that you're actually editing? Or that you're running the 2020 check50 slug?
I tested the code in check50 and it's fine.
I'm guessing that memory is getting stepped on. First, was memory allocated for picture_name? How much memory?
Next, the fread is being executed, the data processed, and only then is a check for EOF done. In other words, "ready, shoot, aim."
After reviewing the full code elsewhere, the problem lies in the declaration:
Thanks to @Cliff B for optimization answers, but the if else statement question was not quite what I was looking for.
After watching the CS50 walkthrough on Recover again, I typed out a few more notes which now makes more sense.
//find start of a new jpeg file
//if we find a file that has the first four bytes in the buffer that is similar to a ...
It serves two purposes in this code.
First, the IF part of the "else if" will make sure that no write is attempted before the first file has been opened. Without it, you'd get a seg fault for trying to write to a file that hasn't been opened. This is absolutely necessary.
The second purpose relates to the ELSE. Since the code writes the signature ...
Simple. The program is terminating early because of this code:
// Open file
FILE *card = fopen(argv, "r");
Perhaps there's a missing if statement?
Did you actually test your code or just run check50?
If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)
The code is generating a seg fault. Look at the following code:
if (key == 0)
If this code is supposed to detect that a file has already been opened, shouldn't it be key > 0 ?
Weren't you seeing the seg fault when you ran the code?
If this answers your question, please click on the check mark to accept. ...
I think the issue is with this line of code:
while (fread(block, sizeof(block), 1, input) == 1)
fread returns the number of items successfully read. So, if it successfully reads 1 block of data, it would return 512 and not 1. Since the condition isn't true, the while loop gets skipped and the program exits.
Edit: you are also returning 0 upon reading the ...
The problem lies here. It will cause a block(s) of data to be deleted from an image.
// skip this iteration if the block (likely) contains all 0
if (chunk == 0 && chunk[BLOCK - 1] == 0)
What is the purpose of this code? The comment implies that it is supposed to check for a block that's all zeros and not ...
The problem lies here:
//keep writing to the file
fwrite(buffer, sizeof(BYTE), 1, jpg);
Question: What happens when the first 512 byte data block from the input file is processed, but doesn't have a signature? What happens when this code tries to write to a null file pointer? Oh, wait. You already know. ;-)
If this ...
There are three groups of problems in this code.
First, it looks like a simple oversight error on the test conditions. In each test for x==width or y==height, shouldn't those be width - 1 or height - 1? Remember, an array of size x runs from 0 to x-1.
Second, each formula uses all integer vars and then is divided by the number of pixels, either 4, 6 or 9. ...
Because of the way the code is structured, the signature data blocks are being written to the output file twice - first in the code block that opens the file, and then again in the latter if (JPEGfound == 1) code block.
Also, note that the first file should be 000.jpg, not 001.jpg.
If this answers your question, please click on the check mark to accept. Let'...
From one blind old fart to another, it's like the guy that lost his glasses in the basement but was looking in the back yard because the light was better. You're looking in the wrong place. ;-)
The problem is subtle. (I did the same thing the first time I did this, so don't feel bad.) The code is calculating the blur values (correctly, I assume), but then ...
This is happening because your loop is summing values from neighbors to the left and up of the current pixel that have already been blurred by previous iterations of the same loop. You need to check the values from one of your two copies of the image, and write the blurred values to another copy. Right now, you're writing to both copies at the end of the ...
Just get rid of int x=0; and replace x+i-1 with int x = i - 1. Likewise for the variable y. You can put any expression or declaration in the first section of the for loop.
If this answers your question, please click on the check mark to accept. Don't be shy to ask another question the next time you have an issue!
while(fread(buffer, sizeof(unsigned char), 512, file) == 1)
fread(buffer, sizeof(unsigned char), 512, file);
The code is executing two consecutive freads without processing the data from the first read. That means that all the data from the first read is being thrown away without being processed.
It's a common misconception that an fread in a while or ...