Results 1 to 28 of 28

Thread: JPG picture size...(Adobe) *Solved*

  1. #1

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    JPG picture size...(Adobe) *Solved*

    How can I find out what size a JPG picture has without opening it up in a Image controll or Picture controll. The picture is saved in Adobe Photo Shop 6.X. If I save it in a PSP or something I can do something like this to find the size.

    VB Code:
    1. Public Type ThePicInfo
    2.     Type As String
    3.     Width As Long
    4.     Height As Long
    5. End Type
    6.  
    7. Private Function CheckPicSpecs(TheFile) As ThePicInfo
    8.     Dim TheContent, TheImageInfo As ThePicInfo, TheVar, TheFreeFile
    9.     TheFreeFile = FreeFile
    10.     Open TheFile For Binary As TheFreeFile
    11.     TheContent = Input(10, TheFreeFile)
    12.     Close TheFreeFile
    13.     If Mid(TheContent, 7, 4) = "JFIF" Then
    14.        TheImageInfo.Type = "JPG"
    15.        Open TheFile For Binary As TheFreeFile
    16.        TheContent = Input(167, TheFreeFile)
    17.        Close TheFreeFile
    18.        TheImageInfo.Height = Asc(Mid(TheContent, 165, 1)) + 256 * Asc(Mid(TheContent, 164, 1))
    19.        TheImageInfo.Width = Asc(Mid(TheContent, 167, 1)) + 256 * Asc(Mid(TheContent, 166, 1))
    20.     End If
    21.     If Mid(TheContent, 1, 3) = "GIF" Then
    22.        TheImageInfo.Type = "GIF"
    23.        TheImageInfo.Width = Asc(Mid(TheContent, 7, 1)) + 256 * Asc(Mid(TheContent, 8, 1))
    24.        TheImageInfo.Height = Asc(Mid(TheContent, 9, 1)) + 256 * Asc(Mid(TheContent, 10, 1))
    25.     End If
    26.     CheckPicSpecs = TheImageInfo
    27. End Function

    but that want help with the files saved in Adbobe. Any help?

    The pictures look like this.



    Thanks in advance...
    Last edited by NoteMe; Mar 9th, 2003 at 05:07 PM.

  2. #2

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    anyone?? Any hints?? Or a mail address to the right persons at Adobe???

  3. #3
    PowerPoster
    Join Date
    Aug 2001
    Location
    new jersey
    Posts
    2,904
    as I told you in the other forum where you posted this exact same question, the fact that it was created from or with Adobe is irrelevant. What is relevant is the JPG file format and you can find that at any number of sites on the web that give file formats. There is a pointer to one such at the link below my name.

  4. #4

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    So can you then tell me why the function that I posted in my first post is working if I am saving it in PSP, but is not working if I am saving it in Adobe Photo Shop... .... It gives me something like 5079 in width and 10497 in height no matter what size it is if I am saving it in Adobe, and the right size if I am saving it in PSP....

  5. #5

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Look at this example. They are using the same function to read the size of the picture. But are reading the information from two diffrent files. One is saved in PSP, and the other one in Adobe Photo Shop. What is wrong???
    Attached Files Attached Files

  6. #6
    PowerPoster
    Join Date
    Aug 2001
    Location
    new jersey
    Posts
    2,904
    I'm not sure what your question is but it appears that you think JPG files created by different applications have different structure, but they don't. They might have different resolution and different size, but they have the same file structure, so no matter what application created the JPG file, the PLACE in the JPG file that has the bytes containing the picture size will be the same even if the CONTENTS are different.

    I"m not really sure if I have answered your question because I'm not really sure just what your question is.

  7. #7
    Member
    Join Date
    Feb 2003
    Posts
    37
    A JPEG File does NOT have a set structure that can be retrieved from the same position in the file every time. They have the same information, but it's not always in the same position. If you want, visit www.wotsit.org to look up JPG specs, but I've done that before myself, and here's how you retrieve it.

    -Get ALL data from the file
    -Search for an occurrence of chr$(255)+chr$(192) (refer to as x from here on out)
    -Image height is a flip-flopped word (unsigned integer) in positions x+5 and x+6
    -Image width is a flip-flopped word in positions x+7 and x+8

    I have not written this in VB, but in Powerbasic, and the code I wrote is available here: http://www.powerbasic.com/support/fo...ML/006248.html

    I'm not sure how to mimic the MAKWRD statement in VB, but I'm also not at my development machine, however, making a 2 byte string and using the CVI function may work, or LSETting a TYPEd variable equal to that 2 byte string.

    Hope this helps, If you still have problems with this by next week, I will try to port this over to VB when I get back to my dev machine next weekend.

  8. #8

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    phinds: I am not sure if you saw my example. But if you teste4d it you would see that my function is working on files saved in PSP. So stop telling me that it has nothing to say what app I am saving my JPGs in. But I think that BigDrake here is closer to a solution. He says that the height and width is not stored the same time always. But in PSP they do it anyway, but probably not in Adobe Photo Shop (This is so much easier with BMP.. )...

    OK BigDrake, I have never seen PowerBasic before but I am trying to write your code in VB. But I have some questions...(you should try to comment you code a bit..)...

    Q1: You say that it is stored in an unsigned Integer. But I am not 100% what a unsigned Integer is. How many Bytes is it. Or is it not static?

    Q2: You say that I have to search for chr$(255)+chr$(192), is that two characters right after each other. Or is one of the characters for Width and one for Height?

    Q3: I can see in your code that you are adding the "character" to a byte datatype. (tmp1=ASC(MID$(jpegdata,x+5,1)))...is byte 1 byte big in PowerBasic too?

    Q4: And you are talking about that MAKWRD function. What does it actually do. If you try to explain I can probably find something equal in VB.

    Thanks in advance ØØ.

  9. #9

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Thanks this is working.....YEEEEEEEES....I am going to rewrite the functions in some hours to make it look a little bit better then it does now. And I will post it. Thanks BigDrake...you have been to great help...

  10. #10
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    for future reference...

    www.wotsit.org

  11. #11

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Do not think that I havn't looked at http://www.wotsit.org/ and http://www.filespecs.com/index.jsp and a million other pages. How do you think that I came up with the first function at the first place DiGiTaIErRoR...

  12. #12

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    NOOOOOOOOOO....it is still not working. I only tested it on the PSP JPG picture, and it is working on that one, but not on the ADOBE picture. Here I have made a working sample of it. Any one???
    Attached Files Attached Files

  13. #13
    Member
    Join Date
    Feb 2003
    Posts
    37
    Sorry, I'm VERY bad not to comment code sometimes.

    Q1: It is an number that can't go negative so it increases the range. Tmp1 and Tmp2 are the High and Low Byte, however, I don't remember which one is which.

    Q2: Yes, ASCII 255 right after ASCII 192

    Q3: Yes

    Q4: See code below (will have to expand a bit to include file reading)

    Not sure if tmp1 should be first in the calculation or tmp2, but the MAKWRD function takes 2 8 bit values and creates a 16 bit from it. If you ever have to convert a 4 byte long to a number without VB's nice conversion functions, you can do that as follows:

    Code:
    dim x as long
    
    x=(byte1*256^3)+(byte2*256^2)+(byte3*256)+byte4
    The math can be done as follows (you'll probably have to experiment to make sure tmp1 and tmp2 are in the right places):

    Code:
    Dim tmp1 as BYTE
    dim tmp2 as BYTE
    dim SrchString as String
    dim scrheight as LONG
    dim scrwidth as long
    dim x as long
    
    SrchString=chr$(255)+Chr$(192)
    x=instr(JpegData,SrchString)
    
    tmp1=asc(mid$(JpegData,x+5,1))
    tmp2=asc(mid$(JpegData,x+6,1))
    
    ScrHeight=(tmp1*256)+tmp2
    
    tmp1=asc(mid$(JpegData,x+7,1))
    tmp2=asc(mid$(JpegData,x+8,1))
    
    ScrWidth=(tmp1*256)+tmp2

  14. #14

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Yes I found out all of that if you read my post....but did you see my example app???? I can't understand what is wrong. I think I have translated your example in to VB code, and it is working great for the picture saved in PSP, but not for the one saved in Adobe....I can't understand what is wrong. Any help is appreciated.

    Thanks.
    Last edited by NoteMe; Mar 9th, 2003 at 08:05 AM.

  15. #15
    Member
    Join Date
    Feb 2003
    Posts
    37
    I will not be where I can download and run the app until next weekend. I'll be able to look at it then...

    If you could post some of the code here, I might be able to look at it sometime this week, but I won't be able to download and run anything until next weekend...

  16. #16

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    OK....thanks...I will post the function anyway. It is working great for the pictures saved in PSP.
    VB Code:
    1. Dim lHeightWidth As ThePicInfo
    2.  
    3. Private Type ThePicInfo
    4.     Height As String
    5.     Width As Long
    6. End Type
    7.  
    8. ------------
    9.  
    10. Private Function CheckPicSpecs(TheFile) As ThePicInfo
    11.    
    12.     Dim TheFreeFile As Integer
    13.     Dim sBuffer  As String * 1
    14.     Dim sFileContents As String
    15.     Dim XPos As Long
    16.     Dim i As Long
    17.     Dim bTempByte1 As Byte
    18.     Dim bTempByte2 As Byte
    19.     Dim Answer As ThePicInfo
    20.  
    21.     TheFreeFile = FreeFile
    22.    
    23.     Open TheFile For Binary As TheFreeFile
    24.         For i = 1 To LOF(TheFreeFile)
    25.             Get #TheFreeFile, , sBuffer
    26.             sFileContents = sFileContents & sBuffer
    27.         Next i
    28.     Close TheFreeFile
    29.    
    30.     XPos = InStr(1, sFileContents, Chr$(255) + Chr$(192))
    31.    
    32.     bTempByte1 = Asc(Mid$(sFileContents, XPos + 5, 1))
    33.     bTempByte2 = Asc(Mid$(sFileContents, XPos + 6, 1))
    34.     Answer.Height = ((bTempByte1 * 256) + bTempByte2)
    35.    
    36.     bTempByte1 = Asc(Mid$(sFileContents, XPos + 7, 1))
    37.     bTempByte2 = Asc(Mid$(sFileContents, XPos + 8, 1))
    38.     Answer.Width = ((bTempByte1 * 256) + bTempByte2)
    39.    
    40.     CheckPicSpecs = Answer
    41.    
    42. End Function
    Last edited by NoteMe; Mar 9th, 2003 at 08:32 AM.

  17. #17
    Member
    Join Date
    Feb 2003
    Posts
    37
    Does XPos actually find anything in an Adobe Photoshop file? That's actually all I've ever tested on.

    A recommendation...When reading the file, instead of a for...next loop to read the entire file, you can do it like this, and it might be a bit faster:

    Code:
    Open JpegFile$ for binary access read shared as #1 len=16384
    jpegdata$=space$(lof(1))
    get #1,1,jpegdata$
    close #1
    the len part of the binary open statement specifies a buffer.

    I did notice that inside your type variable you only have a .Height member, and not a .Width, and you're using 'type' as a member name. Don't know if that's allowed, but I try to steer clear of using VB keywords as member names or variable names.

  18. #18

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    I did notice that inside your type variable you only have a .Height member, and not a .Width, and you're using 'type' as a member name. Don't know if that's allowed, but I try to steer clear of using VB keywords as member names or variable names.
    Sorry, that was only a miss spelling. It was used to check if it was a GIF or a JPG in the old function. But since I am only going to use JPG, I was going to delete the type variable when I posted it here, but deleted wrong line......


    Does XPos actually find anything in an Adobe Photoshop file? That's actually all I've ever tested on.
    Yes it finds something down at byte 1255....



    A recommendation...When reading the file, instead of a for...next loop to read the entire file, you can do it like this, and it might be a bit faster:
    Thanks, but I hate using shortcuts like $ in my code. It makes it harder to read. And BTW why are you writing len=16384. The file is nopt this size all the time. Some times bigger and some times smaller. Has that something to say?
    And I got an error when I tried your read function. "Excpected Array" on the Space$ variable...

  19. #19
    Member
    Join Date
    Feb 2003
    Posts
    37
    the len=16384 when opened in binary only specifies a buffer for the file reading. Basically the computer only does a physical read every 16384 bytes (16k), you can use 8192, or whatever you'd like. With larger files it tends to speed up the reading. In VB6 at least, the SPACE$() function returns the number of spaces specified. It is a VB function. It might be just Space() in other versions, not sure.

    The $ was only used to be sure you knew what variable type I was using...

    I'm about to head out on a 10 hour drive, and more than likely will not have internet where I am, so I may not be much help over the next few days. If you don't resolve the issue by next weekend, I'll try my best to help out, may get you to email me the file you have or something like that.

  20. #20

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    I am not sure what was wrong, but I commented out Option Explicit and your function worked........But speed is not a problem with my app at all. It is making more then 100 html pages in 2/10 of a sec.

    I will try to figgure it out before you are comming back. But it would be great if you could take a look at it if I don't before the week is over. Thanks.

    Anyone else has a comment here now?

  21. #21

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    I know have one more clue here. If I save the picture in Adobe using the Save As function. My VB function is not working. But if I am saving it using the "Save for web" function, my VB function are working. I am getting so confussed here...

  22. #22

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    I got this answer from an Adobe person...

    Just checking between one image saved in both ways, the dimensional data was at Hex 202 and 204 in the Save As and at Hex C4 and C6 in the S4Web image

    Is it possible for anyone to trancelate this to the two bytes that I have to read.

    C4 = ??

    C6 = ??

    I am not strong in Hex numbers.

    Thanks.

  23. #23

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    I found out that the calculator could do it. So thanks anyway....

  24. #24

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    I think that this might be where I have failed with your code BigDrake. But I ain't good with Perl and the link wan't work. So I am still looking for a solution.

    The header of a JPEG file consists of a series of blocks, called "markers".
    The image height and width are stored in a marker of type SOFn (Start Of
    Frame, type N). To find the SOFn you must skip over the preceding markers;
    you don't have to know what's in the other types of markers, just use their
    length words to skip over them. The minimum logic needed is perhaps a page
    of C code. (Some people have recommended just searching for the byte pair
    representing SOFn, without paying attention to the marker block structure.
    This is unsafe because a prior marker might contain the SOFn pattern, either
    by chance or because it contains a JPEG-compressed thumbnail image. If you
    don't follow the marker structure you will retrieve the thumbnail's size
    instead of the main image size.) A profusely commented example in C can be
    found in rdjpgcom.c in the IJG distribution (see part 2, item 15). Perl
    code can be found in wwwis, from http://www.tardis.ed.ac.uk/~ark/wwwis/.

  25. #25

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Hurra!!!!!!!!!!!!!!!!! YEEEEEEEEEEEES I made it work. This is the function. It is a lot more addvanced. And can be used with a lot more of the JPG files out there. And don't have pit falls like the two other functions....

    VB Code:
    1. Private Type BITMAPINFO
    2.     Width             As Long
    3.     Height            As Long
    4. End Type
    5.  
    6.  
    7.   Private Function GetJPGInfo(ByVal FileName As String) As BITMAPINFO
    8.     Dim bChar As Byte
    9.     Dim a As Byte, b As Byte
    10.     Dim c As Byte, D As Byte
    11.     Dim E As Byte, f As Byte
    12.     Dim i As Integer
    13.     Dim DotPos As Integer
    14.     Dim Header As String
    15.     Dim blExit As Boolean
    16.     Dim MarkerLen As Long
    17.     Dim ImgWidth As Integer
    18.     Dim ImgHeight As Integer
    19.     Dim ImgSize As String
    20.     Dim fnum As Integer
    21.     Dim ImageInfo As BITMAPINFO
    22.     On Error Resume Next
    23.    
    24.     fnum = FreeFile
    25.     Open FileName For Binary As #fnum
    26.  
    27.     ImgSize = LOF(fnum) / 1024
    28.     DotPos = InStr(ImgSize, ",")
    29.     ImgSize = Left(ImgSize, DotPos)
    30.  
    31.     Get #fnum, , bChar
    32.     Header = Hex$(bChar)
    33.     Get #fnum, , bChar
    34.     Header = Header & Hex$(bChar)
    35.     If Header <> "FFD8" Then Exit Function
    36.  
    37.     While Not blExit
    38.         Do Until Hex$(bChar) = "FF"
    39.            Get #fnum, , bChar
    40.         Loop
    41.  
    42.         Get #fnum, , bChar
    43.         If Hex$(bChar) >= "C0" And _
    44.              Hex$(bChar) <= "C3" Then
    45.            Get #fnum, , bChar
    46.            Get #fnum, , bChar
    47.            Get #fnum, , bChar
    48.  
    49.            Get #fnum, , bChar
    50.            a = bChar
    51.            Get #fnum, , bChar
    52.            b = bChar
    53.            Get #fnum, , bChar
    54.            c = bChar
    55.            Get #fnum, , bChar
    56.            D = bChar
    57.  
    58.            ImgHeight = CInt(a * 256 + b)
    59.            ImgWidth = CInt(c * 256 + D)
    60.            blExit = True
    61.         Else
    62.            If Hex$(bChar) = "DA" Then
    63.               blExit = True
    64.            Else
    65.               Get #fnum, , bChar
    66.               E = bChar
    67.               Get #fnum, , bChar
    68.               f = bChar
    69.               MarkerLen = (E * 256 + f) - 2
    70.    
    71.               Dim marker As String
    72.               marker = String(MarkerLen, vbNullChar)
    73.               Get #fnum, , marker
    74.            End If
    75.         End If
    76.     Wend
    77.  
    78.     Close #fnum
    79.  
    80.     With ImageInfo
    81.          .Width = ImgWidth
    82.          .Height = ImgHeight
    83.     End With
    84.    
    85.     GetJPGInfo = ImageInfo
    86.    
    87. End Function

  26. #26
    Hyperactive Member
    Join Date
    May 2002
    Location
    Campbell, OH USA
    Posts
    282

    Re: JPG picture size...(Adobe) *Solved*

    can anyone translate that to ASP???
    Which X do you like better???
    Code:
    x     x       \      /
     x   x         \    /
      x x           \  /
       x             ><
      x x           /  \
     x   x         /    \
    x     x       /      \

  27. #27

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: JPG picture size...(Adobe) *Solved*

    Just as a note if anyone does it. You need to remember to close the file if this line valid to true:

    If Header <> "FFD8" Then Exit Function


    if not you might get problems. I fixed that later on in my code but didn't think about updating this thread..

  28. #28
    New Member
    Join Date
    Apr 2007
    Posts
    2

    Re: JPG picture size...(Adobe) *Solved*

    Quote Originally Posted by NoteMe
    Just as a note if anyone does it. You need to remember to close the file if this line valid to true:

    If Header <> "FFD8" Then Exit Function


    if not you might get problems. I fixed that later on in my code but didn't think about updating this thread..
    Hi NoteMe

    I´m having this kind of problem, I think....
    My header come with Header = Header & Hex$(bChar) as 424D, not FFD8.

    My pictures size are most from 100K, 200K.....

    Now after several hours I found your solution to close the JPG but I need to use it in the image(n).picture.

    Could you help to show how to close the file, or how yo solved your problem?

    Thanks a lot..
    Ivan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width