Like Tree2Likes
  • 1 Post By laserlight
  • 1 Post By christop

Write .ucf image into .ppm format

This is a discussion on Write .ucf image into .ppm format within the C Programming forums, part of the General Programming Boards category; Hi there! I'm dealing with a very interesting problem today. I'm given an image in .ucf format (Universal Communication Format), ...

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    78

    Write .ucf image into .ppm format

    Hi there!

    I'm dealing with a very interesting problem today.
    I'm given an image in .ucf format (Universal Communication Format), which is read by the following code snippet:
    Code:
    /*********************************************************/
    /* UCFReadPic.c                                          */
    /*********************************************************/
    
    PIC UCFReadPic(infile)
    FILE *infile;
    {
       PIC temp;
       unsigned char byte1,byte2,byte3,byte4;
    
       /* getting Type from image data */
    
       fread(&byte1,sizeof(BYTE),1,infile);
       fread(&byte2,sizeof(BYTE),1,infile);
       fread(&byte3,sizeof(BYTE),1,infile);
       fread(&byte4,sizeof(BYTE),1,infile);
       temp.type= byte1*16777216+byte2*65536+byte3*256+byte4;
       switch (temp.type)
       {
          case 0xF10F:
          case 0xF200:
          case 0xF201:
          case 0xF204:
          case 0x0000:
          {  
    
            fread(&byte1,sizeof(BYTE),1,infile);
            fread(&byte2,sizeof(BYTE),1,infile);
            fread(&byte3,sizeof(BYTE),1,infile);
            fread(&byte4,sizeof(BYTE),1,infile);
            temp.maxX= byte1*16777216+byte2*65536+byte3*256+byte4;
            fread(&byte1,sizeof(BYTE),1,infile);
            fread(&byte2,sizeof(BYTE),1,infile);
            fread(&byte3,sizeof(BYTE),1,infile);
            fread(&byte4,sizeof(BYTE),1,infile);
            temp.maxY= byte1*16777216+byte2*65536+byte3*256+byte4;
            break;
          }
          case 0x8000:
          case 0x8001:
          case 0xB003:
          default  :
          {
            printf("****** Unknown image type *****\n");
            exit(1);
          }
       }
    
       if((temp.image=(BYTE*)calloc(temp.maxX*temp.maxY, sizeof(BYTE)))==NULL)
       {
          printf("***** NOT enough memory *****\n");
          exit(1);
       }
    
       fread(temp.image,sizeof(BYTE),temp.maxX * temp.maxY,infile);
       return(temp);
    }
    Unfortunately though, I am unable to open the .ucf format in any editor (Photoshop, GIMP, etc). Thus, I would like to build in a function, which writes this image into .ppm format (or any other format, which is readable by an editor).

    Any ideas of how to proceed?

    Thanks!
    Last edited by in_ship; 08-19-2012 at 08:16 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,267
    Hmm... I notice rather outdated syntax:
    Code:
    PIC UCFReadPic(infile)
    FILE *infile;
    {
    I suggest that you change the above code to:
    Code:
    PIC UCFReadPic(FILE *infile)
    {
    Anyway, you need to find out what is this PIC type and how to write it "into .ppm format (or any other format, which is readable by an editor)". Once you know that, it is a matter of calling this function then doing that with the return value.
    in_ship likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2012
    Posts
    78
    Quote Originally Posted by laserlight View Post
    Hmm... I notice rather outdated syntax:
    Code:
    PIC UCFReadPic(infile)
    FILE *infile;
    {
    I suggest that you change the above code to:
    Code:
    PIC UCFReadPic(FILE *infile)
    {
    Thanks for the remark - I just modified it accordingly!

    I still have no idea what ucf is - I opened the image using 'vi' and the output says nothing to me at all.

    ^@^@^@^@^@^@^@<80>^@^@^@<80> ;d~<92> Ƽ<91>}d9 3`{<8f> ĺ..<9f><8f>z_ [w<8c><9d><9c><8b>vY Sq<87><99> Ⱦ<98><86>pQ Ik<82><94> Ż<93><80>hD ;c|<90> ..¸<9e><8e>z` Zv<8b><9b> ȿ<9a><89>sU Nn<85><97> Ƽ<95><83>kG >f^?<92>ĺ<90>| b \x< 8d><9e>..·<9d><8b>uW Oq<88><9a> <99><86>mG >h<82><96> ɿ<94><80>d ^|<92>Ƚ< 90>yZ Su<8d> Ǽ<9f><8b>sM Dn<89><9d> ƻ..<9c><87>k f<84><9a> ƹ<99><82>c ]^?<97>Ÿ<95>}Z Ty<93> ŷ<92>xP Ht<90>Ŷ<8f> sD <8d>ĵ<8d>o k<8b> ĵ<8a>j g<89>Ĵ<88>g d<87> ó <87>d c<86><9f>ó<9f><86>c c<8 6><9f>ó<9f><86>c d<87><9f> ó<9f><86>d f<88> ó <87>f j<8a>ô<89>i 6n<8c> ô<8b>m3 Fs<8f> <8e>rD Rx<92>õ<91>wP \}<95> ö<94>|Z f<83><98> ÷<97><81>c Gn<88><9c>ø<9a ><86>kC Vv<8d><9f>..ø<9d><8a>sR b}<9 1>ù..<9f><8f>z^ Jl<83><95> ú<93><80>hE Yt<89><99> ú<96><86>qU Ce|<8e><9c>û<9a ><8b>xa= To<82><92><9f>»<9d><8f>^? kO @aw<8 8><96>» <93><85>s]: Sk~<8d> <9a>¼<97><8a>zgM B`t<84><91><9d> ¼<9a><8f><81>p[< Sj{<89><9 5> ½<9e><93><86>xeN E_r<81><8e><99> ½ <97><8b>~n[? 3Uiz<87><92> <9c>..¾<9a><90><84>veP, I`r<80><8c><96> <9f>þ..<9d><94><89>}n\D <Wjy<85><90><9a> ÿ <97><8e><82>ufR6 $ Nbr^?<8a><94><9d>ÿ<9b ><92><88>|n^H^] CZky<85><8f><98> <9e><96><8c><82>vgU= 6Rds^?<8a><93><9b> <99><91><87>|o`M/ H]ly<85><8e><97><9f>¾ <9d><95><8c><82>viXC > Uft^?<8a><93><9b>ÿ.. <99><90><87>|pbQ8 0M_nz< 85><8e><97><9e>< 9c><94><8c><82>wk[H* DXhu<80><8a><93><9a> ..¿ <98><90><87>}rdT? 9Qbp{<85 ><8e><97><9e>< 9c><95><8c><83>xl^L3 (H[jv<81><8a><93><9a>.. <99><91><88>~sgWD" >Tdq|<86>< 8f><97><9e>< 9c><95><8d><84>zn`P9 1L]kw<82><8b><93><9b>
    Last edited by in_ship; 08-19-2012 at 08:25 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,267
    Quote Originally Posted by in_ship
    I still have no idea what ucf is
    You could always ask the person who gave you this file, and it may indeed be useful to know what it is. However, my point is that if the given function works as advertised, then you don't need to worry about what is the file format; you need to worry about what is the PIC type and how to use it.

    Quote Originally Posted by in_ship
    I opened the image using 'vi' and the output says nothing to me at all.
    Binary file format. You can get a sense that that is so just by looking at the implementation of UCFReadPic.
    Last edited by laserlight; 08-19-2012 at 08:57 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    .ucf reader by epson
    All Programmable Technologies from Xilinx Inc.

    Xilinx: Downloads

    epson salts again with 6.2 gb prog ! just to view a pic. meow !
    <whiteflags> You foolishly believe in people and this feeling must be crushed.

    i have gotta get me a bunch of monsantos gmos == genetically modified orgasms .

  6. #6
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    383
    I would start by looking up the ppm file format, if you're not already familiar with it. Then I would see that the PIC image structure has everything needed to write a ppm image: maxX, maxY, and image. I would write all of those pieces of information to the ppm file and then see if the image looks correct when it's opened in an image viewer.

    By the way, the ucf format appears to have a header consisting of type, width, and height, each a four-byte big-endian value. The header is then followed by the image data in an 8-bit pixel format. I'm not familiar with the ucf format, so I can't say whether those 8 bits represent grayscale, indexed, or "true color" data. Grayscale would be the easiest to write to a ppm file. Just write it in the pgm format, which is almost the same as ppm, except it takes grayscale pixel data instead of color pixel data.

  7. #7
    Registered User
    Join Date
    Aug 2012
    Posts
    78
    Quote Originally Posted by christop View Post
    I would start by looking up the ppm file format, if you're not already familiar with it. Then I would see that the PIC image structure has everything needed to write a ppm image: maxX, maxY, and image. I would write all of those pieces of information to the ppm file and then see if the image looks correct when it's opened in an image viewer.
    I this these three values can indeed be read. The code snippet above contains the following lines at the end:
    Code:
    fread(temp.image,sizeof(BYTE),temp.maxX * temp.maxY,infile);
    return(temp);
    I have been reading about the PPM format.
    So, ideally, this is the snippet I would have to add to the preexisting lines of code:

    Code:
    fread(temp.image,sizeof(BYTE),temp.maxX * temp.maxY,infile);
    
    -------- new code --------
    // open new image file
    FILE *fp = fopen("ppmimage.ppm", "wb");
    
    if (!fp) {
       fprintf(stderr, "Unable to open file '%s'\n", filename);
       exit(1);
    }
    
    // write the header for pgm image format
    fprintf(fp, "P6\n");
    
    
    //image size
    fprintf(fp, "%d %d\n",temp.maxX, temp.maxY);
    
    // rgb component depth
    fprintf(fp, "%d\n", 255);
    
    // write image
    fwrite(temp.image, sizeof(BYTE), temp.maxX *temp.maxY, fp);
    
    fclose(fp);
     
    -------- eoc --------
    
    
    return(temp);
    Last edited by in_ship; 08-19-2012 at 02:15 PM.

  8. #8
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    383
    The ppm format expects three bytes for each pixel (width * height * 3 bytes for a complete image), but you're writing only one byte per pixel (width * height bytes). As I mentioned in my last post, you could write it as a pgm file instead of ppm. Simply use P5 instead of P6 in the header.

    I also wouldn't add code to the existing function (a function should do only one thing and do it well). It already returns a pointer to a PIC structure, so you can just as easily write a ppm file after calling UCFReadPIC.

  9. #9
    Registered User
    Join Date
    Aug 2012
    Posts
    78
    Quote Originally Posted by christop View Post
    The ppm format expects three bytes for each pixel (width * height * 3 bytes for a complete image), but you're writing only one byte per pixel (width * height bytes). As I mentioned in my last post, you could write it as a pgm file instead of ppm. Simply use P5 instead of P6 in the header.

    I also wouldn't add code to the existing function (a function should do only one thing and do it well). It already returns a pointer to a PIC structure, so you can just as easily write a ppm file after calling UCFReadPIC.
    I do have a pgm version of the file already. I would like to see whether pgm and ppm match or not.

    So, after heeding your advice; here goes my implementation

    Code:
    invocation from main.c
    PIC pic1 = UCFReadPic(infile);
    PPMWritePic(pic1);    
    
    PPMWritePic.c
    void PPMWritePic(PIC temp)
    
    {
        
        // open new image file
        FILE *fp = fopen("ppmimage.ppm", "wb");
     
        if (!fp)
        {
               fprintf(stderr, "Unable to open file '%s'\n", filename);
               exit(1);
        }
     
        // write the header for ppm image format
        fprintf(fp, "P6\n");
      
        // image size
        fprintf(fp, "%d %d\n",temp.maxX, temp.maxY);
     
        // rgb component depth
        fprintf(fp, "%d\n", 255);
     
        // write image
        fwrite(temp.image, sizeof(BYTE), 3*temp.maxX*temp.maxY, fp);
     
        fclose(fp);
    
    }
    The resulting image contains 3x the original image (back-to-back) and the lower portion of it is just entirely black.

    Here's the vim output:
    vim ppmimage.ppm

    P6
    128 128
    255
    @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
    "ppmimage.ppm" [noeol][converted] 7L, 68617C
    Any ideas of what could have gone wrong here?

  10. #10
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    383
    Quote Originally Posted by in_ship View Post
    Code:
        // write image
        fwrite(temp.image, sizeof(BYTE), 3*temp.maxX*temp.maxY, fp);
    Yes. The memory at temp.image has only maxX*maxY bytes allocated to it, but you're writing 3 times that much to the ppm file. When I said that ppm takes 3 bytes per pixel, I meant that you would have to convert each byte in the original image to 3 bytes, whether it's by repeating a byte 3 times (easy), using a byte as an index into a colormap (which you would have to build beforehand), or splitting up the byte into 3 separate components (you would have to know how many bits each component has). It depends on what pixel format the UCF image uses, which you will need to find out. Start by repeating each byte 3 times (not by writing the complete image 3 times) because it's the easiest, and probably the most likely format used by UCF.
    in_ship likes this.

  11. #11
    Registered User
    Join Date
    Aug 2012
    Posts
    78
    Thanks for your help, christop!
    The issue has been resolved now!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to write image data to binary PGM file format(P5)?
    By tommy_chai in forum C Programming
    Replies: 6
    Last Post: 11-03-2007, 11:52 PM
  2. What image format to use?
    By Kennedy in forum Tech Board
    Replies: 21
    Last Post: 10-16-2006, 09:10 AM
  3. Convert Pdf to Image format!
    By Dilmerv in forum C# Programming
    Replies: 4
    Last Post: 05-22-2006, 11:31 PM
  4. Reading text in an Image format
    By loko in forum C Programming
    Replies: 0
    Last Post: 09-29-2005, 07:38 PM
  5. image format
    By linuxdude in forum Game Programming
    Replies: 5
    Last Post: 08-09-2004, 10:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21