Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 24th, 2012  |  syntax: None  |  size: 1.32 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <iostream>
  4.  
  5. #define RGB_HEADER_LENGTH 0xCC
  6. #define RGB_BYTES_PER_PIXEL 3
  7. #define RGB_WIDTH_OFFSET 0x34
  8. #define RGB_HEIGHT_OFFSET 0x40
  9.  
  10. unsigned char * readRGBtiff(char * filename, int * width, int * height){
  11.  
  12.         // Get handle to file
  13.         FILE *fstream= fopen(filename, "r");
  14.  
  15.          // Seek to the width
  16.         fseek(fstream, RGB_WIDTH_OFFSET, SEEK_SET);
  17.  
  18.         // Attempt to read width
  19.         if(!fread(width, sizeof(*width), 1, fstream))
  20.         {
  21.                 printf("Error reading width...\n");
  22.         }
  23.  
  24.         // Seek to height
  25.         fseek(fstream, RGB_HEIGHT_OFFSET, SEEK_SET);
  26.  
  27.         // Attempt to read height
  28.         if(!fread(height, sizeof(*height), 1, fstream))
  29.         {
  30.                 printf("Error reading height...\n");
  31.         }
  32.  
  33.         // Seek to RGB data
  34.         fseek(fstream, RGB_HEADER_LENGTH, SEEK_SET);
  35.  
  36.         int fileSize = (*width) * (*height) * RGB_BYTES_PER_PIXEL;
  37.  
  38.         // Allocate space
  39.         unsigned char* buffOut = (unsigned char*)malloc(fileSize - RGB_HEADER_LENGTH + 1);
  40.        
  41.         if(buffOut == NULL)
  42.         {
  43.                 printf("malloc failed\n");
  44.         }
  45.  
  46.         unsigned int readLen = fileSize - RGB_HEADER_LENGTH;
  47.         if(!fread(buffOut, readLen, 1, fstream))
  48.         {
  49.                 printf("Error reading RGB\n");
  50.         }
  51.  
  52.         return buffOut;
  53. }
  54.  
  55. int main()
  56. {
  57.         int width, height;
  58.  
  59.         readRGBtiff("C:\\test.tif", &width, &height);
  60.  
  61.         printf("w: %d, h: %d\n\n", width, height);
  62.  
  63.         int k =0;
  64.  
  65. }