PSET 4 solutions for cs50 class

## whod unit

This program takes an input file, and changes the RGB values inside the file

It revolves around understanding that pixels are 3 bytes, stored in values, and interpreting how those values are shown on a computer.

All you had to understand was that inbetween the “fread” and “fwrite” mode, you could change the hexadecimal color values through its typedef struct

Basically white color is 0xFFFFFF, and red is 0x0000FF (the first four numbers after 0x are blue and green respectively). You change values to match that.

Imagine going to your monitor and adjusting blue,green, white values during color calibation. Putting all BGR values to 100 sets the color of screen to white. Actually I’m pretty sure if you took red,green, and blue paint and mixed them together in equal quantities it makes white.

The only things I added was line 15-20

## Resize.C + bmp.h

This program takes an image, takes in a factor, and scales the image to the new factor size. So if the factor was 3, this is what would have to happen:

1. The image width would need to be 3xs longer, in the appropiate locations.
2. The first 3 rows in newest images need to be same thing

Its basically like taking an image and scaling it upwards

First the predefined header of the bmp file:

Week 4 notes introduced typedefs and structs. Typedefs are just abbreviations to make it easier to type out variables. Structs are basically “super variables” or variables that house more variable types.

attribute_((__packged__)) basically just means ignore multiple of 4 padding for GCC compilation, see https://stackoverflow.com/questions/11770451/what-is-the-meaning-of-attribute-packed-aligned4

The C program below is as follows:

Most of the top portion of this file remains unchanged. I changed the number of arguments to 4, to account for the scaling factor on the image. Everything else here checks whether the files are reading correctly and a bitmap is actually hte input file by its number of bytes in header.

This part took me the longer to understand and comprehend. Essentially what happens is the following:

• A new struct bf and bi, 54 bytes total, named new_bf and new_bi to prevent confusion of the existing var
• Default values of original image are used (e.g. same offset values, planecolor values, etc) to initialize information
• Set new width and height
• Padding is used for fseek on the input, and new_padding for the fwrite on output
• BiSizeImage is the size of the pixel area, bfSize is that + the 54 bytes in the header

An important concept that I learned from this is that there’s always 54 bytes in a .bmp file. Each value is offsetted by 2 or 4 bytes depending if its a DWORD, etc.

Next after initializing everything, is scanning the input to create the output. I took the original file and essentially added a few things, namely:

• FORLOOP l=0 mostly so if I had a factor of 3 on the image, I can repeat the same input row 3xs for output
• FORLOOP r=0, this creates the width output. RGB values represent one pixel, and are repeated for a factor of 3 as : BGR+BGR+BGR
• for (int k=0; k<newpadding;K++) this part is for the fputc for writing output, as we need multiple of 4
• if l<factor-1 fseek line. Basically, there’s one technically only one scanline and as new inputs are being read, the SEEK_CUR is adjusting as new reads are happening. Because I’m iterating over the same input row multiple times, I need to re-read some row factor of times, hence this -offset. It needs to be set as a “long” function because by default its an unsigned (positive integer only) when I need negative pushback on fseek.

## Recover.C

How it works

