scan an image and print it

This is a discussion on scan an image and print it within the C Programming forums, part of the General Programming Boards category; Is there a way in the c language, to read/get an image file such as a jpeg or .bmp and ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    10

    scan an image and print it

    Is there a way in the c language, to read/get an image file such as a jpeg or .bmp and print it using some print command. Currently I get around this by converting a bmp file to a bunch of coordinates, (each set of coordinates representing a single pixel), I read these coordinates from a file and print it pixel by pixel using a while loop and using the following function:

    Code:
    print_dot(rows,cols)
    int rows,cols;
    {
    printf("\033*p%dY\0",rows);
    printf("\033*p%dX\0",cols);
    printf("\033*c1A\0");
    printf("\033*c1B\0");
    printf("\033*c0P\0");
    }
    this takes a while with printers that have little memory. Is there some way to print it all at once?
    Last edited by hephaest; 09-07-2006 at 11:57 AM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Possible? Oh heck yeah!

    Just never done it before. My question is "Why not use the program that came with the scanner to scan and print the pic?"

    But, onward and hopefully, upward. I'd start with optimizing your printf code - it's massively inefficient, calling the printf function anew for every pixel you want to print.

    You've seriously offended the German blood in me!

    Why not build up an entire row into a char array, and then just call printf ONCE, per row? Maybe build up many rows at a time in a 2d array of char's, and print them all at once?

    Adak

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    10
    Quote Originally Posted by Adak
    Possible? Oh heck yeah!

    Just never done it before. My question is "Why not use the program that came with the scanner to scan and print the pic?"
    what im trying to accomplish is to print out a header that includes a company logo. The logo is a graphic that i can store as a bmp or gif or jpeg. Currently I have a c program that generates a letter and the user of the program has to feed the letterhead to print the letter on it. Im trying to eliminate it so that both the letter head (with the logo) and the letter itself, print on a blank sheet of paper.
    Quote Originally Posted by Adak
    But, onward and hopefully, upward. I'd start with optimizing your printf code - it's massively inefficient, calling the printf function anew for every pixel you want to print.

    You've seriously offended the German blood in me!

    Why not build up an entire row into a char array, and then just call printf ONCE, per row? Maybe build up many rows at a time in a 2d array of char's, and print them all at once?

    Adak

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    > this takes a while with printers that have little memory. Is there some way to print it all at once?
    Using escape codes to drive printers is pretty old, plus it's also rather specific to each single make of printer.
    You could read through your printer manual again to see what kind of bitmap functionality it has, say for example using custom fonts to draw patches of the image, then redefine the font for the next patch.

    Most printers should understand postscript, which is a language designed for page layout.
    http://en.wikipedia.org/wiki/PostScript
    Try sending a simple postscript command to your printer to see what it does.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    Well if you have access to the Win32 API from your code (console?) this is quite simple. In Windows you paint everything to a DC and if your paint() function requires a DC you simply change the DC to a PrinterDC. You are essentially drawing into a buffer which is then sent to the printer which prints it out. Inside of Windows, printing and drawing are nearly identical in nature because they operate on DC's.

    If you don't have access to Win32 API then any solution you come up with is going to be printer specific and I'm not even sure modern printers still support escape codes like that. Nearly all of them rely on 32-bit drivers loaded into Windows.

    I wish people would get with the times.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    10
    Quote Originally Posted by Bubba
    Well if you have access to the Win32 API from your code (console?) this is quite simple. In Windows you paint everything to a DC and if your paint() function requires a DC you simply change the DC to a PrinterDC. You are essentially drawing into a buffer which is then sent to the printer which prints it out. Inside of Windows, printing and drawing are nearly identical in nature because they operate on DC's.

    If you don't have access to Win32 API then any solution you come up with is going to be printer specific and I'm not even sure modern printers still support escape codes like that. Nearly all of them rely on 32-bit drivers loaded into Windows.

    I wish people would get with the times.
    Im doing this on hpux.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
    print_dot(rows,cols)
    int rows,cols;
    Can't you use the C89 style of function declarations?

    Look up ANSI escape sequences if you want to keep using them, I'm sure there's one that would work.

    Does hpux have a "print" command? system("print file");
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    10
    Quote Originally Posted by dwks
    Code:
    print_dot(rows,cols)
    int rows,cols;
    Can't you use the C89 style of function declarations?

    Look up ANSI escape sequences if you want to keep using them, I'm sure there's one that would work.

    Does hpux have a "print" command? system("print file");
    the file is a bmp, i dont think you can just print it via a print command in unix. I think you would need a program that can interpret the jpg or gif or bmp and convert it into printer code or pcl or postscript. But that would defeat my purpose. I want to be able to print straight from the c program.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    10
    Quote Originally Posted by dwks
    Code:
    print_dot(rows,cols)
    int rows,cols;
    Can't you use the C89 style of function declarations?

    Look up ANSI escape sequences if you want to keep using them, I'm sure there's one that would work.

    Does hpux have a "print" command? system("print file");
    Oh i see what you mean, call the system print command from within the c program. I would need to find a unix program that converts jpeg to pcl or to ps though. I still would like to find a C command

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Well, whatever your C program does is what the print command would do, except less efficiently. I imagine the print command would be highly optimised.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    10
    im trying to find a c function to get and print a bmp or jpeg inmage in unix.
    I dont want to do it by pixel or to convert the bmp file into a pixel coordinate file as i have done above

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    bmp2ps perhaps?
    http://clusty.com/search?query=bmp2p...Mozilla-search

    Or you could carry on arguing over the colour of the beads on your abacus
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    10
    Quote Originally Posted by Salem
    bmp2ps perhaps?
    http://clusty.com/search?query=bmp2p...Mozilla-search

    Or you could carry on arguing over the colour of the beads on your abacus
    But im not looking for a separate program. Im looking for a C function that i can call inside a C program
    like printf()
    except it would be something like print_image() ?

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    10
    Quote Originally Posted by Salem
    bmp2ps perhaps?
    http://clusty.com/search?query=bmp2p...Mozilla-search

    Or you could carry on arguing over the colour of the beads on your abacus
    BTW I tried to convert jpg to post script, I sent it to my hplaserjet 4 plus
    printer, i got garbage.

  15. #15
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    Since postscript files are just text files, you can
    a) look at them with a text editor (check that they begin with %!PS IIRC)
    b) read said text file into your own program, so you can merge your data with the already encoded bitmap data.
    c) squirt the whole lot out to the printer.

    Reading image.ps is no different to reading image.bmp, it's still an external file.
    If you want to hide the conversion inside your program, then that's doable as well.

    No doubt you could get the source code for bmp2ps and just incorporate that directly into your code.

    I've no idea what you did, but this seems to indicate that your printer might not understand postscript.
    http://en.wikipedia.org/wiki/HP_LaserJet_4
    Any particular kind of garbage - like for instance the same as what you see when you view the poststript file or was it a different kind of garbage.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers trouble
    By saahmed in forum C Programming
    Replies: 39
    Last Post: 03-24-2006, 03:08 AM
  2. a 2-D array image (Prompt)
    By amirahasanen1 in forum C++ Programming
    Replies: 6
    Last Post: 09-26-2005, 03:59 PM
  3. HELP !! .. print a "box" problem
    By imbecile in C in forum C Programming
    Replies: 6
    Last Post: 07-26-2003, 01:54 PM
  4. Image Thinning
    By Colin in forum C++ Programming
    Replies: 3
    Last Post: 02-10-2003, 09:38 AM
  5. Still can't print a Histogram from fail input
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 07-11-2002, 12:24 PM

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