Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] it seems RGBA image saved as BGRA? #106

Open
DamonsJ opened this issue Oct 28, 2024 · 0 comments
Open

[BUG] it seems RGBA image saved as BGRA? #106

DamonsJ opened this issue Oct 28, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@DamonsJ
Copy link

DamonsJ commented Oct 28, 2024

I run this code in ios and I got image like this :
saved_image3

lipstick_filter_ = LipstickFilter::create();
    blusher_filter_ = BlusherFilter::create();
    
    
    beauty_face_filter_ = BeautyFaceFilter::create();
    face_reshape_filter_ = FaceReshapeFilter::create();
    
    NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"sample_face" ofType:@"png"];
    
    gpuSourceImage = SourceImage::create([imagePath UTF8String]);
    
    gpuSourceImage->RegLandmarkCallback([=](std::vector<float> landmarks) {
        lipstick_filter_->SetFaceLandmarks(landmarks);
        blusher_filter_->SetFaceLandmarks(landmarks);
        face_reshape_filter_->SetFaceLandmarks(landmarks);
    });
    
    // filter pipline
    gpuSourceImage->addTarget(lipstick_filter_)
    ->addTarget(blusher_filter_)
    ->addTarget(face_reshape_filter_)
    ->addTarget(beauty_face_filter_);
    
    
    float value = 8.0f;
    blusher_filter_->setBlendLevel(value/10);
    lipstick_filter_->setBlendLevel(value/10);
    face_reshape_filter_->setEyeZoomLevel(value/50);
    face_reshape_filter_->setFaceSlimLevel(value/100);
    beauty_face_filter_->setWhite(value/20);
    beauty_face_filter_->setBlurAlpha(value/10);
    
    gpuSourceImage->Render();
    
    unsigned char* res = gpuSourceImage->captureAProcessedFrameData(beauty_face_filter_);
    int imageWidth = gpuSourceImage->getRotatedFramebufferWidth();
    int imageHeight = gpuSourceImage->getRotatedFramebufferHeight();
    
//    gpuSourceImage->getFramebuffer()->active();
//    GPUPixelContext::getInstance()->capturedFrameData =
//    new unsigned char[imageWidth * imageHeight * 4];
//    CHECK_GL(glReadPixels(0, 0, imageWidth, imageHeight, GL_RGBA,
//                          GL_UNSIGNED_BYTE,
//                          GPUPixelContext::getInstance()->capturedFrameData));
//    gpuSourceImage->getFramebuffer()->inactive();
//    
//    unsigned char* res = GPUPixelContext::getInstance()->capturedFrameData;
    
//    for (int i = 0; i < imageWidth*imageHeight*4; i += 4) {
//        // Swap the red and blue channels
//        unsigned char temp = res[i];    // R
//        res[i] = res[i + 2];            // B
//        res[i + 2] = temp;              // Set original R to B
//    }
//    
    // make data provider with data.
    CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, res, imageWidth*imageHeight*4, NULL);
    
    // prep the ingredients
    int bitsPerComponent = 8;
    int bitsPerPixel = 32;
    int bytesPerRow = 4 * imageWidth;
    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
    CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
    CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
    
    // make the cgimage
    CGImageRef imageRef = CGImageCreate(imageWidth, imageHeight, bitsPerComponent, bitsPerPixel, bytesPerRow,     colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
    
    // then make the uiimage from that
    UIImage *image = [UIImage imageWithCGImage:imageRef];
    
    // Save the UIImage as a PNG file in the document directory
    if (image) {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths firstObject];
        NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"saved_image3.png"];
        
        NSData *pngData = UIImagePNGRepresentation(image);
        if ([pngData writeToFile:filePath atomically:YES]) {
            NSLog(@"Image saved successfully at %@", filePath);
        } else {
            NSLog(@"Failed to save image");
        }
        
        // Cleanup
        CGImageRelease(imageRef);
        CGDataProviderRelease(provider);
        CGColorSpaceRelease(colorSpaceRef);
        delete res;
@DamonsJ DamonsJ added the bug Something isn't working label Oct 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant