jya-dev / supernote-tool

Unofficial python tool for Supernote
Apache License 2.0
237 stars 26 forks source link

uncompressed bitmap length error #8

Closed fbalobanov closed 2 years ago

fbalobanov commented 2 years ago

Hi, thanks for the library, but it doesn't work for all files. I've tried to convert several notes and some of them are failing with the mistakes like this:


DecoderException Traceback (most recent call last) d:\Dropbox#PY\supernote-tool-master\supernote-tool.py in 25 for i in range(0, total_pages): 26 converter = sn.converter.ImageConverter(notebook) ---> 27 img = converter.convert(i) 28 29 html += '
'

d:\Dropbox#PY\supernote-tool-master\supernotelib\converter.py in convert(self, page_number, visibility_overlay) 62 page = self.note.get_page(page_number) 63 if page.is_layer_supported(): ---> 64 return self._convert_layered_page(page, self.palette, visibility_overlay) 65 else: 66 return self._convert_nonlayered_page(page, self.palette, visibility_overlay)

d:\Dropbox#PY\supernote-tool-master\supernotelib\converter.py in _convert_layered_page(self, page, palette, visibility_overlay) 88 if custom_bg: 89 decoder = Decoder.PngDecoder() ---> 90 img = self._create_image_from_decoder(decoder, binary, palette=palette, blank_hint=all_blank) 91 imgs[layer_name] = img 92 return self._flatten_layers(page, imgs, visibility_overlay)

d:\Dropbox#PY\supernote-tool-master\supernotelib\converter.py in _create_image_from_decoder(self, decoder, binary, palette, blank_hint) 116 117 def _create_image_from_decoder(self, decoder, binary, palette=None, blank_hint=False): --> 118 bitmap, size, bpp = decoder.decode(binary, palette=palette, all_blank=blank_hint) 119 if bpp == 32: 120 img = Image.frombytes('RGBA', size, bitmap)

d:\Dropbox#PY\supernote-tool-master\supernotelib\decoder.py in decode(self, data, palette, all_blank) 184 expected_length = fileformat.PAGE_HEIGHT fileformat.PAGE_WIDTH int(bit_per_pixel / 8) 185 if len(uncompressed) != expected_length: --> 186 raise exceptions.DecoderException(f'uncompressed bitmap length = {len(uncompressed)}, expected = {expected_length}') 187 188 return bytes(uncompressed), (fileformat.PAGE_WIDTH, fileformat.PAGE_HEIGHT), bit_per_pixel

DecoderException: uncompressed bitmap length = 2618568, expected = 2628288

jya-dev commented 2 years ago

Hi, There may be a problem with the handling of custom templates. I will check the details so could you please send me the note file that causes the error?

fbalobanov commented 2 years ago

Hi, I think you are right, the other notes with the plain template are working.

I've created a new note with the custom template, which the converter fails to convert. Here it is: https://www.dropbox.com/s/gppj54q61ssvwvh/test.note?dl=0

and here is the template: https://www.dropbox.com/s/2mn2qjslolas756/byjo.png?dl=0

jya-dev commented 2 years ago

Hi, I found a strange metadata in your test.note and Work.note (received from email). The metadata of a page generally has MAINLAYER and BGLAYER (and optionally LAYER1, 2, 3), but the files that cause the error have two MAINLAYERs and no BGLAYER. I don't know why.

$ python3 ./supernote-tool.py analyze test.note | jq .__pages__[0].__layers__
[
  {
    "LAYERTYPE": "NOTE",
    "LAYERPROTOCOL": "RATTA_RLE",
    "LAYERNAME": "MAINLAYER",
    "LAYERPATH": "0",
    "LAYERBITMAP": "16472",
    "LAYERVECTORGRAPH": "0",
    "LAYERRECOGN": "0"
  },
  {},
  {},
  {},
  {
    "LAYERTYPE": "NOTE",
    "LAYERPROTOCOL": "RATTA_RLE",
    "LAYERNAME": "MAINLAYER",       <--------- duplicated layer name; normally "BGLAYER"
    "LAYERPATH": "0",
    "LAYERBITMAP": "293",
    "LAYERVECTORGRAPH": "0",
    "LAYERRECOGN": "0"
  }
]

I added a workaround to treat 2nd MAINLAYER as BGLAYER and now I can convert them. Please check latest code (commit d860016a37936465142380b3a8114a1cefa42e4b) helps you to convert your note files. Thanks.

jya-dev commented 2 years ago

I submitted a new commit 566b29ce437db642ff37f14f8bad02aba857749e that fix bitmap length problem. This fix solves conversion error you encountered with Trades.note (received from email).

This is the error message on trying to convert Trades.note with the tool before applying the patch:

supernotelib.exceptions.DecoderException: uncompressed bitmap length = 2613528, expected = 2628288

It is similar to your first report, so perhaps the first report was also due to this issue.

fbalobanov commented 2 years ago

Thanks for your prompt fix. On 11/22/21 0:00:38, jya @.***> wrote: Closed #8 [https://github.com/jya-dev/supernote-tool/issues/8]. — You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub [https://github.com/jya-dev/supernote-tool/issues/8#event-5651400140], or unsubscribe [https://github.com/notifications/unsubscribe-auth/AGFDXAR2E7OWD3TIV5RLJ5TUNHE7LANCNFSM5HY7X4XQ]. Triage notifications on the go with GitHub Mobile for iOS [https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&amp;mt=8&amp;pt=524675] or Android [https://play.google.com/store/apps/details?id=com.github.android&amp;referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub].

fbalobanov commented 2 years ago

Hi, just wanted to let you know that the latest supernote beta 2.04 changed the .note format, and the library not working anymore with it. (UnsupportedFileFormat: unsupported file format)

I'm attaching a test .note from the new beta.

My understanding is that Ratta plans to release the new version in a couple of weeks, maybe even next week.

If you would be able to update the library it would be amazing

jya-dev commented 2 years ago

Thank you for the notice. Could you create a new issue on github? Please also attach a test.note file as I am not beta program user.

Thanks.