shared_ptr<Image> CameraImageInput::createImageFromImageFormat(Img::ImageData& srcImage)
{
    auto image = std::make_shared<Image>();
    auto& data = srcImage.raw_data;
    QImage qImage = QImage::fromData(data.get_buffer(), data.length());
    const int w = qImage.width();
    const int h = qImage.height();
    const bool hasAlpha = qImage.hasAlphaChannel();
    if(qImage.isGrayscale()){
        image->setSize(w, h, 1);
        unsigned char* pixels = image->pixels();
        for(int y=0; y < h; ++y){
            for(int x=0; x < w; ++x){
                *pixels++ = qGray(qImage.pixel(x, y));
            }
        }
    } else {
        image->setSize(w, h, hasAlpha ? 4 : 3);
        unsigned char* pixels = image->pixels();
        for(int y=0; y < h; ++y){
            for(int x=0; x < w; ++x){
                QRgb rgb = qImage.pixel(x, y);
                *pixels++ = qRed(rgb);
                *pixels++ = qGreen(rgb);
                *pixels++ = qBlue(rgb);
                if(hasAlpha){
                    *pixels++ = qAlpha(rgb);
                }
            }
        }
    }
    return image;
}
Example #2
0
// Check the file for being a readable QImage and set up parameters
int iiQImageCheck(ImodImageFile *inFile)
{
  QImage *image;
  if (!inFile) 
    return IIERR_BAD_CALL;
  image = new QImage(QString(inFile->filename));
  if (image->isNull()) {
    delete image;
    return IIERR_NOT_FORMAT;
  }

  if (image->depth() < 8) {
    delete image;
    b3dError(NULL, "%s is a recognized file type but data type is not "
             "supported\n", inFile->filename);
    return IIERR_NO_SUPPORT;
  }

  if(inFile->fp)
    fclose(inFile->fp);
  inFile->nx = image->width();
  inFile->ny = image->height();
  inFile->nz = 1;
  inFile->file = IIFILE_QIMAGE;
  inFile->type   = IITYPE_UBYTE;
  inFile->amean  = 128;
  inFile->amax   = 255;
  inFile->smax   = 255;

  // Grayscale images have depth 8 and color tables that are gray
  if (image->depth() == 8 && image->isGrayscale()) {
    inFile->format = IIFORMAT_LUMINANCE;
    inFile->readSectionByte = qimageReadSectionByte;
    inFile->readSectionFloat = qimageReadSectionFloat;
    inFile->mode   = MRC_MODE_BYTE;
  } else {

    // Otherwise images will be read as RGB
    inFile->format = IIFORMAT_RGB;
    inFile->mode   = MRC_MODE_RGB;
    inFile->readSection = qimageReadSection;
  }

  // Imitate TIFF for some of these behaviors - close file, assign image to
  // header but not fp here, assign to fp when reopen
  inFile->headerSize = 8;
  inFile->header = (char *)image;

  inFile->cleanUp = qimageClose;
  inFile->reopen = qimageReopen;
  inFile->close = qimageClose;
  inFile->fillMrcHeader = tiffFillMrcHeader;

  return 0;
}
void ImggFormatsConvertViewQtWidget::convert(
    const std::string &inputName, const std::string &inputFormat,
    const std::string &outputName, const std::string &outputFormat) const {

  QImage img = loadImgFile(inputName, inputFormat);

  if (!img.isGrayscale()) {
    // Qt5 has QImage::Format_Alpha8;
    QImage::Format toFormat = QImage::Format_RGB32;
    Qt::ImageConversionFlag toFlags = Qt::MonoOnly;
    img = img.convertToFormat(toFormat, toFlags);
  }

  writeImgFile(img, outputName, outputFormat);
}
Example #4
0
bool PNGAImageWriter::Export(QFile& file) {
    QImage pixmap = buildImage();
    assert(pixmap.isGrayscale());
    png::image<png::gray_pixel> image(pixmap.width(), pixmap.height());
    for (size_t y = 0; y < image.get_height(); ++y)
    {
        for (size_t x = 0; x < image.get_width(); ++x)
        {
            QRgb pix = pixmap.pixel(x, y);
            assert(qIsGray(pix));
            image[y][x] = png::gray_pixel(qAlpha(pix));
        }
    }
    image.write(file.fileName().toStdString());
    return true;
}
/**
 * Automatic marshaling of a QImage for org.freedesktop.Notifications.Notify
 *
 * This function is from the Clementine project (see
 * http://www.clementine-player.org) and licensed under the GNU General Public
 * License, version 3 or later.
 *
 * Copyright 2010, David Sansome <*****@*****.**>
 */
