Logo Search packages:      
Sourcecode: libgphoto2 version File versions

int gp_file_detect_mime_type ( CameraFile file  ) 

Parameters:
file a CameraFile
Returns:
a gphoto2 error code.

Definition at line 858 of file gphoto2-file.c.

References CHECK_NULL, CHECK_RESULT, GP_ERROR_IO_READ, GP_FILE_ACCESSTYPE_FD, GP_FILE_ACCESSTYPE_MEMORY, gp_file_set_mime_type(), and GP_OK.

{
      const char TIFF_SOI_MARKER[] = {(char) 0x49, (char) 0x49, (char) 0x2A,
                                (char) 0x00, (char) 0x08, '\0' };
      const char JPEG_SOI_MARKER[] = {(char) 0xFF, (char) 0xD8, '\0' };

      CHECK_NULL (file);

      switch (file->accesstype) {
      case GP_FILE_ACCESSTYPE_MEMORY:
            /* image/tiff */
            if ((file->size >= 5) && !memcmp (file->data, TIFF_SOI_MARKER, 5))
                  CHECK_RESULT (gp_file_set_mime_type (file, GP_MIME_TIFF))

            /* image/jpeg */
            else if ((file->size >= 2) && !memcmp (file->data, JPEG_SOI_MARKER, 2))
                  CHECK_RESULT (gp_file_set_mime_type (file, GP_MIME_JPEG))
            else
                  CHECK_RESULT (gp_file_set_mime_type (file, GP_MIME_RAW));
            return GP_OK;
      case GP_FILE_ACCESSTYPE_FD: {
            char  data[5];
            off_t offset;
            int   res;

            offset = lseek (file->fd, 0, SEEK_SET);
            res = read (file->fd, data, sizeof(data));
            if (res == -1)
                  return GP_ERROR_IO_READ;
            /* image/tiff */
            if ((res >= 5) && !memcmp (data, TIFF_SOI_MARKER, 5))
                  CHECK_RESULT (gp_file_set_mime_type (file, GP_MIME_TIFF))

            /* image/jpeg */
            else if ((res >= 2) && !memcmp (data, JPEG_SOI_MARKER, 2))
                  CHECK_RESULT (gp_file_set_mime_type (file, GP_MIME_JPEG))
            else
                  CHECK_RESULT (gp_file_set_mime_type (file, GP_MIME_RAW));
            lseek (file->fd, offset, SEEK_SET);
            break;
      }
      default:
            break;
      }
      return GP_OK;
}


Generated by  Doxygen 1.6.0   Back to index