Example #1
0
bool ImageSegmenter::_generateRaw( const ImageWrapper& image,
                                   const Handler& handler ) const
{
    const SegmentParametersList& paramList = _generateSegmentParameters( image );

    // resulting Raw segments
    for( SegmentParametersList::const_iterator it = paramList.begin();
            it != paramList.end(); ++it )
    {
        Segment segment;
        segment.parameters = *it;
        segment.imageData.reserve( segment.parameters.width *
                                   segment.parameters.height *
                                   image.getBytesPerPixel( ));

        if( paramList.size() == 1 )
        {
            // If we are not segmenting the image, just append the image data
            segment.imageData.append( (const char*)image.data,
                                      int(image.getBufferSize( )));
        }
        else // Copy the image subregion
        {
            // assume imageBuffer isn't padded
            const size_t imagePitch = image.width * image.getBytesPerPixel();
            const size_t offset = segment.parameters.y * imagePitch +
                                  segment.parameters.x * image.getBytesPerPixel();
            const char* lineData = (const char*)image.data + offset;

            for( unsigned int i = 0; i < segment.parameters.height; ++i )
            {
                segment.imageData.append( lineData, segment.parameters.width *
                                          image.getBytesPerPixel( ));
                lineData += imagePitch;
            }
        }

        if( !handler( segment ))
            return false;
    }

    return true;
}
Example #2
0
QByteArray ImageJpegCompressor::computeJpeg(const ImageWrapper& sourceImage,
                                            const QRect& imageRegion)
{
    // tjCompress API is incorrect and takes a non-const input buffer, even
    // though it does not modify it. It can "safely" be cast to non-const
    // pointer to comply to the incorrect API.
    unsigned char* tjSrcBuffer = (unsigned char*)sourceImage.data;
    tjSrcBuffer +=
        imageRegion.y() * sourceImage.width * sourceImage.getBytesPerPixel();
    tjSrcBuffer += imageRegion.x() * sourceImage.getBytesPerPixel();

    const int tjWidth = imageRegion.width();
    // assume imageBuffer isn't padded
    const int tjPitch = sourceImage.width * sourceImage.getBytesPerPixel();
    const int tjHeight = imageRegion.height();
    const int tjPixelFormat = getTurboJpegFormat(sourceImage.pixelFormat);
    unsigned char* tjJpegBuf = 0;
    unsigned long tjJpegSize = 0;
    const int tjJpegSubsamp = TJSAMP_444;
    const int tjJpegQual = sourceImage.compressionQuality;
    const int tjFlags = 0; // or: TJFLAG_BOTTOMUP

    int err = tjCompress2(_tjHandle, tjSrcBuffer, tjWidth, tjPitch, tjHeight,
                          tjPixelFormat, &tjJpegBuf, &tjJpegSize, tjJpegSubsamp,
                          tjJpegQual, tjFlags);
    if (err != 0)
    {
        std::cerr << "libjpeg-turbo image conversion failure" << std::endl;
        return QByteArray();
    }

    // move the JPEG buffer to a byte array
    const QByteArray jpegData((char*)tjJpegBuf, tjJpegSize);

    // free the libjpeg-turbo allocated memory
    tjFree(tjJpegBuf);

    return jpegData;
}