• sprintf("%03d.jpg", fnumber) gives file names as 001, 002, 003, etc as the 0 indicates to put leading 0’s
• The first IF statements keep going until the first 512 bytes for a JPEG are found
• Then only the next set of top level IF statements are used because (searchjpeg=0) now
• An array of 512 bytes called buffer is constantly read and rewritten over
• while(fread(&buffer, 512, 1, inptr)==1){ acts as both a logical statement to end the loop when no more data can be read, and physically reading the bytes into the buffer. 2 birds with 1 stone

History revisions

https://github.com/AnacondaPython/cs50solutions/commit/7139daff9d10f1d9146279e9cbe1ea9f8886cb34?diff=unified original code I hashed up had too many bugs. I tried using fseek -512 to move the cursor back and forth and using heap stack memory through malloc, but I could’ve just initialized an array on the stack according to https://www.quora.com/Where-are-the-arrays-in-C-stored-in-a-stack-or-a-heap

errors

https://cs50.stackexchange.com/questions/21776/pset4-works-correctly-but-check-shows-error/21777#21777

Everything performs as expected producing the 50 images perfectly on first iteration. But there’s some memory errors. Not really sure exactly where they are yet. Also the cs50 check isn’t producing perfect outputs but program still runs

0. How many different colors does each format support

https://www.quora.com/How-many-different-colors-does-each-format-like-JPEG-BMP-GIF-and-PNG-suppor. It depends on number of bits the file allows on color. 8 bit is 256 colors

1. Which of the formats supports animation GIF, GIFV, WEBM

2. What’s the difference between lossy and lossless compression tech quickie summarizes it well. https://www.youtube.com/watch?v=guo8if4Yxhw .Lossless compression takes the binary code and condenses down the information some simple ciphers. A good visual answer here: http://i.imgur.com/ILqyO4j.png. Things that use lossless compression would be zip files, daemon tools, etc

Lossy compression is used for videos, results in chunks of data being removed. Youtube probably has the most advanced algorithm for this. Lossy compression can sometimes result in artifacts in videos.

http://lockergnome.com/2011/08/22/lossy-vs-lossless-video-compression/

3. Which of these formats is lossy-compressed?

http://i.imgur.com/OoNdUgR.png. JPEG and GIFs are lossy. BMP and PNG are lossless MP3 and MP4 are also lossy

4. What happens, technically speaking, when a file is deleted on a FAT file system?

https://youtu.be/T87iyGiXb7o. Techquickie. The 010101010 within the system are unlinked to the mastertable, and given permissions to be rewritten over. If they’ve been overwritten then you lose it.

5. What can someone like you do to ensure (with high probability) that files you delete cannot be recovered?

Delete all the data. Rewrite (fill with more random data) several times over again on the same file so all the original bit and bytes are overrwriten. This option is usually done during reformatting of a hard drive

Physically destroying drive

6. What’s stdint.h?

Specifies aliases for more primitive data types. There typedefs

7. What’s the point of using uint8_t, uint32_t, int32_t, and uint16_t in a program?

The number refers to the number of bits it can allocate. You can TYPEDEF them into more common terms like byte, long, word, etc

U stands for unsigned integer type. All 8 bits for that 1 byte are set to positive #s ranging from 0 to 255, this is for uint8_t. uint32_t

Signed values range from -128 to 127

Unsigned values from 0 o 255

http://stackoverflow.com/questions/247873/signed-versus-unsigned-integers

Unsigned bits can store larger values.

In this case integers

8. How many bytes is a BYTE, a DWORD, a LONG, and a WORD, respectively?

Byte is 8 bits, or 1 byte

DWORD is 32 bits ,or 4 bytes

LONG is 32 bits, or 4 bytes

WORD is 16 bits or 2 bytes

9. What (in ASCII, decimal, or hexadecimal) must the first two bytes of any BMP file be? (Leading bytes used to identify file formats (with high probability) are generally called “magic numbers.)”

Hex: 0x424d, 42 and 4d are the first two bytes. ASCII: BM for bitmap. reasoning 42 (hex) = 4(161)+ 2(160) = 66 in ASCII value or “B” Decimal would be 0

10. What’s the difference between bfSize and biSize? https://msdn.microsoft.com/en-us/library/windows/desktop/dd183374(v=vs.85).aspx

http://i.imgur.com/4HHtnWQ.png bfsize is the total number of bytes in the file header, 14 bytes. bisize is the number of bytes in the info header, 40 bytes

something like this, wikipedia’s documentation vs. CS50’s shorthand naming conventions are confusing:

A better conversion:

Also, looking into the file GDB describes what happens better

11. What does it mean if biHeight is negative?

(wikipedia and CS50 has been crappy as a resource for these problems, MSDN.microsoft.com is a better authority wiki)

https://msdn.microsoft.com/en-us/library/windows/desktop/dd318229(v=vs.85).aspx

Biheight being negative flips the DIB (also called bitmapinfo header) so its oriented towards top left instead of bottom left. Basically, I believe a flipped image across x-axis when representing the bitmap.

12. What field in BITMAPINFOHEADER specifies the BMP’s color depth (i.e., bits per pixel)?

Bitcount specifies the number of bits per pixel, and therefore max number of colors per pixel. Normally, 24 -BMP, 1 pixel has 3 bytes = 24 bits. Each byte represents a RGB values from 0 to 255 since its 1 byte each.

e.g. Red color = 1 byte Green color = 1 byte Blue = 1 byte

More bytes allocated means more colordepth, instead of 255 color choices, there could be more per RGB

13. Why might fopen return NULL in copy.c:37?

fopen might return null if it can’t find the file

14. Why is the third argument to fread always 1 in our code?

We’re specifiying number of elements read, there’s only 1 struct

If we had been reading two files, it’d be 2 (e.g. two bitmap files)

15. What value does copy.c:70 assign padding if bi.biWidth is 3?

Scanline needs to be a multiple of 4. Why it needs to be this way it isn’t explained. Bi.biwidth =3. Had to look it up

http://cboard.cprogramming.com/game-programming/6102-bitmap-4-byte-boundry.html

Basically, it has to do with the CPU and intels standarization to 4 bytes, and a byte being 4 bits.

16. What does fseek do?

Skips over any padding and then looks for the next pixel represented by RGB triple. File position indicator

17. What is SEEK_CUR?

This is the current position of the file pointer, to skip over the padding

18. whod’ unit

Pretty sure its rick astley rick rolling

• None

Vincent Tang