示例#1
0
文件: epeg_main.c 项目: anjn/epeg
/**
 * This saves the image to its specified destination.
 * @param im A handle to an opened Epeg image.
 * 
 * This saves the image @p im to its destination specified by
 * epeg_file_output_set() or epeg_memory_output_set(). The image will be
 * encoded at the deoded pixel size, using the quality, comment and thumbnail
 * comment settings set on the image.
 *
 * retval 1 - error scale
 *        2 - error encode
 *        3 - error decode
 *        4 - error decode ( setjmp )
 */
EAPI int
epeg_encode(Epeg_Image *im)
{
   int ret;
   if ((ret = _epeg_decode(im)) != 0)
     return (ret == 2 ? 4 : 3);
   if (_epeg_scale(im) != 0)
     return 1;
   if (_epeg_encode(im) != 0)
     return 2;
   return 0;
}
示例#2
0
文件: epeg.c 项目: clones/kaa
/**
 * Get a segment of decoded pixels from an image.
 * @param im A handle to an opened Epeg image.
 * @param x Rectangle X.
 * @param y Rectangle Y.
 * @param w Rectangle width.
 * @param h Rectangle height.
 * @return Pointer to the top left of the requested pixel block.
 *
 * Return image pixels in the decoded format from the specified location
 * rectangle bounded with the box @p x, @p y @p w X @p y. The pixel block is
 * packed with no row padding, and it organsied from top-left to bottom right,
 * row by row. You must free the pixel block using epeg_pixels_free() before
 * you close the image handle, and assume the pixels to be read-only memory.
 *
 * On success the pointer is returned, on failure, NULL is returned. Failure
 * may be because the rectangle is out of the bounds of the image, memory
 * allocations failed or the image data cannot be decoded.
 *
 */
const void *
epeg_pixels_get(Epeg_Image *im)
{
    int xx, yy, bpp, w, h;
    unsigned int *pix, *p;

    if (!im->pixels)
	{
	    if (_epeg_decode(im) != 0) return NULL;
	}

    if (!im->pixels) return NULL;
    if ((im->out.w < 1) || (im->out.h < 1)) return NULL;
    if (_epeg_scale(im) != 0) return NULL;

    bpp = im->in.jinfo.output_components;
    w = im->out.w;
    h = im->out.h;

    pix = malloc(w * h * 4);
    if (!pix) return NULL;

    for (yy = 0; yy < h; yy++)
	{
	    unsigned char *s;

	    s = im->lines[yy];
	    p = pix + ((((yy) * w)));
	    for (xx = 0; xx < w; xx++)
		{
		    p[0] = 0xff000000 | (s[0] << 16) | (s[1] << 8) | (s[2]);
		    p++;
		    s += bpp;
		}
	}
    return pix;
}
示例#3
0
/**
 * Get a segment of decoded pixels from an image.
 * @param im A handle to an opened Epeg image.
 * @param x Rectangle X.
 * @param y Rectangle Y.
 * @param w Rectangle width.
 * @param h Rectangle height.
 * @return Pointer to the top left of the requested pixel block.
 *
 * Return image pixels in the decoded format from the specified location
 * rectangle bounded with the box @p x, @p y @p w X @p y. The pixel block is
 * packed with no row padding, and it organsied from top-left to bottom right,
 * row by row. You must free the pixel block using epeg_pixels_free() before
 * you close the image handle, and assume the pixels to be read-only memory.
 *
 * On success the pointer is returned, on failure, NULL is returned. Failure
 * may be because the rectangle is out of the bounds of the image, memory
 * allocations failed or the image data cannot be decoded.
 *
 */
