-
Notifications
You must be signed in to change notification settings - Fork 77
PAINTROID_457: Add .ora support for save/load #119
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
Draft
Amit-Matth
wants to merge
2
commits into
Catrobat:develop
Choose a base branch
from
Amit-Matth:PAINTROID-457
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import 'dart:convert'; | ||
import 'dart:typed_data'; | ||
import 'package:image/image.dart' as img; | ||
import 'package:archive/archive.dart'; | ||
|
||
class OraImage { | ||
final int width; | ||
final int height; | ||
final List<img.Image> layers; | ||
final String xmlMetadata; | ||
|
||
OraImage({ | ||
required this.width, | ||
required this.height, | ||
required this.layers, | ||
required this.xmlMetadata, | ||
}); | ||
|
||
Uint8List toBytes() { | ||
final archive = Archive(); | ||
|
||
final mimetypeContent = utf8.encode('image/openraster'); | ||
archive.addFile( | ||
ArchiveFile('mimetype', mimetypeContent.length, mimetypeContent) | ||
..compress = false, | ||
); | ||
|
||
for (int i = 0; i < layers.length; i++) { | ||
final layer = layers[i]; | ||
final encoder = img.PngEncoder(); | ||
final layerData = encoder.encodeImage(layer); | ||
archive.addFile( | ||
ArchiveFile('data/layer_$i.png', layerData.length, layerData)); | ||
} | ||
|
||
final encodedXml = utf8.encode(xmlMetadata); | ||
archive.addFile(ArchiveFile('stack.xml', encodedXml.length, encodedXml)); | ||
|
||
final zipEncoder = ZipEncoder(); | ||
return Uint8List.fromList(zipEncoder.encode(archive)!); | ||
} | ||
|
||
static String generateXmlMetadataForOra( | ||
List<img.Image> layers, int width, int height) { | ||
var buffer = StringBuffer(); | ||
buffer.writeln('<?xml version="1.0" encoding="UTF-8"?>'); | ||
buffer.writeln('<image w="$width" h="$height">'); | ||
buffer.writeln(' <stack>'); | ||
|
||
for (int i = 0; i < layers.length; i++) { | ||
final layerName = 'Layer $i'; | ||
final layerSrc = 'data/layer_$i.png'; | ||
buffer.writeln( | ||
' <layer name="$layerName" src="$layerSrc" x="0" y="0" opacity="1.0" visibility="visible"/>'); | ||
} | ||
|
||
buffer.writeln(' </stack>'); | ||
buffer.writeln('</image>'); | ||
return buffer.toString(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import 'dart:typed_data'; | ||
import 'dart:ui' as ui; | ||
import 'package:archive/archive.dart'; | ||
import 'package:image/image.dart' as img; | ||
import 'package:xml/xml.dart' as xml; | ||
import 'package:paintroid/core/models/loggable_mixin.dart'; | ||
|
||
class ProcessOra with LoggableMixin { | ||
Future<List<ui.Image>> processOraFile(Archive archive) async { | ||
List<ui.Image> layers = []; | ||
ArchiveFile? stackXmlFile = archive.findFile('stack.xml'); | ||
|
||
if (stackXmlFile == null) { | ||
logger.severe('Error: stack.xml not found in ORA file.'); | ||
return layers; | ||
} | ||
|
||
if (stackXmlFile.content == null || stackXmlFile.content is! List<int>) { | ||
logger.severe('Error: stack.xml content is invalid.'); | ||
return layers; | ||
} | ||
|
||
String xmlContent = String.fromCharCodes(stackXmlFile.content as List<int>); | ||
xml.XmlDocument document; | ||
try { | ||
document = xml.XmlDocument.parse(xmlContent); | ||
} catch (e, s) { | ||
logger.severe('Error parsing stack.xml', e, s); | ||
return layers; | ||
} | ||
|
||
var imageElement = document.rootElement; | ||
var stackElement = imageElement.findElements('stack').firstOrNull; | ||
|
||
if (stackElement == null) { | ||
logger.severe('Error: <stack> element not found in stack.xml.'); | ||
return layers; | ||
} | ||
|
||
for (var layerElement in stackElement.findElements('layer')) { | ||
String? layerSrc = layerElement.getAttribute('src'); | ||
if (layerSrc == null) { | ||
logger | ||
.warning('Warning: Layer element missing src attribute. Skipping.'); | ||
continue; | ||
} | ||
|
||
ArchiveFile? imageFile = archive.findFile(layerSrc); | ||
if (imageFile == null || !imageFile.isFile) { | ||
logger.warning( | ||
'Warning: Image file $layerSrc not found in archive for a layer. Skipping.'); | ||
continue; | ||
} | ||
|
||
if (imageFile.content == null || imageFile.content is! List<int>) { | ||
logger.warning( | ||
'Warning: Image file $layerSrc content is invalid. Skipping.'); | ||
continue; | ||
} | ||
|
||
img.Image? decodedImage; | ||
try { | ||
decodedImage = img.decodeImage(imageFile.content as List<int>); | ||
if (decodedImage == null) { | ||
logger.warning( | ||
'Warning: decodeImage returned null for $layerSrc. Skipping.'); | ||
continue; | ||
} | ||
} catch (e, s) { | ||
logger.severe('Error decoding image $layerSrc. Skipping.', e, s); | ||
continue; | ||
} | ||
|
||
ui.Image layerUiImage = await convertImgImageToUiImage(decodedImage); | ||
layers.add(layerUiImage); | ||
} | ||
|
||
return layers; | ||
} | ||
|
||
Future<ui.Image> convertImgImageToUiImage(img.Image image) async { | ||
List<int> pngBytes = img.encodePng(image); | ||
|
||
final codec = await ui.instantiateImageCodec(Uint8List.fromList(pngBytes)); | ||
final frame = await codec.getNextFrame(); | ||
return frame.image; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.