pset 4: forensicscdn.cs50.net/2012/fall/psets/4/walkthrough4.pdf · reading files fread(&data,...
TRANSCRIPT
![Page 2: pset 4: Forensicscdn.cs50.net/2012/fall/psets/4/walkthrough4.pdf · Reading Files fread(&data, size, number, inptr); &data: pointer to a struct which will contain the bytes you’re](https://reader034.vdocument.in/reader034/viewer/2022050410/5f86c8fa2d0f717b396444dd/html5/thumbnails/2.jpg)
Toolbox
update50 File I/O copy.c bitmaps
padding! JPEGs
![Page 3: pset 4: Forensicscdn.cs50.net/2012/fall/psets/4/walkthrough4.pdf · Reading Files fread(&data, size, number, inptr); &data: pointer to a struct which will contain the bytes you’re](https://reader034.vdocument.in/reader034/viewer/2022050410/5f86c8fa2d0f717b396444dd/html5/thumbnails/3.jpg)
0. A Section of Questions 1. Whodunit 2. Resize 3. Recover
pset 4
![Page 4: pset 4: Forensicscdn.cs50.net/2012/fall/psets/4/walkthrough4.pdf · Reading Files fread(&data, size, number, inptr); &data: pointer to a struct which will contain the bytes you’re](https://reader034.vdocument.in/reader034/viewer/2022050410/5f86c8fa2d0f717b396444dd/html5/thumbnails/4.jpg)
File I/O Toolbox
fopen fread fwrite fputc fseek sprintf fclose feof
![Page 5: pset 4: Forensicscdn.cs50.net/2012/fall/psets/4/walkthrough4.pdf · Reading Files fread(&data, size, number, inptr); &data: pointer to a struct which will contain the bytes you’re](https://reader034.vdocument.in/reader034/viewer/2022050410/5f86c8fa2d0f717b396444dd/html5/thumbnails/5.jpg)
Opening Files
FILE* inptr = fopen("foo.bmp", "r"); Opens foo.bmp for reading
FILE* outptr = fopen("bar.bmp", "w"); Opens bar.bmp for writing
![Page 6: pset 4: Forensicscdn.cs50.net/2012/fall/psets/4/walkthrough4.pdf · Reading Files fread(&data, size, number, inptr); &data: pointer to a struct which will contain the bytes you’re](https://reader034.vdocument.in/reader034/viewer/2022050410/5f86c8fa2d0f717b396444dd/html5/thumbnails/6.jpg)
Reading Files
fread(&data, size, number, inptr);
&data: pointer to a struct which will contain the bytes you’re reading
size: size of each element to read sizeof
number: number of elements to read inptr: FILE* to read from
![Page 7: pset 4: Forensicscdn.cs50.net/2012/fall/psets/4/walkthrough4.pdf · Reading Files fread(&data, size, number, inptr); &data: pointer to a struct which will contain the bytes you’re](https://reader034.vdocument.in/reader034/viewer/2022050410/5f86c8fa2d0f717b396444dd/html5/thumbnails/7.jpg)
Reading Files
fread(&data, sizeof(DOG), 2, inptr);
vs.
fread(&data, 2 * sizeof(DOG), 1, inptr);
![Page 8: pset 4: Forensicscdn.cs50.net/2012/fall/psets/4/walkthrough4.pdf · Reading Files fread(&data, size, number, inptr); &data: pointer to a struct which will contain the bytes you’re](https://reader034.vdocument.in/reader034/viewer/2022050410/5f86c8fa2d0f717b396444dd/html5/thumbnails/8.jpg)
Writing Files
fwrite(&data, size, number, outptr);
&data: pointer to the struct that contains the bytes you’re reading from
size number outptr: FILE* to write to
![Page 9: pset 4: Forensicscdn.cs50.net/2012/fall/psets/4/walkthrough4.pdf · Reading Files fread(&data, size, number, inptr); &data: pointer to a struct which will contain the bytes you’re](https://reader034.vdocument.in/reader034/viewer/2022050410/5f86c8fa2d0f717b396444dd/html5/thumbnails/9.jpg)
Writing Files
fputc(chr, outptr);
chr: char to write outptr: FILE* to write to
![Page 10: pset 4: Forensicscdn.cs50.net/2012/fall/psets/4/walkthrough4.pdf · Reading Files fread(&data, size, number, inptr); &data: pointer to a struct which will contain the bytes you’re](https://reader034.vdocument.in/reader034/viewer/2022050410/5f86c8fa2d0f717b396444dd/html5/thumbnails/10.jpg)
File Position Indicator
fseek(inptr, amount, from);
inptr: FILE* to seek in amount: number of bytes to move cursor from:
SEEK_CUR (current position in file) SEEK_SET (beginning of file)
SEEK_END (end of file)
![Page 11: pset 4: Forensicscdn.cs50.net/2012/fall/psets/4/walkthrough4.pdf · Reading Files fread(&data, size, number, inptr); &data: pointer to a struct which will contain the bytes you’re](https://reader034.vdocument.in/reader034/viewer/2022050410/5f86c8fa2d0f717b396444dd/html5/thumbnails/11.jpg)
./whodunit clue.bmp verdict.bmp
Whodunit
![Page 12: pset 4: Forensicscdn.cs50.net/2012/fall/psets/4/walkthrough4.pdf · Reading Files fread(&data, size, number, inptr); &data: pointer to a struct which will contain the bytes you’re](https://reader034.vdocument.in/reader034/viewer/2022050410/5f86c8fa2d0f717b396444dd/html5/thumbnails/12.jpg)
TODO
Understand the structure of bitmaps Metadata Pixel colors Padding
![Page 13: pset 4: Forensicscdn.cs50.net/2012/fall/psets/4/walkthrough4.pdf · Reading Files fread(&data, size, number, inptr); &data: pointer to a struct which will contain the bytes you’re](https://reader034.vdocument.in/reader034/viewer/2022050410/5f86c8fa2d0f717b396444dd/html5/thumbnails/13.jpg)
Bitmaps
An arrangement of bytes
bmp.h
![Page 14: pset 4: Forensicscdn.cs50.net/2012/fall/psets/4/walkthrough4.pdf · Reading Files fread(&data, size, number, inptr); &data: pointer to a struct which will contain the bytes you’re](https://reader034.vdocument.in/reader034/viewer/2022050410/5f86c8fa2d0f717b396444dd/html5/thumbnails/14.jpg)
Header
biSizeImage total size of image (in bytes)
includes pixels and padding
biWidth width of image (in pixels)
does not include padding
biHeight height of image (in pixels)
structs BITMAPFILEHEADER, BITMAPINFOHEADER
![Page 15: pset 4: Forensicscdn.cs50.net/2012/fall/psets/4/walkthrough4.pdf · Reading Files fread(&data, size, number, inptr); &data: pointer to a struct which will contain the bytes you’re](https://reader034.vdocument.in/reader034/viewer/2022050410/5f86c8fa2d0f717b396444dd/html5/thumbnails/15.jpg)
Pixel color
Each color is represented by 3 bytes: amount of blue amount of green amount of red
ff0000 blue ffffff white
![Page 16: pset 4: Forensicscdn.cs50.net/2012/fall/psets/4/walkthrough4.pdf · Reading Files fread(&data, size, number, inptr); &data: pointer to a struct which will contain the bytes you’re](https://reader034.vdocument.in/reader034/viewer/2022050410/5f86c8fa2d0f717b396444dd/html5/thumbnails/16.jpg)
smiley.bmp
ffffff ffffff 0000ff 0000ff 0000ff 0000ff ffffff ffffff
ffffff 0000ff ffffff ffffff ffffff ffffff 0000ff ffffff
0000ff ffffff 0000ff ffffff ffffff 0000ff ffffff 0000ff
0000ff ffffff ffffff ffffff ffffff ffffff ffffff 0000ff
0000ff ffffff 0000ff ffffff ffffff 0000ff ffffff 0000ff
0000ff ffffff ffffff 0000ff 0000ff ffffff ffffff 0000ff
ffffff 0000ff ffffff ffffff ffffff ffffff 0000ff ffffff
ffffff ffffff 0000ff 0000ff 0000ff 0000ff ffffff ffffff
![Page 17: pset 4: Forensicscdn.cs50.net/2012/fall/psets/4/walkthrough4.pdf · Reading Files fread(&data, size, number, inptr); &data: pointer to a struct which will contain the bytes you’re](https://reader034.vdocument.in/reader034/viewer/2022050410/5f86c8fa2d0f717b396444dd/html5/thumbnails/17.jpg)
Padding
Each pixel is 3 bytes Size of each scanline must be a
multiple of 4 bytes If the number of pixels isn’t a multiple
of 4, we need “padding” Padding is just zeros (0x00)
![Page 18: pset 4: Forensicscdn.cs50.net/2012/fall/psets/4/walkthrough4.pdf · Reading Files fread(&data, size, number, inptr); &data: pointer to a struct which will contain the bytes you’re](https://reader034.vdocument.in/reader034/viewer/2022050410/5f86c8fa2d0f717b396444dd/html5/thumbnails/18.jpg)
xxd –c 12 -‐g 3 -‐s 54 small.bmp
0000036: 00ff00 00ff00 00ff00 000000 ......
0000042: 00ff00 ffffff 00ff00 000000 ......
000004e: 00ff00 00ff00 00ff00 000000 ......
![Page 19: pset 4: Forensicscdn.cs50.net/2012/fall/psets/4/walkthrough4.pdf · Reading Files fread(&data, size, number, inptr); &data: pointer to a struct which will contain the bytes you’re](https://reader034.vdocument.in/reader034/viewer/2022050410/5f86c8fa2d0f717b396444dd/html5/thumbnails/19.jpg)
xxd –c 36 -‐g 3 -‐s 54 large.bmp
0000036: 00ff00 00ff00 00ff00 00ff00 00ff00 00ff00 00ff00 00ff00 00ff00 00ff00 00ff00 00ff00 ......
000005a: 00ff00 00ff00 00ff00 00ff00 00ff00 00ff00 00ff00 00ff00 00ff00 00ff00 00ff00 00ff00 ......
000007e: 00ff00 00ff00 00ff00 00ff00 00ff00 00ff00 00ff00 00ff00 00ff00 00ff00 00ff00 00ff00 ......
00000a2: 00ff00 00ff00 00ff00 00ff00 00ff00 00ff00 00ff00 00ff00 00ff00 00ff00 00ff00 00ff00 ......
00000c6: 00ff00 00ff00 00ff00 00ff00 ffffff ffffff ffffff ffffff 00ff00 00ff00 00ff00 00ff00 ......
00000ea: 00ff00 00ff00 00ff00 00ff00 ffffff ffffff ffffff ffffff 00ff00 00ff00 00ff00 00ff00 ......
000010e: 00ff00 00ff00 00ff00 00ff00 ffffff ffffff ffffff ffffff 00ff00 00ff00 00ff00 00ff00 ......
0000132: 00ff00 00ff00 00ff00 00ff00 ffffff ffffff ffffff ffffff 00ff00 00ff00 00ff00 00ff00 ......
0000156: 00ff00 00ff00 00ff00 00ff00 00ff00 00ff00 00ff00 00ff00 00ff00 00ff00 00ff00 00ff00 ......
000017a: 00ff00 00ff00 00ff00 00ff00 00ff00 00ff00 00ff00 00ff00 00ff00 00ff00 00ff00 00ff00 ......
000019e: 00ff00 00ff00 00ff00 00ff00 00ff00 00ff00 00ff00 00ff00 00ff00 00ff00 00ff00 00ff00 ......
00001c2: 00ff00 00ff00 00ff00 00ff00 00ff00 00ff00 00ff00 00ff00 00ff00 00ff00 00ff00 00ff00 ......
![Page 20: pset 4: Forensicscdn.cs50.net/2012/fall/psets/4/walkthrough4.pdf · Reading Files fread(&data, size, number, inptr); &data: pointer to a struct which will contain the bytes you’re](https://reader034.vdocument.in/reader034/viewer/2022050410/5f86c8fa2d0f717b396444dd/html5/thumbnails/20.jpg)
RGBTRIPLE
struct to represent pixels
RBGTRIPLE triple; triple.rgbtBlue = 0x00; triple.rgbtGreen = 0xff; triple.rgbtRed = 0x00;
What color is this?
![Page 21: pset 4: Forensicscdn.cs50.net/2012/fall/psets/4/walkthrough4.pdf · Reading Files fread(&data, size, number, inptr); &data: pointer to a struct which will contain the bytes you’re](https://reader034.vdocument.in/reader034/viewer/2022050410/5f86c8fa2d0f717b396444dd/html5/thumbnails/21.jpg)
TODO
Understand the structure of bitmaps Make the bmp readable!
Open clue.bmp file Read each scanline, pixel by pixel Change pixels as needed Write the scanline into verdict.bmp, pixel
by pixel
![Page 22: pset 4: Forensicscdn.cs50.net/2012/fall/psets/4/walkthrough4.pdf · Reading Files fread(&data, size, number, inptr); &data: pointer to a struct which will contain the bytes you’re](https://reader034.vdocument.in/reader034/viewer/2022050410/5f86c8fa2d0f717b396444dd/html5/thumbnails/22.jpg)
copy.c
Opens a file Read each scanline, pixel by pixel Copies each pixel into the output file’s
scanline
cp copy.c whodunit.c
![Page 23: pset 4: Forensicscdn.cs50.net/2012/fall/psets/4/walkthrough4.pdf · Reading Files fread(&data, size, number, inptr); &data: pointer to a struct which will contain the bytes you’re](https://reader034.vdocument.in/reader034/viewer/2022050410/5f86c8fa2d0f717b396444dd/html5/thumbnails/23.jpg)
TODO
Understand the structure of bitmaps Make the bmp readable
Open clue.bmp file Read each scanline, pixel by pixel Change pixels as needed Write the scanline into verdict.bmp, pixel
by pixel
![Page 24: pset 4: Forensicscdn.cs50.net/2012/fall/psets/4/walkthrough4.pdf · Reading Files fread(&data, size, number, inptr); &data: pointer to a struct which will contain the bytes you’re](https://reader034.vdocument.in/reader034/viewer/2022050410/5f86c8fa2d0f717b396444dd/html5/thumbnails/24.jpg)
Changing pixel color
For a given pixel triple, you can access: triple.rgbtBlue triple.rgbtGreen triple.rgbtRed
Hmm, that’s handy!
![Page 25: pset 4: Forensicscdn.cs50.net/2012/fall/psets/4/walkthrough4.pdf · Reading Files fread(&data, size, number, inptr); &data: pointer to a struct which will contain the bytes you’re](https://reader034.vdocument.in/reader034/viewer/2022050410/5f86c8fa2d0f717b396444dd/html5/thumbnails/25.jpg)
Scale bmp image by a factor of n Usage: ./resize n infile outfile
Resize
![Page 26: pset 4: Forensicscdn.cs50.net/2012/fall/psets/4/walkthrough4.pdf · Reading Files fread(&data, size, number, inptr); &data: pointer to a struct which will contain the bytes you’re](https://reader034.vdocument.in/reader034/viewer/2022050410/5f86c8fa2d0f717b396444dd/html5/thumbnails/26.jpg)
Resize
Every pixel repeated n times
Every row repeated n times
n = 3
n = 2
![Page 27: pset 4: Forensicscdn.cs50.net/2012/fall/psets/4/walkthrough4.pdf · Reading Files fread(&data, size, number, inptr); &data: pointer to a struct which will contain the bytes you’re](https://reader034.vdocument.in/reader034/viewer/2022050410/5f86c8fa2d0f717b396444dd/html5/thumbnails/27.jpg)
TODO
Open file Update header info for outfile
![Page 28: pset 4: Forensicscdn.cs50.net/2012/fall/psets/4/walkthrough4.pdf · Reading Files fread(&data, size, number, inptr); &data: pointer to a struct which will contain the bytes you’re](https://reader034.vdocument.in/reader034/viewer/2022050410/5f86c8fa2d0f717b396444dd/html5/thumbnails/28.jpg)
Update header info
New bmp new header info What’s changing?
file size image size width height
Which structs need to be changed? How do you access those variables?
![Page 29: pset 4: Forensicscdn.cs50.net/2012/fall/psets/4/walkthrough4.pdf · Reading Files fread(&data, size, number, inptr); &data: pointer to a struct which will contain the bytes you’re](https://reader034.vdocument.in/reader034/viewer/2022050410/5f86c8fa2d0f717b396444dd/html5/thumbnails/29.jpg)
TODO
Open file Update header info for outfile Read each scanline, pixel by pixel Resize Horizontally Padding! Resize Vertically
![Page 30: pset 4: Forensicscdn.cs50.net/2012/fall/psets/4/walkthrough4.pdf · Reading Files fread(&data, size, number, inptr); &data: pointer to a struct which will contain the bytes you’re](https://reader034.vdocument.in/reader034/viewer/2022050410/5f86c8fa2d0f717b396444dd/html5/thumbnails/30.jpg)
Resize Horizontally
n = 2
![Page 31: pset 4: Forensicscdn.cs50.net/2012/fall/psets/4/walkthrough4.pdf · Reading Files fread(&data, size, number, inptr); &data: pointer to a struct which will contain the bytes you’re](https://reader034.vdocument.in/reader034/viewer/2022050410/5f86c8fa2d0f717b396444dd/html5/thumbnails/31.jpg)
TODO
Open file Update header info for outfile Read each scanline, pixel by pixel Resize Horizontally Padding! Resize Vertically
![Page 32: pset 4: Forensicscdn.cs50.net/2012/fall/psets/4/walkthrough4.pdf · Reading Files fread(&data, size, number, inptr); &data: pointer to a struct which will contain the bytes you’re](https://reader034.vdocument.in/reader034/viewer/2022050410/5f86c8fa2d0f717b396444dd/html5/thumbnails/32.jpg)
Padding
Padding isn’t an RGBTRIPLE we can’t fread padding
Infile image and outfile image have different padding!
![Page 33: pset 4: Forensicscdn.cs50.net/2012/fall/psets/4/walkthrough4.pdf · Reading Files fread(&data, size, number, inptr); &data: pointer to a struct which will contain the bytes you’re](https://reader034.vdocument.in/reader034/viewer/2022050410/5f86c8fa2d0f717b396444dd/html5/thumbnails/33.jpg)
Padding
If the number of pixels isn’t a multiple of 4, we need to add “padding” such that the scanline has a multiple of 4 bytes Padding is just zeros (0x00)
Hmm… a formula would come in handy!
![Page 34: pset 4: Forensicscdn.cs50.net/2012/fall/psets/4/walkthrough4.pdf · Reading Files fread(&data, size, number, inptr); &data: pointer to a struct which will contain the bytes you’re](https://reader034.vdocument.in/reader034/viewer/2022050410/5f86c8fa2d0f717b396444dd/html5/thumbnails/34.jpg)
TODO
Open file Update header info Read each scanline, pixel by pixel Resize Horizontally Padding! Resize Vertically
![Page 35: pset 4: Forensicscdn.cs50.net/2012/fall/psets/4/walkthrough4.pdf · Reading Files fread(&data, size, number, inptr); &data: pointer to a struct which will contain the bytes you’re](https://reader034.vdocument.in/reader034/viewer/2022050410/5f86c8fa2d0f717b396444dd/html5/thumbnails/35.jpg)
Resize Vertically
Several different ways to do this! 1. “Rewrite” methods
Remember pixels in an array Write array as many times as needed
2. “Re-copy” methods Go back to the start of the original row Repeat the horizontal resizing
![Page 36: pset 4: Forensicscdn.cs50.net/2012/fall/psets/4/walkthrough4.pdf · Reading Files fread(&data, size, number, inptr); &data: pointer to a struct which will contain the bytes you’re](https://reader034.vdocument.in/reader034/viewer/2022050410/5f86c8fa2d0f717b396444dd/html5/thumbnails/36.jpg)
Recover
![Page 37: pset 4: Forensicscdn.cs50.net/2012/fall/psets/4/walkthrough4.pdf · Reading Files fread(&data, size, number, inptr); &data: pointer to a struct which will contain the bytes you’re](https://reader034.vdocument.in/reader034/viewer/2022050410/5f86c8fa2d0f717b396444dd/html5/thumbnails/37.jpg)
JPEGs
JPEGs are also just sequences of bytes Each JPEG starts with either:
0xff 0xd8 0xff 0xe0 0xff 0xd8 0xff 0xe1
JPEGs are stored side-by-side on the CF card
![Page 38: pset 4: Forensicscdn.cs50.net/2012/fall/psets/4/walkthrough4.pdf · Reading Files fread(&data, size, number, inptr); &data: pointer to a struct which will contain the bytes you’re](https://reader034.vdocument.in/reader034/viewer/2022050410/5f86c8fa2d0f717b396444dd/html5/thumbnails/38.jpg)
Recover
Each represents 512 bytes
★ ★
★ ★
★
★
★
eof ★
![Page 39: pset 4: Forensicscdn.cs50.net/2012/fall/psets/4/walkthrough4.pdf · Reading Files fread(&data, size, number, inptr); &data: pointer to a struct which will contain the bytes you’re](https://reader034.vdocument.in/reader034/viewer/2022050410/5f86c8fa2d0f717b396444dd/html5/thumbnails/39.jpg)
Pseudocode
open card file
repeat until end of file
read 512 bytes into a buffer start of a new jpg? yes …
no …
already found a jpg? no …
yes …
close last jpg
close card file
![Page 40: pset 4: Forensicscdn.cs50.net/2012/fall/psets/4/walkthrough4.pdf · Reading Files fread(&data, size, number, inptr); &data: pointer to a struct which will contain the bytes you’re](https://reader034.vdocument.in/reader034/viewer/2022050410/5f86c8fa2d0f717b396444dd/html5/thumbnails/40.jpg)
Making JPG files
Filenames: ###.jpg JPEGs named in the order in which
they are found, starting at 0. (So keep track!)
sprintf(title,"%d.jpg", 2); title: char array to store the resultant string Hmm… this gives 2.jpg, not 002.jpg
How long is each array?
![Page 41: pset 4: Forensicscdn.cs50.net/2012/fall/psets/4/walkthrough4.pdf · Reading Files fread(&data, size, number, inptr); &data: pointer to a struct which will contain the bytes you’re](https://reader034.vdocument.in/reader034/viewer/2022050410/5f86c8fa2d0f717b396444dd/html5/thumbnails/41.jpg)
Contest!
![Page 42: pset 4: Forensicscdn.cs50.net/2012/fall/psets/4/walkthrough4.pdf · Reading Files fread(&data, size, number, inptr); &data: pointer to a struct which will contain the bytes you’re](https://reader034.vdocument.in/reader034/viewer/2022050410/5f86c8fa2d0f717b396444dd/html5/thumbnails/42.jpg)
this was walkthrough 4