Thread: Jpeg header

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    4

    Jpeg header

    I just start learning C the hard way and i am new to this forum and i hope to
    get some advice or if i 'm the right direction.

    this the main.c file
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <byteswap.h>
    #include <stdint.h>
    #include "jpeg.h"
    
    enum types {
        PDF,
        JPEG,
        PNG,
        GIF,
        INVALID
    };
    
    typedef enum types format;
    
    format checkFormat(FILE *fd, const uint8_t buffer[]){
       if( fd == NULL){
          printf("Invalid file format");
    }
       switch (buffer[0]){
          case 0x25:
             printf(" PDF \n");
             return PDF;
          case 0x89:
             printf(" PNG \n");
             return PNG;
          case 0x47:
             printf(" GIF \n");
             return GIF;
         case 0xFF:
             printf(" JPEG \n");
             return JPEG;
         default:
             return INVALID;
    }
    }
    
    int main(int argc, char *argv[]) {
         FILE *fd;
          long fdSize;
          uint8_t *buffer;
    
          printf("Enter file path:");
    
         fd = fopen(argv[1], "rb");
         if ( fd == NULL )
        {
            perror("Unable to open the file");
            exit(EXIT_FAILURE);
        }
       fseek(fd, 0L, SEEK_END);
        fdSize = ftell(fd);
     
    /* reset the file position indicator to the beginning of the file */
    fseek(fd, 0L, SEEK_SET);
    
    buffer = (uint8_t*)calloc((size_t) fdSize, sizeof(uint8_t));
    /* memory error */
    if(buffer == NULL)
          return 1;
    
    /* copy all the text into the buffer */
    fread(buffer, sizeof(uint8_t), (size_t) fdSize, fd);
    fclose(fd);
    /* fm for later use*/
    format fm = checkFormat(fd, (const uint8_t *) buffer);
    parseHeader(buffer);
    free(buffer);
    return 0;
    }
    jpeg.h file
    Code:
    #include <stdint.h>
    
    #ifndef FILE_DECODER_JPEG_H
    #define FILE_DECODER_JPEG_H
    
    #endif //FILE_DECODER_JPEG_H
    typedef struct JHeader {
      uint16_t SIO;               
       uint16_t APP0;         
       uint16_t Lenght;         
       uint32_t ID;                      
       uint16_t VERSION;     
       uint16_t Xdensity;          
       uint16_t Ydensity;       
       uint16_t XThumbnail;     
       uint16_t YThumbnail;     
    }JHeader;
    
    JHeader parseHeader(uint8_t *buffer);
    uint32_t slice(int start, int end, uint8_t *buffer);
    jpeg.c file
    Code:
    #include <stdio.h>
    #include <memory.h>
    #include <stdint.h>
    #include <byteswap.h>
    #include "jpeg.h"
    
    JHeader header;
    
    uint32_t slice(int start, int end, uint8_t *buffer){
        uint32_t a;
        unsigned int i = 0;
        uint8_t sl[16];
    
    while(start <= end){
          sl[i] = buffer[start];
          start++;
          i++;
    }
       memcpy(&a, sl, sizeof(unsigned int));
       return a;
    }
    
    JHeader parseHeader(uint8_t *buffer) {
     header.SIO = bswap_16(slice(0,2, buffer));
      header.APP0 = bswap_16(slice(2,3, buffer));
      header.Lenght = bswap_16(slice(4,5, buffer));
      header.ID = bswap_32(slice(6,9, buffer));
      header.VERSION = bswap_16(slice(11,13, buffer));
      header.Xdensity = bswap_16(slice(14,15, buffer));
      header.Ydensity = bswap_16(slice(16,17, buffer));
      header.XThumbnail = buffer[18];
      header.YThumbnail = buffer[19];
      printf("start of image %x\n", header.SIO);
      printf("APP0 marker %x\n", header.APP0);
      printf("Length %x\n", header.Lenght);
      printf("Identifier %x\n", header.ID);
      printf("Version %x\n", header.VERSION);
      printf("X density %x\n", header.Xdensity);
      printf("Y density %x\n", header.Ydensity);
      printf("X thumbnail %x\n", header.XThumbnail);
      printf("Y thumbnail %x\n", header.YThumbnail);
    
      return header;
    }
    Last edited by Salem; 04-23-2019 at 11:46 AM. Reason: Removed crayola

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    You are using magic numbers
    Code:
    switch (buffer[0]){
          case 0x25:
             printf(" PDF \n");
             return PDF;
          case 0x89:
             printf(" PNG \n");
             return PNG;
          case 0x47:
             printf(" GIF \n");
             return GIF;
         case 0xFF:
             printf(" JPEG \n");
             return JPEG;
         default:
             return INVALID;

    Your indentation is off - Each of your line's starting point is inconsistant. I had to look twice to notice that there are two functions there...

    Choose one indentation style and stick to it - Styles - I use Allman, because I do a lot of C# on visual studio. If you want to programme for a job, then you'll need to follow the style that they use.


    You may want to check argc to see if the user has entered the correct thing - If they did not, quickly prompt them


    Don't cast the return value of collac unless you are compiling for C++. See here


    Every use of "fdSize" is cast to size_t - Why not just define it as type size_t?
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by Click_here View Post
    I use Allman, because I do a lot of C# on visual studio.
    I use Allman style too, and some other options for astyle (F2, normal mode keymap, on my VIM):
    Code:
    " C style formater
    map <F2> :%!astyle --mode=c -A1 -s2 -xc -xj -f -p -D -k3 -xd -xW -xV -L -p -xg -P -H -y -xb -xj -xf -w -c -U -S <CR>
    Last edited by flp1969; 04-23-2019 at 07:07 PM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Apr 2019
    Posts
    4
    the indentation is actually i copied directly without fixing it, i use linux style 8 indentation, i really appreciate guys your opinions it really helping me fix my issues that i couldn't see on my own
    Last edited by bensam; 04-24-2019 at 10:17 AM.

  6. #6
    Registered User
    Join Date
    Apr 2019
    Posts
    4
    cause ftell return long that's why fdSize is cast size_t in calloc

  7. #7
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Sorry, I see that now.

    Just check to see if the value is larger than SIZE_MAX (defined in stdint.h) before using it.

  8. #8
    Registered User
    Join Date
    Apr 2019
    Posts
    4
    true i didn't think about it thanks a lot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 11-22-2011, 07:51 AM
  2. JPEG in SDL?
    By Ruski in forum Game Programming
    Replies: 1
    Last Post: 07-25-2005, 01:29 AM
  3. JPEG, GIF etc.
    By playwin in forum Windows Programming
    Replies: 6
    Last Post: 01-19-2005, 12:05 PM
  4. jpeg or gif of buttons
    By samudrala_99 in forum Windows Programming
    Replies: 3
    Last Post: 06-03-2002, 09:10 PM
  5. Jpeg , Jpg
    By Sunny in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 04-10-2002, 12:26 PM

Tags for this Thread