首页 > > 详细

辅导 Assignment 2: Pixel Graphics辅导 R编程

Assignment 2: Pixel Graphics

Overview

In this assignment, you will implement functions which perform. drawing operations into an in-memory image representation, in both C and x86-64 assembly language.

Warning: Assembly language programming is challenging! Make sure you start each milestone as soon as possible, work steadily, and ask questions early. Also, writing unit tests and using gdb to examine the detailed behavior. of code under test will be critical to successful implementation of the assembly language functions.

Non-functional requirements

In Milestones 2 and 3, you will be writing assembly language functions. You must write these "by hand", and your assembly code must have very detailed code comments explaining the purpose of each assembly language instruction.

It is not allowed to generate assembly code using a C compiler and submit this as your own code. We will assign a grade of 0 to any submissions containing compiler-generated code where hand-written assembly language is expected.

Your submission for each milestone should include a README. txt file describing how you and your partner divided the work, and letting us know about any interesting implementation details. If there is functionality you weren't able to get working completely, this is a good place to mention that.

We expect you to follow the style. guidelines. However, the expectations for function length will be relaxed considerably for your assembly language code. It is not unusual for an assembly language function to have 100 or more lines of code. In the reference solution, the longest function was 170 lines, although there was extensive use of comments and whitespace to improve readability.

Of course, you should strive to make your assembly language functions as simple and readable as possible.

We expect your code to be free of memory errors. You should use valgrind to test your code to make sure there are no uses of uninitialized variables, out of bounds memory reads or writes, etc. This applies to both your C code and your assembly code.

Getting started

To get started, download csf_assign02.zip and unzip it.

Grading breakdown

Your grade for the assignment will be determined as follows:

Milestone 1: 30%

o Implementation of C drawing functions: 12.5%

o Unit testing of helper functions: 12.5%

o Design/coding style. of C functions: 5%

Milestone 2: 35%

o Functional correctness of draw_pixel: 15%

Unit testing of helper functions (existence of tests, evidence that they pass): 15%

o Design/coding style. of assembly functions: 5%

Milestone 3: 35%

o Functional correctness of draw_rect: 13.5%

o Functional correctness of draw_circle : 13.5%

o Functional correctness of draw_tile: 1.5%

o Functional correctness of draw_sprite:1.5%

o Design/coding style. of assembly functions: 5%

Note that in each milestone, we expect all of the tests executed by your unit test program to pass. For MS2 in particular, you can comment out calls to test functions that aren't related to draw_pixel. For example, your main function might have code similar to the following:

The tests for in_bounds,blend_colors,blend_components, and draw_pixel are enabled because they are all test functions involved in the implementation of draw pixel The test for draw_rect is commented out because it is not part of the functionality expected for MS2.

Drawing functions

Here are the prototypes of the drawing functions you will implement:

Briefly:

draw_pixel draws a single pixel, blending the specified color value with the current background color

draw_rect draws a filled rectangle with the specified upper-left corner, width and height, blending the specified color with the colors of the existing pixels

draw_circle draws a filled circle of specified radius centered at the specified x /y coordinates, blending the specified color with the colors of the existing pixels

draw_tile copies pixels from the specified rectangular region of a tilemap image to the specified location (x /y) of a destination image, without blending any of the copied colors with the existing colors

draw_sprite is similar to draw_tile, but the pixels of the copied "sprite" are blended with the existing pixel colors

struct Image and struct Rect data types

To understand the functionality of the drawing functions, it is necessary to understand the struct Image and struct Rect data types, as well as how colors are represented.

The struct Image type is defined as follows:

The width and height fields define the width and height of an image, in pixels. The data field is a pointer to a dynamically-allocated array of uint32_t values, each one representing one pixel. The pixels are stored in row-major order, starting with the top row of pixels.

A color is represented by a uint32_t value as follows:

Bits 24-31 are the 8 bit red component value, ranging from 0-255

Bits 16-23 are the 8 bit green component value, ranging from 0-255

Bits 8-15 are the 8 bit blue component value, ranging from 0-255

Bits 0-7 are the 8 bit alpha value, ranging from 0-255

The alpha value of a color represents its opacity, with 255 meaning "fully opaque" and O meaning "fully transparent". (See the Color blending section for details on how an alpha value allows two colors to be "blended".)

The struct Rect data type is defined as follows:

An instance of struct Rect describes a rectangle where the upper-left corner is specified by x and the width of the rectangle (in pixels) is specified by width, and the height of the rectangle (in pixels) is specified by height

Color blending

The color values of the destination image are always fully opaque, with an alpha value of 255.

When a pixel is drawn to a destination image by any operation other than draw_tile, the pixel's color, which we'll call the "foreground" color, is blended with the existing color at the location where the pixel is being drawn, which we'l call the "background" color. To find the correct color value for the new pixel, the following computation is performed for each color component, where f is the foreground color component value, b is the background color component value, and o is the alpha value of the foreground color:

Note that the result of the division is truncated rather than being rounded, so if you use integer division, it will behave in the expected way.

A blended color should have each color component value (red, green, and blue) computed using the formula above, and the alpha value of the blended color should be set to 255.

The draw_pixel, draw_rect, draw_circle, and draw_sprite functions all should use color blending. The exception is the draw_tile function, which does not blend the copied tile colors with the existing pixel colors.

Drawing a circle

The drawing functions should be implemented entirely using integer arithmetic. When drawing a filled circle, all of the pixels within r units of distance from the circle's center should be drawn with the specified color. Ordinarily, if the pixel's center is at x, y and the point being considered is at j, i , determining if j, i is in the circle would be determined by the inequality

The square root operation would require floating point math. However, we could square both sides of the inequality to give us

This computation only requires integer arithmetic.




联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!