EAPI const void *
epeg_pixels_get_as_RGB8(Epeg_Image *im, int x, int y,  int w, int h) {
    int xx, yy, ww, hh, bpp, ox, oy, ow, oh, iw, ih;

    if (!im->pixels) {
        if (_epeg_decode(im) != 0) {
            return NULL;
        }
    }

    if (!im->pixels) {
        return NULL;
    }
    if ((im->out.w < 1) || (im->out.h < 1)) {
        return NULL;
    }

    bpp = im->in.jinfo.output_components;
    iw = im->out.w;
    ih = im->out.h;
    ow = w;
    oh = h;
    ox = 0;
    oy = 0;
    if ((x + ow) > iw) {
        ow = iw - x;
    }
    if ((y + oh) > ih) {
        oh = ih - y;
    }
    if (ow < 1) {
        return NULL;
    }
    if (oh < 1) {
        return NULL;
    }
    if (x < 0) {
        ow += x;
        ox = -x;
    }
    if (y < 0) {
        oh += y;
        oy = -y;
    }
    if (ow < 1) {
        return NULL;
    }
    if (oh < 1) {
        return NULL;
    }

    ww = x + ox + ow;
    hh = y + oy + oh;

    if (im->color_space == EPEG_GRAY8) {
        unsigned char *pix, *p;

        pix = malloc(w * h * 3);
        if (!pix) {
            return NULL;
        }
        for (yy = y + oy; yy < hh; yy++) {
            unsigned char *s;

            s = im->lines[yy] + ((x + ox) * bpp);
            p = pix + ((((yy - y) * w) + ox) * 3);
            for (xx = x + ox; xx < ww; xx++) {
                p[0] = s[0];
                p[1] = s[0];
                p[2] = s[0];
                p += 3;
                s += bpp;
            }
        }
        return pix;
    }
    if (im->color_space == EPEG_RGB8) {
        unsigned char *pix, *p;

        pix = malloc(w * h * 3);
        if (!pix) {
            return NULL;
        }
        for (yy = y + oy; yy < hh; yy++) {
            unsigned char *s;

            s = im->lines[yy] + ((x + ox) * bpp);
            p = pix + ((((yy - y) * w) + ox) * 3);
            for (xx = x + ox; xx < ww; xx++) {
                p[0] = s[0];
                p[1] = s[1];
                p[2] = s[2];
                p += 3;
                s += bpp;
            }
        }
        return pix;
    }
    if (im->color_space == EPEG_CMYK) {
        unsigned char *pix, *p;

        pix = malloc(w * h * 3);
        if (!pix) {
            return NULL;
        }
        for (yy = y + oy; yy < hh; yy++) {
            unsigned char *s;

            s = im->lines[yy] + ((x + ox) * bpp);
            p = pix + ((((yy - y) * w) + ox) * 3);
            for (xx = x + ox; xx < ww; xx++) {
                p[0] = (unsigned char)(MIN(255, (s[0] * s[3]) / 255));
                p[1] = (unsigned char)(MIN(255, (s[1] * s[3]) / 255));
                p[2] = (unsigned char)(MIN(255, (s[2] * s[3]) / 255));
                p += 3;
                s += bpp;
            }
        }
        return pix;
    }
    return NULL;
}
示例#4
0
文件: epeg_main.c 项目: anjn/epeg
/**
 * Get a segment of decoded pixels from an image.
 * @param im A handle to an opened Epeg image.
 * @param x Rectangle X.
 * @param y Rectangle Y.
 * @param w Rectangle width.
 * @param h Rectangle height.
 * @return Pointer to the top left of the requested pixel block.
 * 
 * Return image pixels in the decoded format from the specified location
 * rectangle bounded with the box @p x, @p y @p w X @p y. The pixel block is
 * packed with no row padding, and it organsied from top-left to bottom right,
 * row by row. You must free the pixel block using epeg_pixels_free() before
 * you close the image handle, and assume the pixels to be read-only memory.
 * 
 * On success the pointer is returned, on failure, NULL is returned. Failure
 * may be because the rectangle is out of the bounds of the image, memory
 * allocations failed or the image data cannot be decoded.
 * 
 */
