📄 imgs.cpp
字号:
inT32 ystart, inT32 xext, //extent to copy inT32 yext, IMAGE *dest, //destination image inT32 xdest, //destination coords inT32 ydest, BOOL8 preserve_grey //shift to new bpp ) { IMAGELINE copyline; //copy of line uinT8 *copy; //source pointer inT8 shift4; //shift factor inT8 shift6; //shift factor inT8 colour_shift; //shift of colours uinT8 white_level; //dest white value inT32 pixel; //pixel index inT32 y; //line index inT32 yoffset; //current adjusted offset inT8 srcppb; //pixels per byte if (xstart < 0 || ystart < 0 || xdest < 0 || ydest < 0) return; if (xext <= 0) xext = source->get_xsize (); //default to all if (xext > source->get_xsize () - xstart) //clip to smallest xext = source->get_xsize () - xstart; if (xext > dest->get_xsize () - xdest) xext = dest->get_xsize () - xdest; if (yext <= 0) yext = source->get_ysize (); //default to all if (yext > source->get_ysize () - ystart) //clip to smallest yext = source->get_ysize () - ystart; if (yext > dest->get_ysize () - ydest) yext = dest->get_ysize () - ydest; if (xext <= 0 || yext <= 0) return; //nothing to do //pixels per byte srcppb = 8 / source->get_bpp (); shift4 = 4 - dest->get_bpp (); //for different bpps shift6 = 6 - dest->get_bpp (); //for grey preserve colour_shift = 8 - dest->get_bpp (); white_level = dest->get_white_level (); for (y = 0; y < yext; y++) { if (ystart >= ydest) yoffset = y; //top down else yoffset = yext - y - 1; //bottom up source->check_legal_access (xstart, ystart + yoffset, xext); dest->check_legal_access (xdest, ydest + yoffset, xext); source->get_line (xstart, ystart + yoffset, xext, ©line, 0); for (pixel = 0, copy = copyline.pixels; pixel < xext; pixel++) { if (*copy < FIXED_COLOURS && preserve_grey) *copy = grey_scales[*copy] >> colour_shift; else if (*copy < FIXED_COLOURS) { if (*copy == BLACK_PIX) *copy = white_level; //black->white else *copy = 0; //others->black } else if (*copy >= MIN_4BIT && *copy < MAX_4BIT) { if (shift4 < 0) *copy = (*copy - MIN_4BIT) << (-shift4); else *copy = (*copy - MIN_4BIT) >> shift4; } else if (*copy >= MIN_6BIT && *copy < MAX_6BIT) { if (shift6 < 0) *copy = (*copy - MIN_6BIT) << (-shift6); else *copy = (*copy - MIN_6BIT) >> shift6; } else { *copy = white_level; //white the rest } copy++; } dest->put_line (xdest, ydest + yoffset, xext, ©line, 0); }}/********************************************************************** * fast_get_line * * Get a line of image into the supplied image line buffer. * The image is converted to 8bpp by simple assignment. * If the image is aleady 8 or 6bpp, no copy is done and a pointer * to the correct image section is put in the line buffer. **********************************************************************/void IMAGE::fast_get_line( //get image line inT32 x, //coord to start at inT32 y, //line to get inT32 width, //no of pixels to get IMAGELINE *linebuf //line to copy to ) { if (width > 0 && bpp > 4) { check_legal_access(x, y, width); //get pointer only linebuf->pixels = image + xdim * (ymax - 1 - y) + x * bytespp; } else //just copy it this->get_line (x, y, width, linebuf, 0); linebuf->bpp = bpp;}/********************************************************************** * get_line * * Get a line of image into the supplied image line buffer. * The image is converted to 8bpp by simple assignment. **********************************************************************/void IMAGE::get_line( //get image line inT32 x, //coord to start at inT32 y, //line to get inT32 width, //no of pixels to get IMAGELINE *linebuf, //line to copy to inT32 margins //size of margins ) { uinT8 *src; //source pointer uinT8 *dest; //destination pointer uinT8 *unpacksrc; //unpacking pointer inT8 bit; //bit index inT8 pixperbyte; //pixels per byte uinT8 white; //white colour inT32 pixel; //pixel index //test coords this->check_legal_access (x, y, width); if (width > xsize - x) width = xsize - x; //clip to image width *= bytespp; linebuf->init (width + margins * bytespp * 2); linebuf->bpp = bpp; //start of line src = image + xdim * (ymax - 1 - y); dest = linebuf->line; //destination line linebuf->pixels = dest; white = (1 << bpp) - 1; //max value of pixel for (pixel = margins * bytespp; pixel > 0; pixel--) { *dest++ = white; //margins are white } if (width > 0) { if (bpp > 4) { src += x; //offset //easy way memmove (dest, src, (unsigned) width); } else if (bpp == 4) { src += x / 2; //offset on line if (x & 1) { //get coded nibble *dest++ = bpp4table[*src++][1]; width--; } while (width >= 2) { //get coded bits unpacksrc = bpp4table[*src++]; *dest++ = *unpacksrc++; *dest++ = *unpacksrc++; //copy nibbles width -= 2; } if (width) { //get coded nibble *dest++ = bpp4table[*src++][0]; } } else if (bpp == 2) { pixperbyte = 4; src += x / 4; //offset on line bit = (inT8) (x % 4); //offset in byte width += bit; while (width > 0) { //until all done if (width < pixperbyte) //less on last byte pixperbyte = (inT8) width; //get coded bits unpacksrc = &bpp2table[*src++][bit]; for (; bit < pixperbyte; bit++) *dest++ = *unpacksrc++;//copy bytes width -= pixperbyte; bit = 0; } } else { pixperbyte = 8; src += x / 8; //offset on line bit = (inT8) (x % 8); //offset in byte width += bit; while (width > 0) { //until all done if (width < pixperbyte) //less on last byte pixperbyte = (inT8) width; //get coded bits unpacksrc = &bpp1table[*src++][bit]; for (; bit < pixperbyte; bit++) *dest++ = *unpacksrc++;//copy bytes width -= pixperbyte; bit = 0; } } } for (pixel = margins * bytespp; pixel > 0; pixel--) { *dest++ = white; //margins are white }}/********************************************************************** * get_column * * Get a column of image into the supplied image line buffer. * The image is converted to 8bpp by simple assignment. **********************************************************************/void IMAGE::get_column( //get image column inT32 x, //coord to start at inT32 y, //line to get inT32 height, //no of pixels to get IMAGELINE *linebuf, //line to copy to inT32 margins //size of margins ) { uinT8 *src; //source pointer uinT8 *dest; //destination pointer inT8 bit; //bit index inT8 pixperbyte; //pixels per byte uinT8 white; //white colour inT32 pixel; //pixel index //test coords this->check_legal_access (x, y, 1); //test coords this->check_legal_access (x, y + height - 1, 1); if (height > ysize - y) height = ysize - y; //clip to image linebuf->init (height * bytespp + margins * bytespp * 2); //start of line src = image + xdim * (ymax - 1 - y); dest = linebuf->line; //destination line linebuf->pixels = dest; white = (1 << bpp) - 1; //max value of pixel for (pixel = margins * bytespp; pixel > 0; pixel--) { *dest++ = white; //margins are white } if (height > 0) { if (bpp == 24) { src += x * bytespp; //offset for (; height > 0; --height) { *dest++ = *src; //copy bytes *dest++ = *(src + 1); *dest++ = *(src + 2); src -= xdim; } } else if (bpp > 4) { src += x; for (; height > 0; --height) { *dest++ = *src; //copy bytes src -= xdim; } } else if (bpp == 4) { src += x / 2; //offset on line if (x & 1) { for (; height > 0; --height) { //get coded nibble *dest++ = bpp4table[*src][1]; src -= xdim; } } else { for (; height > 0; --height) { //get coded nibble *dest++ = bpp4table[*src][0]; src -= xdim; } } } else if (bpp == 2) { pixperbyte = 4; src += x / 4; //offset on line bit = (inT8) (x % 4); //offset in byte for (; height > 0; --height) { //get coded bits *dest++ = bpp2table[*src][bit]; src -= xdim; } } else { pixperbyte = 8; src += x / 8; //offset on line bit = (inT8) (x % 8); //offset in byte for (; height > 0; --height) { //get coded bits *dest++ = bpp1table[*src][bit]; src -= xdim; } } } for (pixel = margins * bytespp; pixel > 0; pixel--) { *dest++ = white; //margins are white }}/********************************************************************** * fast_put_line * * Put a line buffer back into the image. * If the line buffer merely points back into the image, nothing is done. * Otherwise, put_line is used to copy the line back. **********************************************************************/void IMAGE::fast_put_line( //put image line inT32 x, //coord to start at inT32 y, //line to get inT32 width, //no of pixels to put IMAGELINE *linebuf //line to copy to ) { if (width > 0 && (bpp <= 4 || linebuf->pixels == linebuf->line)) //just copy it put_line (x, y, width, linebuf, 0);}/********************************************************************** * put_line * * Put the supplied line buffer into the image. * The image is converted from 8bpp by simple assignment. **********************************************************************/void IMAGE::put_line( //put image line inT32 x, //coord to start at inT32 y, //line to get inT32 width, //no of pixels to get IMAGELINE *linebuf, //line to copy to inT32 margins //margins in buffer ) { uinT8 *src; //source pointer uinT8 *dest; //destination pointer inT8 bit; //bit index uinT8 pixel; //collected bits inT8 pixperbyte; //pixels in a byte inT8 bytesperpix; //in source this->check_legal_access (x, y, width); if (width > xsize - x) width = xsize - x; //clip to image if (width <= 0) return; //nothing to do //source line src = linebuf->pixels + margins; //start of line dest = image + xdim * (ymax - 1 - y); if (linebuf->bpp == 24) { src++; bytesperpix = 3; } else bytesperpix = 1; if (bpp == 24 && linebuf->bpp == 24) { dest += x * bytespp; width *= bytespp; memmove (dest, src - 1, (unsigned) width); } else if (bpp == 24) { src--; dest += x * bytespp; while (width > 0) { pixel = *src++; *dest++ = pixel; *dest++ = pixel; *dest++ = pixel; width--; } } else if (bpp > 4) { dest += x; //offset if (linebuf->bpp == 24) { while (width > 0) { *dest++ = *src; src += 3; width--; } } else //easy way memmove (dest, src, (unsigned) width); } else if (bpp == 4) { dest += x / 2; //offset on line if (x & 1) { *dest &= 0xf0; //clean odd byte *dest++ |= *src & 0x0f; //and copy it src += bytesperpix; width--; } while (width >= 2) { pixel = *src << 4; //left pixel src += bytesperpix; pixel |= *src & 0x0f; //right pixel src += bytesperpix; *dest++ = pixel; width -= 2; } if (width) { *dest &= 0x0f; //clean odd byte *dest |= *src << 4; } } else if (bpp == 2) { pixperbyte = 4; dest += x / 4; //offset on line bit = (inT8) (x % 4); //offset in byte width += bit; pixel = *dest >> (8 - bit - bit); while (width >= 4) { //until all done for (; bit < 4; bit++) { pixel <<= 2; //make space for new one pixel |= *src & 3; src += bytesperpix; } *dest++ = pixel; //new pixel width -= 4; bit = 0; } if (width > 0) { //until all done for (bit = 0; bit < width; bit++) { pixel <<= 2; //make space for new one pixel |= *src & 3; src += bytesperpix; } pixel <<= (8 - bit - bit); //shift rest //keep trainling bits
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -