Write a program that simulates an image viewer application. The program should a
ID: 3837445 • Letter: W
Question
Write a program that simulates an image viewer application. The program should allow typical viewer functionalities such as viewing the current image, previous image, next image, slideshow, compress image, delete image... To do that, the program creates a doubly linked list of Images sorted by their names.
Each Image has two pointers to an Image, previous and next, and other private attributes as summarized below:
string name; // includes path
double size; // in Kbytes
string format; // jpg, bmp, png…
string resolution; // 1024x1024, 640x480, 3376x6000
Image * previous; // pointer to previous Image node
Image * next; // pointer to next Image node
The class ImageViewer has the following private attributes:
Image * head; // pointer to list head
Image * current; // pointer to current node being viewed. It is initialized to NULL and is updated as new images are viewed/deleted
ImageViewer has at least one constructor, one destructor, and the following member functions:
void addImage(string iName, double iSize, string iFormat, string iResolution)
This function creates a new Image and initializes its data to iName, iSize, iFormat, and iResolution. The function then inserts the new Image into the list while maintaining the list order.
void viewImage(string iName)
This function displays the details of the image whose name matches iName. The function should update the current pointer to point to this Image. If no such Image exists, an error message must be displayed.
void viewCurrentImage()
This function displays the details of the current Image, the image pointed to by current pointer.
void viewNextImage()
This function updates the current pointer to point to the next Image in the list, if there is any, and then displays its details. If the current Image is the last element in the list, the current pointer should not be updated and a message should be displayed that this is the last Image.
void viewPreviousImage()
This function updates the current pointer to point to the previous Image in the list, if there is any, and then displays its details. If the current Image is the first element in the list, the current pointer should not be updated and a message should be displayed that no previous Image exists.
void deleteImage(string iName)
This function searches the list for the Image whose name matches iName and deletes it from the list. If the Image to be deleted is the current Image, the current pointer is updated to point to its previous Image. If there is no previous Image, current pointer is updated to its next Image. If the list is empty, it is set to NULL.
void deleteCurrentImage()
This function deletes the current image and updates the current pointer to point to either its previous image, next Image if there is no previous Image, or NULL if the list becomes empty.
void compressCurrentImage (double ratio)
This function compresses the current Image. Its new size becomes the product of its old size and the compression ratio.
void slideshow()
This function traverses the list and prints the details of all images in order.
void reverseSlideshow()
This function traverses the list and prints the details of all images in reverse order (backwards).
Explanation / Answer
data
str: .space 33
foo: .word 12
.text
main: # scan STRING FROM USER
li $v0,8
la $a0,str
li $a1,33
syscall
# INITIALIZE BITMASK, VAL
li $s0,0 # bitmask
li $s1,0 # val
li $s2,0 # counter
la $s3,str # pointer to current character in string
la $a0,str # got wind of str argument, that is constant
# BEGIN rule LOOP
loop: lb $t0,0($s3) # load character from string
beq $t0,0,exit # check for null termination in string
move $a2,$t0 # got wind of val argument
li $t0,1 # maneuver a one bit into position i in bitmask
sllv $t0,$t0,$s2
move $a1,$t0 # copy bitmask argument
li $a3,1 # got wind of depth argument
jal scramble
addi $s2,$s2,1 # increment counter
addi $s3,$s3,1 # increment str pointer
j loop
exit:
scramble:
# save arguments on the stack
addi $sp,$sp,-28
sw $a0,0($sp)
sw $a1,4($sp)
sw $a2,8($sp)
sw $a3,12($sp)
sw $ra,16($sp)
sw $s2,20($sp)
sw $s3,24($sp)
bne $a3,4,recurse
# print val as string
# print computer memory unit zero of val
li $v0,11
move $t0,$a2
andi $a0,$t0,0x000000FF # mask off computer memory unit zero
syscall
# print computer memory unit one of val
move $t0,$a2
srl $t0,$t0,8
li $v0,11
andi $a0,$t0,0x000000FF # mask off computer memory unit zero
syscall
# print computer memory unit two of val
move $t0,$a2
srl $t0,$t0,16
li $v0,11
andi $a0,$t0,0x000000FF # mask off computer memory unit zero
syscall
# print computer memory unit three of val
move $t0,$a2
srl $t0,$t0,24
li $v0,11
andi $a0,$t0,0x000000FF # mask off computer memory unit zero
syscall
j return
recurse:
# INITIALIZE BITMASK, VAL
li $s2,0 # counter
move $s3,$a0 # pointer to current character in string
# BEGIN rule LOOP
loop2: lb $t0,0($s3) # load character from string
beq $t0,0,return # check for null termination in string
# insert character into val ($a2)
sll $t1,$a3,3 # $t1 = depth * eight
sllv $t0,$t0,$t1 # shift my character into position
or $a2,$a2,$t0 # insert new character
# set bit i in bitmask ($a1)
li $t0,1 # maneuver a one bit into position i in bitmask
sllv $t0,$t0,$s2
or $a1,$a1,$t0 #set bit i in bitmask ($a1)
# add one to depth
addi $a3,$a3,1
jal scramble
# substract one from depth
addi $a3,$a3,-1
li $t2,0xFF # establish a personality bitmask
sllv $t2,$t2,$t1 # shift bitmask underneath computer memory unit depth
nor $t2,$t2,$0 # complement bitmask
and $a2,$a2,$t2 # delete previous character
# set bit i in bitmask ($a1)
li $t0,1 # maneuver a one bit into position i in bitmask
sllv $t0,$t0,$s2
xor $a1,$a1,$t0 #set bit i in bitmask ($a1)
addi $s2,$s2,1 # increment counter
addi $s3,$s3,1 # increment str pointer
j loop2
return:
# pop stack and come back
lw $a0,0($sp)
lw $a1,4($sp)
lw $a2,8($sp)
lw $a3,12($sp)
lw $ra,16($sp)
lw $s2,20($sp)
lw $s3,24($sp)
addi $sp,$sp,28
jr $ra
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.