Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
3.3 kB
1
Indexable
Never
```
include <resource/image/rawimage.h>
using namespace FWeb;
 
SmartPtr<FWeb::Image> PSImage::cropImage(SmartPtr<FWeb::Image> a_img, FWeb::Rect& a_cropToBox) {  
    FWeb::Rect srcBox = a_cropToBox;
   
    if(a_cropToBox.x < 0)
        srcBox.x = 0;
    if(a_cropToBox.y < 0)
        srcBox.y = 0;
    if(a_cropToBox.width > a_img->getWidth())
        srcBox.width = a_img->getWidth();
    if(a_cropToBox.height > a_img->getHeight())
        srcBox.height = a_img->getHeight();
 
    if(srcBox.x == 0 && srcBox.y == 0 && srcBox.width == a_img->getWidth() && srcBox.height == a_img->getHeight())
        return a_img;
 
    SmartPtr<RawImage> inImg = smart_cast<RawImage>(a_img->getRawImage());
    SmartPtr<RawImage> outImg = fwnew RawImage("out" + inImg->getId(), a_img);
 
    size_t srcStride = a_img->getStride();
    size_t destStride = srcStride;
 
    outImg->setWidth((int)srcBox.width);
    outImg->setHeight((int)srcBox.height);
    destStride = outImg->getStride();
 
   // Move to below
    //Vector<unsigned char>::Type srcBuffTemp(srcStride);
    //unsigned char* srcBuff = &srcBuffTemp[0];


    outImg->writeHeader(); //write out the image header
    outImg->copyPaletteData(a_img); //use the same colour palette for Index images
	
    SmartPtr<BackingStore> in = inImg->getImageData();
    SmartPtr<BackingStore> out = outImg->getImageData();
    size_t xoff = 0;
    size_t shiftBits = 0;
 
    switch(outImg->getBPC()) {
        case 8:
        default:
            xoff = (size_t)(srcBox.x * outImg->getComponents());
    }
 
    //Seek to the start of the cropped data
    in->seekCurrent(srcStride * (int)srcBox.y); 
    
    //reserve output image buffer
    out->reserve(outImg->getHeight()*destStride);
 
    // Store source image data
    Vector<unsigned char>::Type srcBuffTemp(srcStride * outImg->getHeight());
    in->read((char *)&srcBuffTemp, srcStride * outImg->getHeight());
    unsigned char* srcBuffStart = &srcBuffTemp[0];

    //prepare the output temp buff...
    Vector<unsigned char>::Type outBuffTemp(destStride * outImg->getHeight());      
    unsigned char* outBuffStart = &outBuffTemp[0];

    for(int i = 0; i < outImg->getHeight() && in->size() > 0; i++) {
        /* Just first row has been copied
        if(in->read((char *)srcBuff, srcStride) == 0) {
            break;
        } else {
             //prepare the output temp buff...
            Vector<unsigned char>::Type outBuffTemp(destStride);            			
            switch(outImg->getBPC()) {
                case 8:
                default: {
                    out->write((char *)(srcBuff + xoff), destStride);
                    break;
                }
            }
        }*/
        // Copy data to outBuffTemp, row by row
        unsigned char* srcBuff = srcBuffStart + srcStride * i;
        unsigned char* outBuff = srcBuffStart + destStride * i;
        memcpy(outBuff, srcBuff + xoff, destStride);
    }

    // Write data from temp buffer to outImg object
    out->write((char *)outBuffStart, destStride * outImg->getHeight());
 
    a_cropToBox.x = 0;
    a_cropToBox.y = 0;
    a_cropToBox.width = outImg->getWidth();
    a_cropToBox.height = outImg->getHeight();
 
    return outImg;
}
```