Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sending image data to printer directly

Status
Not open for further replies.

fz315

Programmer
Oct 29, 2004
6
CA
Hi,everyone:
This slip of code print out a image(bmp/jpg format) by
sending image data to printer directly. I can't understand well about "send surface" section, could someone give me some clue?
Thanks and Merry Christmas!
------------------
void PrintSurface (int hDevice, SDL_Surface *surface, char justify)
{
Uint8 byte;
int bit;
SDL_Color pixel;
int i, pc;
int r, c;

**==================================================
** Start bit image printing.
**==================================================
*/
write (hDevice, "\x1B\x2A\x62", 3);

byte = surface->h % 256;
write (hDevice, &byte, sizeof (byte));
byte = surface->h / 256;
write (hDevice, &byte, sizeof (byte));

for (r = 0; r < surface->h; r++)
{
byte = 0x00;
bit = 7;
pc = 0;


/*
**==================================================
** Justify line.
**==================================================
*/
if (surface->w < DotsPerLine[Model])
{
switch (toupper (justify))
{
case 'C':
pc = (DotsPerLine[Model] - surface->w) / 2;
break;

case 'R':
pc = DotsPerLine[Model] - surface->w;
break;

default:
pc = 0;
break;
}

byte = 0x00;
for (i = 0; i < (pc / 8); i++)
{
write (hDevice, &byte, sizeof (byte));
}

bit = 7 - (pc % 8);
}


/*
**==================================================
** Send surface.
**==================================================
*/
for (c = 0; (pc < DotsPerLine[Model]) && (c < surface->w); c++, pc++)
{
pixel = GetPixel (surface, c, r);
if ((pixel.r < 128) && (pixel.g < 128) && (pixel.b < 128))
{
byte |= (0x01 << bit);
}
bit--;
if (bit < 0)
{
write (hDevice, &byte, sizeof (byte));
bit = 7;
byte = 0x00;
}
}


/*
**==================================================
** Finish out line.
**==================================================
*/
if (bit != 7)
{
write (hDevice, &byte, sizeof (byte));
pc += bit + 1;
}

byte = 0x00;
for (bit = (DotsPerLine[Model] - pc) / 8; bit > 0; bit--)
{
write (hDevice, &byte, sizeof (byte));
}
}
}


 
I assume this is a monochrome printer. It doesn't make sense otherwise.

First the bit pattern - this is a bit strange as it is reversed every 8 bits. If X represents a set pixel and O represents an unset pixel, the pattern XXOOXOXO will produce 0x53.

The if statement checks whether any of the colours have an intensity greater than 128. If they do, the pixel is set, if they don't the pixel is not set.

You can see roughly what it is going to do on the screen by changing the for loop to
Code:
      for (c = 0; (pc < DotsPerLine[Model]) && (c < surface->w); c++, pc++)
      {
         pixel = GetPixel (surface, c, r);
         if ((pixel.r < 128) && (pixel.g < 128) && (pixel.b < 128))
         {
            printf ("O");
         }
         else
         {
            printf (" ");
         }
       }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top