@lay:3244l2

@fig:Listing 2. C language source that calls the MMX assembly language 
code

 1:  // File name: brightening.c
 2:
 3:  #include <stdio.h>
 4:  #include <stdlib.h>
 5:  
 6:  #define FILE_HEADER_SIZE        1078    // Windows BMP header size.
 7:  #define BK                      0x64    // Brightening Constant.
 8:
 9:  unsigned char BK_vector[8] = {BK, BK, BK, BK, BK, BK, BK, BK};
10:      
11:  extern int mmx_support(void); // Import assembly language definitions.
12:  extern void brightening(
13:      unsigned char *      BK_vector,
14:      unsigned char *      bitmap,
15:      size_t               iterations
16:      );
17:  //----------------------------------------------------------------------
18:  //    Creates an output image that results from brightening an input 
19:  //    image. For simplicity, both images use Windows gray scale 
20:  //    BMP file format. NO ERROR CHECKING DONE.
21:  
22:  int process_image(char * input_file_name, char * output_file_name)
23:      {
24:      FILE * in, * out;
25:      unsigned char * bitmap, header[FILE_HEADER_SIZE];
26:      size_t bitmap_size, adjusted_size;
27:    
28:      // Open input binary file.
29:      in = fopen(input_file_name, "rb");  
30:  
31:      // Calculate the bitmap size.
32:      fseek(in, 0L, SEEK_END);
33:      bitmap_size = ftell(in) - FILE_HEADER_SIZE;
34:  
35:      // Allocate dynamic memory for all the bitmap up to the nearest 
36:      // multiple of eight.
37:      adjusted_size = ((bitmap_size + 7) / 8) * 8;
38:      bitmap = malloc(adjusted_size * sizeof(char));
39:  
40:      // Read the header and the bitmap from the input file.
41:      rewind(in);
42:      fread(header, sizeof(char), FILE_HEADER_SIZE, in);
43:      fread(bitmap, sizeof(char), bitmap_size, in);
44:      fclose(in);
45:  
46:      // Call brightening assembly language routine.
47:      brightening(
48:          BK_vector, 
49:          bitmap,
50:          adjusted_size / 8
51:          );
52:  
53:      // Write result in output file.
54:      out = fopen(output_file_name, "wb");
55:      fwrite(header, sizeof(char), FILE_HEADER_SIZE, out);
56:      fwrite(bitmap, sizeof(char), bitmap_size, out);
57:      fclose(out);
58:  
59:      // Release dynamic memory.
60:      free(bitmap);
61:      }
62:  
63:  //----------------------------------------------------------------------
64:  //      Main function.   
65:  
66:  int main(void)
67:      {
68:      if(! mmx_support()) {
69:          printf("Sorry: There's no MMX support.\n");
70:          return EXIT_FAILURE;
71:          }
72:      process_image("image.bmp", "brighter_image.bmp");
73:      return EXIT_SUCCESS;
74:      }
