Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Translate C code into MIPS program that will: 1) read in an image name (gray level image in binary format), the number of rows and

Translate C code into MIPS program that will:

1) read in an image name (gray level image in binary format), the number of rows and the number of columns

2) using a function read in the image (.bin file, see code below)

3) using a function compute its histogram (see code below)

4) using a function then output the histogram (see expected output below)

You can assume the image size is no larger than 500x500.

Expected output at end of page

FILE read_pgm.c 
#include #include #include #include #include unsigned char *read_pgm(char *fname, int *nrow, int *ncol) { int fd; char str[256]; char P5_str[32]; int i, j, k; unsigned char *chp; unsigned char *image; fd = open(fname, O_RDONLY); if (fd < 0) { /* In the MIPS version, you can assume that the file is always valid */ return (unsigned char *) NULL; } read(fd, P5_str, (int)3); /* Now we read the header of the pgm file */ k = -1; do { k++; read(fd, &(str[k]), 1); } while (str[k] != 10); str[k] = 0; sscanf(str, "%d%d", nrow, ncol); printf("Rows %d columns: %d ", (*nrow), (*ncol)); image = (unsigned char *)malloc((*nrow) * (*ncol)); chp = image; for (i=0; i < (*nrow); i++) { for (j=0; j < (*ncol); j++) { read(fd, chp, 1); /* printf("%d ", *chp); */ chp++; } } close(fd); return image; } 
END OF FILE read_pgm.c  FILE histogram.c /* Here image is the starting address of image, which has nrow rows and ncol columns. h is the histogram of the image. */ #include int histogram(unsigned char *image, int nrow, int ncol, int *h) { int i, j, k; unsigned char *p; for (i=0; i < 256; i++) h[i] = 0; p = image; k = 0; for (i=0; i < nrow; i++) { for (j=0; j < ncol; j++) { h[*p] ++; /* printf("%d ", *p); */ p++; k ++; } } return k; } 

END OF FILE histogram.c

MAIN PROGRAM #include 
extern int histogram(unsigned char *image, int nrow, int ncol, int *h); extern unsigned char *read_pgm(char *fname, int *nrow, int *ncol); int main(int argc, char *argv[]) { char fname[256]; unsigned char *image; int nrow, ncol; int h[256]; int i; printf("Please enter a pgm image file name: "); scanf("%s", fname); image = read_pgm(fname, &nrow, &ncol); if (image != (unsigned char *)NULL) { printf("Image with %d rows and %d columns. ", nrow, ncol); histogram(image, nrow, ncol, h); printf("The histogram of image \"%s\" is ", fname); for (i=0; i < 256; i++) { printf("%d ", h[i]); } printf(" "); } return 0; } 

Expected output:

Please enter a valid file name: \usr\example.bin \usr\example.bin Number of rows: 43 Number of columns: 21 First pixel value: 87. Last pixel value: 88. 0: 0. 1: 0. 2: 0. 3: 0. 4: 0. 5: 0. 6: 0. 7: 0. 8: 0. 9: 0. 10: 0. 11: 0. 12: 0. 13: 0. 14: 0. 15: 0. 16: 0. 17: 0. 18: 0. 19: 0. 20: 0. 21: 0. 22: 0. 23: 0. 24: 0. 25: 0. 26: 0. 27: 0. 28: 0. 29: 0. 30: 0. 31: 0. 32: 0. 33: 0. 34: 0. 35: 0. 36: 0. 37: 0. 38: 0. 39: 0. 40: 0. 41: 0. 42: 0. 43: 0. 44: 0. 45: 0. 46: 0. 47: 0. 48: 0. 49: 0. 50: 0. 51: 0. 52: 0. 53: 0. 54: 0. 55: 0. 56: 0. 57: 0. 58: 0. 59: 0. 60: 0. 61: 0. 62: 0. 63: 0. 64: 0. 65: 0. 66: 0. 67: 2. 68: 2. 69: 4. 70: 6. 71: 5. 72: 8. 73: 14. 74: 12. 75: 13. 76: 8. 77: 27. 78: 22. 79: 22. 80: 25. 81: 12. 82: 19. 83: 12. 84: 10. 85: 5. 86: 15. 87: 20. 88: 24. 89: 8. 90: 7. 91: 9. 92: 11. 93: 12. 94: 10. 95: 7. 96: 9. 97: 7. 98: 6. 99: 5. 100: 4. 101: 7. 102: 11. 103: 6. 104: 6. 105: 4. 106: 2. 107: 6. 108: 3. 109: 2. 110: 1. 111: 9. 112: 7. 113: 3. 114: 5. 115: 4. 116: 6. 117: 7. 118: 4. 119: 4. 120: 8. 121: 10. 122: 8. 123: 5. 124: 3. 125: 2. 126: 5. 127: 4. 128: 0. 129: 4. 130: 2. 131: 2. 132: 7. 133: 1. 134: 3. 135: 5. 136: 4. 137: 3. 138: 0. 139: 4. 140: 4. 141: 6. 142: 7. 143: 6. 144: 6. 145: 5. 146: 5. 147: 5. 148: 6. 149: 3. 150: 3. 151: 4. 152: 5. 153: 1. 154: 4. 155: 3. 156: 2. 157: 3. 158: 3. 159: 2. 160: 1. 161: 4. 162: 3. 163: 1. 164: 6. 165: 4. 166: 1. 167: 7. 168: 7. 169: 4. 170: 4. 171: 5. 172: 2. 173: 5. 174: 3. 175: 3. 176: 2. 177: 2. 178: 2. 179: 3. 180: 4. 181: 5. 182: 4. 183: 3. 184: 3. 185: 3. 186: 4. 187: 2. 188: 4. 189: 6. 190: 6. 191: 2. 192: 2. 193: 10. 194: 5. 195: 7. 196: 3. 197: 5. 198: 0. 199: 4. 200: 5. 201: 2. 202: 8. 203: 4. 204: 4. 205: 9. 206: 3. 207: 6. 208: 1. 209: 2. 210: 8. 211: 2. 212: 4. 213: 3. 214: 3. 215: 4. 216: 7. 217: 5. 218: 9. 219: 4. 220: 5. 221: 3. 222: 1. 223: 3. 224: 4. 225: 1. 226: 0. 227: 0. 228: 0. 229: 0. 230: 0. 231: 0. 232: 0. 233: 0. 234: 0. 235: 0. 236: 0. 237: 0. 238: 0. 239: 0. 240: 0. 241: 0. 242: 0. 243: 0. 244: 0. 245: 0. 246: 0. 247: 0. 248: 0. 249: 0. 250: 0. 251: 0. 252: 0. 253: 0. 254: 0. 255: 0. 

Thank you and please comment if you need any more info

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Database Experts Guide To Database 2

Authors: Bruce L. Larson

1st Edition

0070232679, 978-0070232679

More Books

Students also viewed these Databases questions