QDBusArgument &operator<<( QDBusArgument &arg, const QImage &image )
{
  if ( image.isNull() )
  {
    arg.beginStructure();
    arg << 0 << 0 << 0 << false << 0 << 0 << QByteArray();
    arg.endStructure();
    return arg;
  }

  QImage scaled = image.scaledToHeight( 100, Qt::SmoothTransformation );
  scaled = scaled.convertToFormat( QImage::Format_ARGB32 );

#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
  // ABGR -> ARGB
  QImage i = scaled.rgbSwapped();
#else
  // ABGR -> GBAR
  QImage i( scaled.size(), scaled.format() );
  for ( int y = 0; y < i.height(); ++y )
  {
    QRgb *p = ( QRgb * ) scaled.scanLine( y );
    QRgb *q = ( QRgb * ) i.scanLine( y );
    QRgb *end = p + scaled.width();
    while ( p < end )
    {
      *q = qRgba( qGreen( *p ), qBlue( *p ), qAlpha( *p ), qRed( *p ) );
      p++;
      q++;
    }
  }
#endif

  arg.beginStructure();
  arg << i.width();
  arg << i.height();
  arg << i.bytesPerLine();
  arg << i.hasAlphaChannel();
  int channels = i.isGrayscale() ? 1 : ( i.hasAlphaChannel() ? 4 : 3 );
  arg << i.depth() / channels;
  arg << channels;
  arg << QByteArray( reinterpret_cast<const char *>( i.bits() ), i.numBytes() );
  arg.endStructure();
  return arg;
}
Example #6
0
// -------------------------------------------------------------------------
void ctkQImageView::addImage( const QImage & image )
{
  Q_D( ctkQImageView );
  d->ImageList.push_back( image );
  d->TmpXMin = 0;
  d->TmpXMax = image.width();
  d->TmpYMin = 0;
  d->TmpYMax = image.height();
  if( image.isGrayscale() )
    {
    d->IntensityMin = 0;
    d->IntensityMax = image.colorCount();
    this->setIntensityWindowLevel(
      image.colorCount(), image.colorCount()/2 );
    }
  this->update( true, false );
  this->setCenter( image.width()/2.0, image.height()/2.0 );
}
void
QtCoinCompatibility::QImageToSbImage(const QImage & image, SbImage & sbimage)
{
    int w = image.width();
    int h = image.height();
    int c;

    // Keep in 8-bits mode if that was what we read
    if (image.depth() == 8 && image.isGrayscale()) {
        c = 1;
    }
    else {
        // FIXME: consider if we should detect allGrayscale() and alpha (c = 2)
        c = image.hasAlphaChannel() ? 4 : 3;
    }

    SbVec2s size((short) w, (short) h);
    sbimage.setValue(size, c, NULL);
    unsigned char * buffer = sbimage.getValue(size, c);

    if (c == 1) {
        for (int i = 0; i < h; i++) {
            memcpy(buffer + i*w, image.scanLine(h-(i+1)), w);
        }
    }
    else { // (c == 3 || c == 4)
        QRgb * bits = (QRgb*) image.bits();
        for (int y = 0; y < h; y++) {
            unsigned char * line = &buffer[c*w*(h-(y+1))];
            for (int x = 0; x < w; x++) {
                *line++ = qRed(*bits);
                *line++ = qGreen(*bits);
                *line++ = qBlue(*bits);
                if (c == 4) {
                    *line++ = qAlpha(*bits);
                }
                bits++;
            }
        }
    }
}
Example #8
0
SbBool
ImageReader::readImage(const SbString & filename, SbImage & sbimage) const
{
  QImage image;
  if (image.load(filename.getString())) {
    //int c;
    //int w = image.width();
    //int h = image.height();

    // Keep in 8-bits mode if that was what we read
    if (image.depth() != 8 || !image.isGrayscale()) {
      // FIXME: consider if we should detect allGrayscale() and alpha (c = 2)
      image = image.convertToFormat(image.hasAlphaChannel() ?
                                    QImage::Format_ARGB32 : QImage::Format_RGB32);
    }

    QtCoinCompatibility::QImageToSbImage(image,sbimage);
    return true;
  }
  return false;
}
//Fonction permettant de faire lexpanssion de la dynamique de l'image a partir de la palette
QImage ImageWindows::expanssionAffichage(QImage img){

    QImage im_expand = img;

    if(!img.isGrayscale())
        return img;

    unsigned char valeur_max = 0;
    for(int i = 0; i< img.height(); i++){
        uchar * bits = im_expand.scanLine(i);
            for(int j = 0; j< img.width(); j++ ){
                if(valeur_max <  bits[j])
                    valeur_max = bits[j] ;
            }
    }
    if(valeur_max < 5){
        im_expand.setColor(0,qRgb(0, 0, 0) );
        im_expand.setColor(1,qRgb(255, 255, 255) );
    }

    return im_expand;
}
Example #10
0
QDBusArgument& operator<< (QDBusArgument& arg, const DBusNotifyImageData &data) {
	if (data.image.isNull()) {
		// Sometimes this gets called with a null QImage for no obvious reason.
		// - There is one reason: Qt calls this method at first time to research it's structure
		arg.beginStructure();
		arg << 0 << 0 << 0 << false << 0 << 0 << QByteArray();
		arg.endStructure();
		return arg;
	}
	QImage scaled = data.image.scaledToHeight(qMin(100, qMin(data.image.height(), data.image.width())),
											  Qt::SmoothTransformation).toImage();
	QImage i = scaled.convertToFormat(QImage::Format_ARGB32).rgbSwapped();
	arg.beginStructure();
	arg << i.width();
	arg << i.height();
	arg << i.bytesPerLine();
	arg << i.hasAlphaChannel();
	int channels = i.isGrayscale() ? 1 : (i.hasAlphaChannel() ? 4 : 3);
	arg << i.depth() / channels;
	arg << channels;
	arg << QByteArray(reinterpret_cast<const char*>(i.bits()), i.numBytes());
	arg.endStructure();
	return arg;
}
Example #11
0
bool Q_INTERNAL_WIN_NO_THROW QPNGImageWriter::writeImage(const QImage& image, volatile int quality_in, const QString &description,
                                 int off_x_in, int off_y_in)
{
    QPoint offset = image.offset();
    int off_x = off_x_in + offset.x();
    int off_y = off_y_in + offset.y();

    png_structp png_ptr;
    png_infop info_ptr;

    png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,0,0,0);
    if (!png_ptr) {
        return false;
    }

    png_set_error_fn(png_ptr, 0, 0, qt_png_warning);

    info_ptr = png_create_info_struct(png_ptr);
    if (!info_ptr) {
        png_destroy_write_struct(&png_ptr, 0);
        return false;
    }

    if (setjmp(png_jmpbuf(png_ptr))) {
        png_destroy_write_struct(&png_ptr, &info_ptr);
        return false;
    }

    int quality = quality_in;
    if (quality >= 0) {
        if (quality > 9) {
            qWarning("PNG: Quality %d out of range", quality);
            quality = 9;
        }
        png_set_compression_level(png_ptr, quality);
    }

    png_set_write_fn(png_ptr, (void*)this, qpiw_write_fn, qpiw_flush_fn);


    int color_type = 0;
    if (image.colorCount()) {
        if (image.isGrayscale())
            color_type = PNG_COLOR_TYPE_GRAY;
        else
            color_type = PNG_COLOR_TYPE_PALETTE;
    }
    else if (image.format() == QImage::Format_Grayscale8)
        color_type = PNG_COLOR_TYPE_GRAY;
    else if (image.hasAlphaChannel())
        color_type = PNG_COLOR_TYPE_RGB_ALPHA;
    else
        color_type = PNG_COLOR_TYPE_RGB;

    png_set_IHDR(png_ptr, info_ptr, image.width(), image.height(),
                 image.depth() == 1 ? 1 : 8, // per channel
                 color_type, 0, 0, 0);       // sets #channels

    if (gamma != 0.0) {
        png_set_gAMA(png_ptr, info_ptr, 1.0/gamma);
    }

    if (image.format() == QImage::Format_MonoLSB)
       png_set_packswap(png_ptr);

    if (color_type == PNG_COLOR_TYPE_PALETTE) {
        // Paletted
        int num_palette = qMin(256, image.colorCount());
        png_color palette[256];
        png_byte trans[256];
        int num_trans = 0;
        for (int i=0; i<num_palette; i++) {
            QRgb rgba=image.color(i);
            palette[i].red = qRed(rgba);
            palette[i].green = qGreen(rgba);
            palette[i].blue = qBlue(rgba);
            trans[i] = qAlpha(rgba);
            if (trans[i] < 255) {
                num_trans = i+1;
            }
        }
        png_set_PLTE(png_ptr, info_ptr, palette, num_palette);

        if (num_trans) {
            png_set_tRNS(png_ptr, info_ptr, trans, num_trans, 0);
        }
    }

    // Swap ARGB to RGBA (normal PNG format) before saving on
    // BigEndian machines
    if (QSysInfo::ByteOrder == QSysInfo::BigEndian) {
        png_set_swap_alpha(png_ptr);
    }

    // Qt==ARGB==Big(ARGB)==Little(BGRA). But RGB888 is RGB regardless
    if (QSysInfo::ByteOrder == QSysInfo::LittleEndian
        && image.format() != QImage::Format_RGB888) {
        png_set_bgr(png_ptr);
    }

    if (off_x || off_y) {
        png_set_oFFs(png_ptr, info_ptr, off_x, off_y, PNG_OFFSET_PIXEL);
    }

    if (frames_written > 0)
        png_set_sig_bytes(png_ptr, 8);

    if (image.dotsPerMeterX() > 0 || image.dotsPerMeterY() > 0) {
        png_set_pHYs(png_ptr, info_ptr,
                image.dotsPerMeterX(), image.dotsPerMeterY(),
                PNG_RESOLUTION_METER);
    }

    set_text(image, png_ptr, info_ptr, description);

    png_write_info(png_ptr, info_ptr);

    if (image.depth() != 1)
        png_set_packing(png_ptr);

    if (color_type == PNG_COLOR_TYPE_RGB && image.format() != QImage::Format_RGB888)
        png_set_filler(png_ptr, 0,
            QSysInfo::ByteOrder == QSysInfo::BigEndian ?
                PNG_FILLER_BEFORE : PNG_FILLER_AFTER);

    if (looping >= 0 && frames_written == 0) {
        uchar data[13] = "NETSCAPE2.0";
        //                0123456789aBC
        data[0xB] = looping%0x100;
        data[0xC] = looping/0x100;
        png_write_chunk(png_ptr, const_cast<png_bytep>((const png_byte *)"gIFx"), data, 13);
    }
    if (ms_delay >= 0 || disposal!=Unspecified) {
        uchar data[4];
        data[0] = disposal;
        data[1] = 0;
        data[2] = (ms_delay/10)/0x100; // hundredths
        data[3] = (ms_delay/10)%0x100;
        png_write_chunk(png_ptr, const_cast<png_bytep>((const png_byte *)"gIFg"), data, 4);
    }

    int height = image.height();
    int width = image.width();
    switch (image.format()) {
    case QImage::Format_Mono:
    case QImage::Format_MonoLSB:
    case QImage::Format_Indexed8:
    case QImage::Format_Grayscale8:
    case QImage::Format_RGB32:
    case QImage::Format_ARGB32:
    case QImage::Format_RGB888:
        {
            png_bytep* row_pointers = new png_bytep[height];
            for (int y=0; y<height; y++)
                row_pointers[y] = const_cast<png_bytep>(image.constScanLine(y));
            png_write_image(png_ptr, row_pointers);
            delete [] row_pointers;
        }
        break;
    default:
        {
            QImage::Format fmt = image.hasAlphaChannel() ? QImage::Format_ARGB32 : QImage::Format_RGB32;
            QImage row;
            png_bytep row_pointers[1];
            for (int y=0; y<height; y++) {
                row = image.copy(0, y, width, 1).convertToFormat(fmt);
                row_pointers[0] = const_cast<png_bytep>(row.constScanLine(0));
                png_write_rows(png_ptr, row_pointers, 1);
            }
        }
        break;
    }

    png_write_end(png_ptr, info_ptr);
    frames_written++;

    png_destroy_write_struct(&png_ptr, &info_ptr);

    return true;
}
QByteArray StelTexture::convertToGLFormat(const QImage& image, GLint *format, GLint *type)
{
	QByteArray ret;
	const int width = image.width();
	const int height = image.height();
	if (image.isGrayscale())
	{
		*format = image.hasAlphaChannel() ? GL_LUMINANCE_ALPHA : GL_LUMINANCE;
	}
	else if (image.hasAlphaChannel())
	{
		*format = GL_RGBA;
	}
	else
		*format = GL_RGB;
	*type = GL_UNSIGNED_BYTE;
	int bpp = *format == GL_LUMINANCE_ALPHA ? 2 :
			  *format == GL_LUMINANCE ? 1 :
			  *format == GL_RGBA ? 4 :
			  3;

	ret.reserve(width * height * bpp);
	QImage tmp = image.convertToFormat(QImage::Format_ARGB32);

	// flips bits over y
	int ipl = tmp.bytesPerLine() / 4;
	for (int y = 0; y < height / 2; ++y)
	{
		int *a = (int *) tmp.scanLine(y);
		int *b = (int *) tmp.scanLine(height - y - 1);
		for (int x = 0; x < ipl; ++x)
			qSwap(a[x], b[x]);
	}

	// convert data
	for (int i = 0; i < height; ++i)
	{
		uint *p = (uint *) tmp.scanLine(i);
		for (int x = 0; x < width; ++x)
		{
			uint c = qToBigEndian(p[x]);
			const char* ptr = (const char*)&c;
			switch (*format)
			{
			case GL_RGBA:
				ret.append(ptr + 1, 3);
				ret.append(ptr, 1);
				break;
			case GL_RGB:
				ret.append(ptr + 1, 3);
				break;
			case GL_LUMINANCE:
				ret.append(ptr + 1, 1);
				break;
			case GL_LUMINANCE_ALPHA:
				ret.append(ptr + 1, 1);
				ret.append(ptr, 1);
				break;
			default:
				Q_ASSERT(false);
			}
		}
	}
	return ret;
}