EAPI const void *
epeg_pixels_get(Epeg_Image *im, int x, int y,  int w, int h)
{
   int xx, yy, ww, hh, bpp, ox, oy, ow, oh, iw, ih;
   
   if (!im->pixels)
     {
	if (_epeg_decode(im) != 0) return NULL;
     }
   
   if (!im->pixels) return NULL;
   if ((im->out.w < 1) || (im->out.h < 1)) return NULL;
   
   if (_epeg_scale(im) != 0) return NULL;
   
   bpp = im->in.jinfo.output_components;
   iw = im->out.w;
   ih = im->out.h;
   ow = w;
   oh = h;
   ox = 0;
   oy = 0;
   if ((x + ow) > iw) ow = iw - x;
   if ((y + oh) > ih) oh = ih - y;
   if (ow < 1) return NULL;
   if (oh < 1) return NULL;
   if (x < 0)
     {
	ow += x;
	ox = -x;
     }
   if (y < 0)
     {
	oh += y;
	oy = -y;
     }
   if (ow < 1) return NULL;
   if (oh < 1) return NULL;

   ww = x + ox + ow;
   hh = y + oy + oh;

   if (im->color_space == EPEG_GRAY8)
     {
	unsigned char *pix, *p;
	
	pix = malloc(w * h * 1);
	if (!pix) return NULL;
	for (yy = y + oy; yy < hh; yy++)
	  {
	     unsigned char *s;
	     
	     s = im->lines[yy] + ((x + ox) * bpp);
	     p = pix + ((((yy - y) * w) + ox));
	     for (xx = x + ox; xx < ww; xx++)
	       {
		  p[0] = s[0];
		  p++;
		  s += bpp;
	       }
	  }
	return pix;
     }
   else if (im->color_space == EPEG_YUV8)
     {
	unsigned char *pix, *p;
	
	pix = malloc(w * h * 3);
	if (!pix) return NULL;
	for (yy = y + oy; yy < hh; yy++)
	  {
	     unsigned char *s;
	     
	     s = im->lines[yy] + ((x + ox) * bpp);
	     p = pix + ((((yy - y) * w) + ox) * 3);
	     for (xx = x + ox; xx < ww; xx++)
	       {
		  p[0] = s[0];
		  p[1] = s[1];
		  p[2] = s[2];
		  p += 3;
		  s += bpp;
	       }
	  }
	return pix;
     }
   else if (im->color_space == EPEG_RGB8)
     {
	unsigned char *pix, *p;
	
	pix = malloc(w * h * 3);
	if (!pix) return NULL;
	for (yy = y + oy; yy < hh; yy++)
	  {
	     unsigned char *s;
	     
	     s = im->lines[yy] + ((x + ox) * bpp);
	     p = pix + ((((yy - y) * w) + ox) * 3);
	     for (xx = x + ox; xx < ww; xx++)
	       {
		  p[0] = s[0];
		  p[1] = s[1];
		  p[2] = s[2];
		  p += 3;
		  s += bpp;
	       }
	  }
	return pix;
     }
   else if (im->color_space == EPEG_BGR8)
     {
	unsigned char *pix, *p;
	
	pix = malloc(w * h * 3);
	if (!pix) return NULL;
	for (yy = y + oy; yy < hh; yy++)
	  {
	     unsigned char *s;
	     
	     s = im->lines[yy] + ((x + ox) * bpp);
	     p = pix + ((((yy - y) * w) + ox) * 3);
	     for (xx = x + ox; xx < ww; xx++)
	       {
		  p[0] = s[2];
		  p[1] = s[1];
		  p[2] = s[0];
		  p += 3;
		  s += bpp;
	       }
	  }
	return pix;
     }
   else if (im->color_space == EPEG_RGBA8)
     {
	unsigned char *pix, *p;
	
	pix = malloc(w * h * 4);
	if (!pix) return NULL;
	for (yy = y + oy; yy < hh; yy++)
	  {
	     unsigned char *s;
	     
	     s = im->lines[yy] + ((x + ox) * bpp);
	     p = pix + ((((yy - y) * w) + ox) * 4);
	     for (xx = x + ox; xx < ww; xx++)
	       {
		  p[0] = s[0];
		  p[1] = s[1];
		  p[2] = s[2];
		  p[3] = 0xff;
		  p += 4;
		  s += bpp;
	       }
	  }
	return pix;
     }
   else if (im->color_space == EPEG_BGRA8)
     {
	unsigned char *pix, *p;
	
	pix = malloc(w * h * 4);
	if (!pix) return NULL;
	for (yy = y + oy; yy < hh; yy++)
	  {
	     unsigned char *s;
	     
	     s = im->lines[yy] + ((x + ox) * bpp);
	     p = pix + ((((yy - y) * w) + ox) * 4);
	     for (xx = x + ox; xx < ww; xx++)
	       {
		  p[0] = 0xff;
		  p[1] = s[2];
		  p[2] = s[1];
		  p[3] = s[0];
		  p += 4;
		  s += bpp;
	       }
	  }
	return pix;
     }
   else if (im->color_space == EPEG_ARGB32)
     {
	unsigned int *pix, *p;
	
	pix = malloc(w * h * 4);
	if (!pix) return NULL;
	for (yy = y + oy; yy < hh; yy++)
	  {
	     unsigned char *s;
	     
	     s = im->lines[yy] + ((x + ox) * bpp);
	     p = pix + ((((yy - y) * w) + ox));
	     for (xx = x + ox; xx < ww; xx++)
	       {
		  p[0] = 0xff000000 | (s[0] << 16) | (s[1] << 8) | (s[2]);
		  p++;
		  s += bpp;
	       }
	  }
	return pix;
     }
   else if (im->color_space == EPEG_CMYK)
     {
	unsigned char *pix, *p;
	
	pix = malloc(w * h * 4);
	if (!pix) return NULL;
	for (yy = y + oy; yy < hh; yy++)
	  {
	     unsigned char *s;
	     
	     s = im->lines[yy] + ((x + ox) * bpp);
	     p = pix + ((((yy - y) * w) + ox) * 4);
	     for (xx = x + ox; xx < ww; xx++)
	       {
		  p[0] = s[0];
		  p[1] = s[1];
		  p[2] = s[2];
		  p[3] = 0xff;
		  p += 4;
		  s += bpp;
	       }
	  }
	return pix;
     }
   return NULL;
}