Пример #1
0
result_t Image::flip(int32_t dir, AsyncEvent *ac)
{
    if (!m_image)
        return CHECK_ERROR(CALL_E_INVALID_CALL);

    if (!ac)
        return CHECK_ERROR(CALL_E_NOSYNC);

    if (dir == gd_base::_HORIZONTAL)
        gdImageFlipHorizontal(m_image);
    else if (dir == gd_base::_VERTICAL)
        gdImageFlipVertical(m_image);
    else if (dir == gd_base::_BOTH)
        gdImageFlipBoth(m_image);
    else
        return CHECK_ERROR(CALL_E_INVALIDARG);

    return 0;
}
Пример #2
0
/*
	Function: gdImageCreateFromTgaCtx

	Creates a gdImage from a gdIOCtx referencing a TGA binary file.

	Parameters:
		ctx - Pointer to a gdIOCtx structure
 */
BGD_DECLARE(gdImagePtr) gdImageCreateFromTgaCtx(gdIOCtx* ctx)
{
	int bitmap_caret = 0;
	oTga *tga = NULL;
	/*	int pixel_block_size = 0;
		int image_block_size = 0; */
	volatile gdImagePtr image = NULL;
	int x = 0;
	int y = 0;

	tga = (oTga *) gdMalloc(sizeof(oTga));
	if (!tga) {
		return NULL;
	}

	tga->bitmap = NULL;
	tga->ident = NULL;

	if (read_header_tga(ctx, tga) < 0) {
		free_tga(tga);
		return NULL;
	}

	/*TODO: Will this be used?
		pixel_block_size = tga->bits / 8;
		image_block_size = (tga->width * tga->height) * pixel_block_size;
	*/

	if (read_image_tga(ctx, tga) < 0) {
		free_tga(tga);
		return NULL;
	}

	image = gdImageCreateTrueColor((int)tga->width, (int)tga->height );

	if (image == 0) {
		free_tga( tga );
		return NULL;
	}

	/*!	\brief Populate GD image object
	 *  Copy the pixel data from our tga bitmap buffer into the GD image
	 *  Disable blending and save the alpha channel per default
	 */
	if (tga->alphabits) {
		gdImageAlphaBlending(image, 0);
		gdImageSaveAlpha(image, 1);
	}

	/* TODO: use alphabits as soon as we support 24bit and other alpha bps (ie != 8bits) */
	for (y = 0; y < tga->height; y++) {
		register int *tpix = image->tpixels[y];
		for ( x = 0; x < tga->width; x++, tpix++) {
			if (tga->bits == TGA_BPP_24) {
				*tpix = gdTrueColor(tga->bitmap[bitmap_caret + 2], tga->bitmap[bitmap_caret + 1], tga->bitmap[bitmap_caret]);
				bitmap_caret += 3;
			} else if (tga->bits == TGA_BPP_32 && tga->alphabits) {
				register int a = tga->bitmap[bitmap_caret + 3];

				*tpix = gdTrueColorAlpha(tga->bitmap[bitmap_caret + 2], tga->bitmap[bitmap_caret + 1], tga->bitmap[bitmap_caret], gdAlphaMax - (a >> 1));
				bitmap_caret += 4;
			}
		}
	}

	if (tga->flipv && tga->fliph) {
		gdImageFlipBoth(image);
	} else if (tga->flipv) {
		gdImageFlipVertical(image);
	} else if (tga->fliph) {
		gdImageFlipHorizontal(image);
	}

	free_tga(tga);

	return image;
}
static VALUE fastimage_native_resize(
        VALUE self,
        VALUE rb_in, VALUE rb_out,
        VALUE rb_w, VALUE rb_h,
        VALUE rb_image_type,
        VALUE rb_jpeg_quality,
        VALUE rb_orientation
       ) {
  char *filename_in  = StringValuePtr(rb_in);
  char *filename_out = StringValuePtr(rb_out);
  int w              = NUM2INT(rb_w);
  int h              = NUM2INT(rb_h);
  int image_type     = NUM2INT(rb_image_type);
  int jpeg_quality   = NUM2INT(rb_jpeg_quality);
  int orientation    = NUM2INT(rb_orientation);


  gdImagePtr im_in, im_out;
  FILE *in, *out;
  int trans = 0, x = 0, y = 0, f = 0;

  in = fopen(filename_in, "rb");
  if (!in) return Qnil;

  switch(image_type) {
    case 0: im_in = gdImageCreateFromJpeg(in);
            break;
    case 1: im_in = gdImageCreateFromPng(in);
            break;
    case 2: im_in = gdImageCreateFromGif(in);
            trans = gdImageGetTransparent(im_in);
            /* find a transparent pixel, then turn off transparency
               so that it copies correctly */
            if (trans >= 0) {
              for (x=0; x<gdImageSX(im_in); x++) {
                for (y=0; y<gdImageSY(im_in); y++) {
                  if (gdImageGetPixel(im_in, x, y) == trans) {
                    f = 1;
                    break;
                  }
                }
                if (f) break;
              }
              gdImageColorTransparent(im_in, -1);
              if (!f) trans = -1;  /* no transparent pixel found */
            }
            break;
  }

  if (!im_in) {
    fclose(in);
    return Qnil;
  }



  /*  Handle orientation */
  if (orientation == 5 || orientation == 6) {
    im_in = gdImageRotateInterpolated(im_in, 270.0, 0);
  }
  if (orientation == 7 || orientation == 8) {
    im_in = gdImageRotateInterpolated(im_in, 90.0, 0);
  }
  if (!im_in) {
    fclose(in);
    return Qnil;
  }

  if (orientation == 2 || orientation == 5 || orientation == 7) {
    gdImageFlipHorizontal(im_in);
  }
  if (orientation == 3) {
      gdImageFlipBoth(im_in);
  }
  if (orientation == 4) {
    gdImageFlipVertical(im_in);
  }



  /* Compute target size */
  if (w == 0 || h == 0) {
    int originalWidth  = gdImageSX(im_in);
    int originalHeight = gdImageSY(im_in);
    if (h != 0) {
      w = (int)(h * originalWidth / originalHeight);
    } else if (w != 0) {
      h = (int)(w * originalHeight / originalWidth);
    } else {
      w = originalWidth;
      h = originalHeight;
    }
  }



  im_out = gdImageCreateTrueColor(w, h);  /* must be truecolor */
  if (im_out) {
    if (image_type == 1) {
      gdImageAlphaBlending(im_out, 0);  /* handle transparency correctly */
      gdImageSaveAlpha(im_out, 1);
    }
    fclose(in);
  } else {
    fclose(in);
    return Qnil;
  }

  /* Now copy the original */
  gdImageCopyResampled(im_out, im_in, 0, 0, 0, 0,
    gdImageSX(im_out), gdImageSY(im_out),
    gdImageSX(im_in), gdImageSY(im_in));

  out = fopen(filename_out, "wb");
  if (out) {
    switch(image_type) {
      case 0: gdImageJpeg(im_out, out, jpeg_quality);
              break;
      case 1: gdImagePng(im_out, out);
              break;
      case 2: gdImageTrueColorToPalette(im_out, 0, 256);
              if (trans >= 0) {
                trans = gdImageGetPixel(im_out, x, y);  /* get the color index of our transparent pixel */
                gdImageColorTransparent(im_out, trans); /* may not always work as hoped */
              }
              gdImageGif(im_out, out);
              break;
    }
    fclose(out);
  }
  gdImageDestroy(im_in);
  gdImageDestroy(im_out);
  return Qnil;
}
Пример #4
0
result_t Image::load(Buffer_base *data)
{
    std::string strBuf;
    data->toString(strBuf);

    if (strBuf.length() < 2)
        return CHECK_ERROR(CALL_E_INVALIDARG);

    int32_t format;
    unsigned char ch1 = (unsigned char) strBuf[0];
    unsigned char ch2 = (unsigned char) strBuf[1];

    if (ch1 == 0x47 && ch2 == 0x49)
        format = gd_base::_GIF;
    else if (ch1 == 0x89 && ch2 == 0x50)
        format = gd_base::_PNG;
    else if (ch1 == 0xff && ch2 == 0xd8)
        format = gd_base::_JPEG;
    else if ((ch1 == 0x49 && ch2 == 0x49) || (ch1 == 0x4d && ch2 == 0x4d))
        format = gd_base::_TIFF;
    else if (ch1 == 0x42 && ch2 == 0x4d)
        format = gd_base::_BMP;
    else if (ch1 == 0x52 && ch2 == 0x49)
        format = gd_base::_WEBP;
    else
        return CHECK_ERROR(CALL_E_INVALID_DATA);

    switch (format)
    {
    case gd_base::_GIF:
        m_image = gdImageCreateFromGifPtr((int32_t) strBuf.length(),
                                          (void *) strBuf.c_str());
        break;
    case gd_base::_PNG:
        m_image = gdImageCreateFromPngPtr((int32_t) strBuf.length(),
                                          (void *) strBuf.c_str());
        break;
    case gd_base::_JPEG:
        m_image = gdImageCreateFromJpegPtr((int32_t) strBuf.length(),
                                           (void *) strBuf.c_str());
        if (m_image != NULL)
        {
            EXIFInfo result;
            result.parseFrom((const unsigned char *) strBuf.c_str(),
                             (uint32_t)strBuf.length());

            switch (result.Orientation)
            {
            case 2:
                gdImageFlipHorizontal(m_image);
                break;
            case 3:
                gdImageFlipBoth(m_image);
                break;
            case 4:
                gdImageFlipVertical(m_image);
                break;
            case 5:
                gdImageFlipVertical(m_image);
            case 6:
                rotate(gd_base::_RIGHT);
                break;
            case 7:
                gdImageFlipVertical(m_image);
            case 8:
                rotate(gd_base::_LEFT);
                break;
            }
        }

        break;
    case gd_base::_TIFF:
        m_image = gdImageCreateFromTiffPtr((int32_t) strBuf.length(),
                                           (void *) strBuf.c_str());
        break;
    case gd_base::_BMP:
        m_image = gdImageCreateFromBmpPtr((int32_t) strBuf.length(),
                                          (void *) strBuf.c_str());
        break;
    case gd_base::_WEBP:
        m_image = gdImageCreateFromWebpPtr((int32_t) strBuf.length(),
                                           (void *) strBuf.c_str());
        break;
    }

    if (m_image == NULL)
        return CHECK_ERROR(CALL_E_INVALIDARG);

    setExtMemory();
    m_type = format;

    return 0;
}