| 23 | |
| 24 | |
| 25 | int main (int argc, char *argv[]) { |
| 26 | int rc, i, j; |
| 27 | |
| 28 | if (argc != 2) { |
| 29 | fprintf(stderr, "USAGE: %s filename.jpg\n", argv[0]); |
| 30 | exit(EXIT_FAILURE); |
| 31 | } |
| 32 | |
| 33 | // SSS EEEEEEE TTTTTTT U U PPPP |
| 34 | // SS SS E T U U P PP |
| 35 | // S E T U U P PP |
| 36 | // SS E T U U P PP |
| 37 | // SSS EEEE T U U PPPP |
| 38 | // SS E T U U P |
| 39 | // S E T U U P |
| 40 | // SS SS E T U U P |
| 41 | // SSS EEEEEEE T UUU P |
| 42 | |
| 43 | // Variables for the source jpg |
| 44 | struct stat file_info; |
| 45 | unsigned long jpg_size; |
| 46 | unsigned char *jpg_buffer; |
| 47 | |
| 48 | // Variables for the decompressor itself |
| 49 | struct jpeg_decompress_struct cinfo; |
| 50 | struct jpeg_error_mgr jerr; |
| 51 | |
| 52 | // Variables for the output buffer, and how long each row is |
| 53 | unsigned long bmp_size; |
| 54 | unsigned char *bmp_buffer; |
| 55 | int row_stride, width, height, pixel_size; |
| 56 | |
| 57 | |
| 58 | // Load the jpeg data from a file into a memory buffer for |
| 59 | // the purpose of this demonstration. |
| 60 | // Normally, if it's a file, you'd use jpeg_stdio_src, but just |
| 61 | // imagine that this was instead being downloaded from the Internet |
| 62 | // or otherwise not coming from disk |
| 63 | rc = stat(argv[1], &file_info); |
| 64 | if (rc) { |
| 65 | fprintf(stderr, "FAILED to stat source jpg\n"); |
| 66 | exit(EXIT_FAILURE); |
| 67 | } |
| 68 | jpg_size = file_info.st_size; |
| 69 | jpg_buffer = (unsigned char*) malloc(jpg_size + 100); |
| 70 | |
| 71 | int fd = open(argv[1], O_RDONLY); |
| 72 | i = 0; |
| 73 | while (i < jpg_size) { |
| 74 | rc = read(fd, jpg_buffer + i, jpg_size - i); |
| 75 | printf("Input: Read %d/%lu bytes\n", rc, jpg_size-i); |
| 76 | i += rc; |
| 77 | } |
| 78 | close(fd); |
| 79 | |
| 80 | // SSS TTTTTTT A RRRR TTTTTTT |
| 81 | // SS SS T A A R RR T |
| 82 | // S T A A R RR T |