Placeholder Image

字幕表 動画を再生する

  • All right.

  • Hello, world.

  • This is CS 50 on Twitch.

  • My name is Colton Ogden, and I'm joined today by Bryan and David for a part to code review Stream.

  • Yeah, I did a lot of fun the first time, and now we brought Brian one of our best teaching fellows here on campus.

  • Good to be here.

  • Yeah, I wasn't on sometime last time.

  • We had a ton of viewership, and it's super interesting getting the test.

  • See everybody's projects, a wide range of programming, languages.

  • Last time I looked at some python programming languages are projects programming projects on.

  • And I think we looked at, um, html page.

  • Actually, night.

  • Nate was 10 years old indeed.

  • Broad audience out there.

  • So it's good to see some familiar names and faces and emojis here.

  • And welcome to reveal who just followed as well.

  • Yeah, and we had another follower.

  • Actually, we're adjusting our view here a little bit in real time so that we can actually see not only the comments nice and wide, but also our followers here.

  • So sayyed 055 as well.

  • Thank you very much for the follow Lita to G and D Manda.

  • Oh, and we have a few, actually.

  • And see Miss Sweden.

  • Hugh, thank you very much for the follows.

  • We really should just say hello to people's numbers.

  • Yeah, maybe it might be a little easier to pronounce you, but Yeah.

  • So we have a bunch of awesome project today.

  • And also thanks to Brian for joining us for the first time.

  • Absolutely.

  • And many more going forward.

  • Why don't we?

  • Why don't we dive right into our first product roll?

  • Sure.

  • So if you're new to the stream, we via Colton solicited some code that folks had worked on other for personal projects or otherwise by a Google form a few weeks ago.

  • And we got a number of submissions python, html, c and other languages.

  • And so what Brian and I thought we would do today with Colton here is just provide some feedback, much like our teaching fellows here on campus Would, based on the design thing, efficiency, the quality of the code that we're seeing and certainly take all this with a grain of salt.

  • Reasonable people will disagree, so you don't need to take our thoughts as cannon.

  • But hopefully it's helpful food for thought whether it's your coat we're looking at or code, that's of interest.

  • Some of some of us might even disagree, We shall say Probably not.

  • Probably not with a bunch of regulars in the chat with ST Nate.

  • Uh, we just actually mentioned has submitted a thing last week.

  • And actually, is he saying that they submitted more code and they hope that they contribute again?

  • Well, thank you.

  • And, uh, we should probably preface this by plugging the actual you, Earl for the form for people that want to submit us projects in the future for more episodes after this one.

  • This is part two.

  • We didn't another one.

  • You can watch the video on YouTube.

  • If you goto billy slash CS 50 twitch code review, you can submit a get up or a Google form with your gift every but we're just and then you could be in any language.

  • Can be a small project, be an advanced project.

  • But we'd be happy to take a look at it.

  • Graded on a correctness, not practice on design and style.

  • Not correctness will not be bug testing.

  • You know, I think we need your else order for our u R l sort.

  • Yeah.

  • We probably should go to a C study that.

  • But what we have here a CZ the first project, and we sort of looked these a little bit in advance and cure.

  • At least, um, sort curated.

  • Want them in kind of an order.

  • This is a C library, which is kind of cool because we didn't actually look at any C code in the last video, but sees 50 teachers see a sort of the first language on ramps.

  • Everybody to the more dynamic languages.

  • It's kind of cool that maybe look at a real world project in C.

  • Yeah, It looks like we have a project here from Noah.

  • 1101 to live picks map on for the top of this.

  • It get hub.

  • You're able to put a description for every project he put on.

  • Get home.

  • This is a simple to use library to read and write P p.

  • M.

  • Or portable picks, map images.

  • Frankly, I was not familiar too much with p p.

  • M.

  • Just a few minutes ago.

  • Yeah, I think so.

  • But I think that's okay.

  • So let's actually go ahead.

  • And Google, for instance, on Wikipedia is probably a great source of information here, and indeed, there's our file format.

  • But in a different number of contexts here, uh, let's see pages per minutes.

  • It's probably this portable picks map format.

  • So Annette's PBM format is any graphics format used and defined by the Nets PBM Project.

  • Uh, this is the, uh, these air image file formats designed to be easily exchanged between platforms.

  • And they are with us since, like the 19 eighties, it seems.

  • Yeah, it looks like in the 19 eighties, there were first developed in order to make it easier to transmit images via e mails where you wanted to be able to send an email, that an image in it.

  • And it looks like this format.

  • The Net PBM format would allow you to take an image, turn it into a plane, ask you text, according to this article, in order to just send it more easily from one person to another over the Internet.

  • So no, if you're tuning in or if you tune and after certainly let us know why or what motivated this project because it's quite interesting, it seems, but as we did last time, shall we just dive right into the gate, huh?

  • Yeah.

  • And I should also mention that there get abusing Was actually from there.

  • Twitch.

  • User name, which we did solicit.

  • Twitch.

  • Every release is the talk of an eye out for every release other today or in some future stream.

  • So the way I only start by diving into the students code or colleagues code is just to look at, like, read me.

  • And I think if we scroll down here on, get Hub will indeed see, read me from Noah.

  • So here we have a nice description.

  • Frankly, we should just roll before I guess, what is live picks?

  • Map.

  • It's a library and impure.

  • See the names to provide an easy and convenient A P I for reading and writing.

  • PPM files four features opening ppm images creating blank ppm images, setting and retrieving the color values for specific pixels.

  • Not unlike what we do in CS 50 on dhe set and get all pixels.

  • This extra sounds reminiscent of what we do with bit map files, which is another relatively simple image format.

  • What I like is that this library is very simple, but very flexible, like you could use this to dynamically generate any image data based at least when I'm reading here.

  • But the fact that you create a blink image and then senator chief color values yeah, that's great.

  • That's good way to start because other formats that are more recent like J.

  • Peg are actually very highly optimized for compression and use much fancier algorithms to store the information.

  • It's not quite as easy, therefore, to get at individual pixel values.

  • All right, So should we take a look at an example, for instance, and get a sense of how this all works?

  • You wanna walk us through what you're seeing here and how you would go about reading someone's code for the first time?

  • Yeah, sure.

  • So we have the main function there, So it seems like well up at the top before the main function there, including Standard Out H, which you might be familiar with a few chickens.

  • 50.

  • We use that in a lot of see programs and then there, including picks map dot each, which looks like it's probably the header file for the library that Noah has created here, and that's gonna give him access to some of the types that we're going to see in just a moment, probably in some of the functions.

  • Meanwhile, so inside the main function we're defining a something of type picks WMAP image.

  • So not an image that's built in deceased's probably have some sort of definition that will take a look at just the moment on.

  • We're fighting a pointer to it called Image, and then we're running a function that looks like he's defined called picks WMAP Image open.

  • I presume this is gonna be some file path to the image that we're going to try and open, and we're gonna store some sort of data about it inside of what's pointed to by the Pointer image.

  • And we looks like there's an air checking here, checking to make sure that we're able to open the image successfully.

  • If we weren't able to open the image successfully, we print in error, return a non zero error code, And what would it mean for image to be not image like?

  • What is this?

  • Checking for C.

  • U S o N C.

  • When you have ah pointer value like image and you have exclamation point image or bang image that's checking to see whether or not image is equal to know.

  • And so no stands for the no pointer, meaning image is not pointing to anything.

  • And so it's a convention and a lot of functions.

  • If we try and run, pick snap image open, for instance, If we couldn't open the image, for instance, if the file didn't exist at the particular file path, then what we meant to get back is rather than a pointer to an actual picks me up image.

  • You might get a pointer.

  • That's just know.

  • And so if bang images checking to see if images know in which case something went wrong and we print that air.

  • So it's really like checking for zero really or fall.

  • So it's not false.

  • Then go ahead, right?

  • Do this.

  • So I'm a little nervous and we wouldn't know really until we dive into the code.

  • But because images declared is a pointer.

  • As per this asterisk right here, a pointer to a picks mapped image.

  • I'm a little nervous that were then in the sample code, getting the address of the pointer unless this image is actually gonna do a lot of our memory allocation for us and then change what is at what point changed the contents of that pointer itself as opposed to what it's pointing it.

  • But we'll see.

  • Yeah, that's a good place.

  • Is appointed to a pointer, which feels a little a little worrisome.

  • But maybe it's correct.

  • Yeah, all right.

  • And then down here, Do you want to fight on for what's going on a little further with these prints?

  • Okay, so next it looks like we're trying to print the images within height.

  • And in order to do that, looks like Noah's to find some functions, picks my image, picks WMAP image, get with and picks me up.

  • Image get height, each of which is taking that pointer a pointer to a pixel image.

  • And presumably those functions are able to take that image and somehow where inside the data stored within the image get access to the width and the height of that image and maybe a couple points year.

  • If you've been taking CS 50 you might know that in recent years we actually introduce students 2% I for integer well percent D for decimal into Juries almost identical, and it is in fact, with the context of print depth.

  • It's a little different if you're using scanning for actually getting user input.

  • But that's otherwise.

  • The same thing is percent I and I think it's worth noting.

  • For those of you who have heard about or familiar with object oriented programming, this is a convention.

  • And see that, frankly starts to get messy pretty quickly and see there is no notion of object orientation.

  • And so even though image kind of sounds like an object that represents an actual image, and you'd like to just, like, look inside of it for its with or its height, you could do that if it's a struck.

  • But if there's more to it, you sometimes need to pass these structures into other functions and let them figure out what the height the width is.

  • So this is a common procedural paradigm.

  • I've seen this in certain, like game code bases to and see they have to exact gonna pass everything around his inputs.

  • All right, so just a few more lines here, let's see if we can glean what's going on here.

  • Maybe, All right, so it looks like we're defining a variable of a new type.

  • We saw the type picks that image before.

  • And now we have picks matte r B R g b, which was probably red, green blue, standing in place for a single pixel that has a bunch of color values.

  • And now we've got this function picks WMAP image Get pixel.

  • Looks like it takes four arguments.

  • One is the image that we're trying to get the pixel from, and then we have two numbers and I'm assuming those air like an X and Y coordinate of where in the image we want to try and extract a pixel from.

  • And then the remaining is okay.

  • We've gotten the pixel from the image and that we probably need to tell the function.

  • Where do you want to store the resulting information about the pixel?

  • And so we're passing in a pointer to that picks up RGB variable that we just to find a couple lines earlier.

  • Mainly, it looks like also, if we do happen to accidentally pass in 1000 images say, 500 pixels wide, it does return negative one.

  • So there is some error checking that we can cleanly do.

  • That's true.

  • We're not actually using the return value in this example of you could imagine you might want to add some air checking here.

  • We said it equal to an end to then check to see if that result is negative one, because that would mean that the coordinates supplied or not in the bounds of the image on based on the next three lines, it looks like pixel is probably a structure of some kind because we're using this dot notation to say pixel has a whole bunch of properties within it a red, green and blue property.

  • And the dot notation and see is how you access a member of instruct and get access to you.

  • Just the red value of the pixel, the green value of the excellent value that exult looks like those air integers because we're printing them out using percent D.

  • Um, and then we're creating a new pixel again.

  • Creating a new pics map are RGB, and this time, rather than reading it from the image we know it's instruct.

  • We know it has members called red, green and blue, so we could also create a pixel just by defining what we want the red, green and blue values to be for ourselves.

  • We want to decide what we want.

  • The pixels color to be This has a red value of 50 green of 1 50 blue of 200 which is probably gonna be a greenish blue color, I would guess.

  • And then where it looks like we're setting the pixel, we're calling a function called picks WMAP image set pixel, which falls a similar format to get pixel.

  • But it looks like instead of we're going to be providing the image and the coordinates.

  • And rather than reading a pixel from those coordinates were taking a pixel of our own, namely new pixel, and saying we would liketo update the image most likely and set the books of pixel it coordinate 100 comma 100 to be this new pixel that we've just created again.

  • I like the fact that you could just do this dynamically and generate textures this way because this has applications in game development.

  • If you want to randomly generate textures for stuff, that's pretty cool to just change the colors suit of randomly as opposed to heart.

  • Exactly.

  • If you wanted to make Mario, for example, have a red outfit, you could do that by there are wasted and hardware in the olden days.

  • But now, generally you just have ah, to the image is Mario.

  • You can just choose.

  • You can either choose a layer on top of Mario or just re color Mario Sprite.

  • But this way you could actually handset however you want it or just make him a new question from scratch using algorithms.

  • Well, you know, it's worth noting here, and I'm just inferring this from how this functions being used.

  • The fact that new pixels being passed in by pointer or by reference so to speak, is actually an interesting technique.

  • Because, strictly speaking, I'm gonna guess that when you set a pixels color to be whatever this pixel represents at Location 100 come a 100.

  • Strictly speaking, you don't intend to change anything inside this picture.

  • You're just passing it in for It's red, it's green, it's blue value.

  • But the interesting thing is, if each of these values red, green and blue is each using like an integer to store it, that might be 32 bits plus 32 bits plus 32 beds.

  • So three times the number three ends in total.

  • Well, you can actually get away by passing in and therefore copying fewer bids by just passing in that entire new pixel with those three fields and anything else that's tucked inside of the struck by just one end, which is gonna be into dress.

  • So this is a common technique in C for efficiency.

  • You can pass in a very large data structure to another function is input, but you do it by way of a single value.

  • It's a dress, so it's not waste time copying an entire structure you can imagine the same thing applies to, like, pick picks, Matt Image get with two images, pointer.

  • In this case, you will be the same exact thing.

  • Now you wanna be careful.

  • There are techniques that we kind of sort of showing CS 50 where you can declare variables or parameters to functions is being constant constant because the danger in passing and something as a dress like this is that that means the function you're calling theoretically could change the contents.

  • So you try to at least put a barrier of defense between you and some accidental code.

  • It was the constant, a fairly recent scything Has it been with sea since, Like the seven?

  • Pretty sure that's a pretty old yet.

  • Okay, And then I could like this one off.

  • I'm guessing here this is what's freeing the image and bring up any memory that was dynamically allocated by the original function that actually created this image in the first place.

  • So should we dive into the library itself and see if we can't provide some helpful feedback accordingly?

  • A saying.